ÈÕÆÚ£º2011-03-22 13:54:00  À´Ô´£º±¾Õ¾ÕûÀí

<b>Qtѧϰ֮·(31):Ò»¸ö¼òÒ×»­°åµÄʵÏÖ(QWidget)</b>[VC/C++±à³Ì]

ÔÞÖúÉÌÁ´½Ó



¡¡¡¡±¾ÎÄ¡°<b>Qtѧϰ֮·(31):Ò»¸ö¼òÒ×»­°åµÄʵÏÖ(QWidget)</b>[VC/C++±à³Ì]¡±ÊÇÓÉÆßµÀÆæΪÄú¾«ÐÄÊÕ¼¯£¬À´Ô´ÓÚÍøÂçתÔØ£¬ÎÄÕ°æȨ¹éÎÄÕÂ×÷ÕßËùÓУ¬±¾Õ¾²»¶ÔÆä¹ÛµãÒÔ¼°ÄÚÈÝ×öÈκÎÆÀ¼Û£¬Çë¶ÁÕß×ÔÐÐÅжϣ¬ÒÔÏÂÊÇÆä¾ßÌåÄÚÈÝ£º

˵Õæ»°,±¾À´ÎÒÊÇûÓдòËã·ÅÒ»¸öºÜ´óµÄÀý×ÓµÄ,Ò»Ôò¶Ô±È¸´ÔÓ,¶þÀ´´ó¸ÅÐèÇóºÜ¶à´Î²ÅÄÜ˵µÃÍê.²»¹ý,Ä¿Ç°ÒѾ­ËµÍêÁË»­Í¼²¿·Ý,ËùÒԳﻮ»¹ÊÇÉÏÒ»¸öÕâÑùµÄÀý×Ó.ÕâÀïÎÒ»áÖ»×ö³öÒ»¸ö¼òµ¥µÄ»­°å³ÌÐò,´óÌåÉϾÍÊÇ¿ÉÒÔ»­Ö±Ïߺ;ØÐΰÉ.ÕâÑù,Îҳﻮ·Ö³ÉÁ½ÖÖʵÏÖ,Ò»ÊÇÀûÓÃÆÕͨµÄQWidget×÷Ϊ»­°å,µÚ¶þÔòÊÇÀûÓÃGraphcis View FrameworkÀ´ÊµÏÖ.ÒòΪǰÃæÓÐÅóÓÑ˵²»´ó¶®µÃGraphics ViewµÄÏà¹ØÄÚÈÝ,ËùÒԳﻮÈç´Ë.

ºÃÁË,Ä¿Ç°ÏÈÀ´¿´¿´ÎÒÃǵÄÖ÷Ìå¿ò¼Ü.ÎÒÃǵĿò¼Ü»¹ÊÇÀûÓÃQt Creator³ÉÁ¢Ò»¸öGui Application¹¤³Ì.

¼òµ¥µÄmain()º¯Êý¾Í²»ÔÙ׸ÊöÁË,ÕâÀïÊ×ÏÈÀ´¿´MainWindow.³Ã±ã˵һÏÂ,ÎÒÆÕͨ²»»áÀûÓÃuiÎļþ,ËùÒÔÕâЩÄÚÈݶ¼ÊÇÊÖдµÄ.Ê×ÏÈÏÈÀ´¿´¿´ÖÕ¾¿µÄÔËÐкó¹û:

´ó¸ÅºÜ¼òµ¥,µ«ÊÇÖÁÉÙÎÒÃÇ¿ÉÒÔ°ÑÇ°ÃæËù˵µÄ¸÷ÖÖ֪ʶ´®Í¨ÆðÀ´,ÕâÒ²¾Íµ½´ïÄ¿µÄÁË.

Ä¿Ç°ÏÈÀ´¿´¿´MainWindowµÄ´úÂë:

mainwindow.h
#ifndef¡¡MAINWINDOW_H
#define¡¡MAINWINDOW_H
#include¡¡<QtGui>
#include¡¡"shape.h"
#include¡¡"paintwidget.h"
class¡¡MainWindow¡¡:¡¡public¡¡QMainWindow
{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Q_OBJECT
public:
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MainWindow(QWidget¡¡*parent¡¡=¡¡0);
signals:
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡void¡¡changeCurrentShape(Shape::Code¡¡newShape);
private¡¡slots:
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡void¡¡drawLineActionTriggered();
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡void¡¡drawRectActionTriggered();
};
#endif¡¡//¡¡MAINWINDOW_H

mainwindow.cpp
#include¡¡"mainwindow.h"
MainWindow::MainWindow(QWidget¡¡*parent)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡:¡¡QMainWindow(parent)
{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡QToolBar¡¡*bar¡¡=¡¡this->addToolBar("Tools");
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡QActionGroup¡¡*group¡¡=¡¡new¡¡QActionGroup(bar);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡QAction¡¡*drawLineAction¡¡=¡¡new¡¡QAction("Line",¡¡bar);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawLineAction->setIcon(QIcon(":/line.png"));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawLineAction->setToolTip(tr("Draw¡¡a¡¡line."));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawLineAction->setStatusTip(tr("Draw¡¡a¡¡line."));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawLineAction->setCheckable(true);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawLineAction->setChecked(true);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡group->addAction(drawLineAction);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bar->addAction(drawLineAction);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡QAction¡¡*drawRectAction¡¡=¡¡new¡¡QAction("Rectangle",¡¡bar);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawRectAction->setIcon(QIcon(":/rect.png"));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawRectAction->setToolTip(tr("Draw¡¡a¡¡rectangle."));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawRectAction->setStatusTip(tr("Draw¡¡a¡¡rectangle."));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡drawRectAction->setCheckable(true);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡group->addAction(drawRectAction);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bar->addAction(drawRectAction);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡QLabel¡¡*statusMsg¡¡=¡¡new¡¡QLabel;
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡statusBar()->addWidget(statusMsg);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡PaintWidget¡¡*paintWidget¡¡=¡¡new¡¡PaintWidget(this);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡setCentralWidget(paintWidget);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connect(drawLineAction,¡¡SIGNAL(triggered()),
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this,¡¡SLOT(drawLineActionTriggered()));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connect(drawRectAction,¡¡SIGNAL(triggered()),
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this,¡¡SLOT(drawRectActionTriggered()));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connect(this,¡¡SIGNAL(changeCurrentShape(Shape::Code)),
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡paintWidget,¡¡SLOT(setCurrentShape(Shape::Code)));
}
void¡¡MainWindow::drawLineActionTriggered()
{
¡¡emit¡¡changeCurrentShape(Shape::Line);
}
void¡¡MainWindow::drawRectActionTriggered()
{
¡¡emit¡¡changeCurrentShape(Shape::Rect);
}


¡¡¡¡ÒÔÉÏÊÇ¡°<b>Qtѧϰ֮·(31):Ò»¸ö¼òÒ×»­°åµÄʵÏÖ(QWidget)</b>[VC/C++±à³Ì]¡±µÄÄÚÈÝ£¬Èç¹ûÄã¶ÔÒÔÉϸÃÎÄÕÂÄÚÈݸÐÐËȤ£¬Äã¿ÉÒÔ¿´¿´ÆßµÀÆæΪÄúÍƼöÒÔÏÂÎÄÕ£º

  • <b>hostsÊÇʲô hostsÎļþÔÚʲôλÖà ÈôºÎ¸ÄÕýhosts</b>
  • <b>ÔÚ Windows 8 ÖÐÊÖ¶¯°²×°ÓïÑÔ°ü</b>
  • <b>Îå¸ö³£¼û PHPÊý¾Ý¿âÎÊÌâ</b>
  • WindowsÖÐAlt¼üµÄ12¸ö¸ßЧ¿ìËÙµÄÀûÓñ¾Áì½éÉÜ
  • <b>MySQL ORDER BY µÄʵÏÖ½âÎö</b>
  • <b>Ïê½âMySQL´æ´¢Àú³Ì²ÎÊýÓÐÈýÖÖ·¶Àý(in¡¢out¡¢inout)</b>
  • <b>Win8ϵͳ»Ö¸´³öÀ´¾­µäµÄ¿ªÊ¼²Ëµ¥µÄ°ì·¨</b>
  • <b>Win8ϵͳ»¨ÆÁÔõô°ì Win8ϵͳ»¨ÆÁµÄ°ìÀí°ì·¨</b>
  • <b>Windows 7ϵͳÏÂÎÞÏßÍø¿¨°²×°</b>
  • <b>Ϊʲô Linux²»ÐèÇóËéƬÕûÀí</b>
  • <b>Windows 8ÖÐɾ³ýÕË»§µÄ¼¸ÖÖ°ì·¨(ͼ)</b>
  • <b>½ÌÄãÈç°²ÔÚwin7ÏÂÅäÖ÷ÓÉÆ÷</b>
  • ±¾ÎĵØÖ·£º ÓëÄúµÄQQ/BBSºÃÓÑ·ÖÏí!
    • ºÃµÄÆÀ¼Û¡¡Èç¹ûÄú¾õµÃ´ËÎÄպ㬾ÍÇëÄú
        0%(0)
    • ²îµÄÆÀ¼Û¡¡Èç¹ûÄú¾õµÃ´ËÎÄÕ²¾ÍÇëÄú
        0%(0)

    ÎÄÕÂÆÀÂÛÆÀÂÛÄÚÈÝÖ»´ú±íÍøÓѹ۵㣬Óë±¾Õ¾Á¢³¡Î޹أ¡

       ÆÀÂÛÕªÒª(¹² 0 Ìõ£¬µÃ·Ö 0 ·Ö£¬Æ½¾ù 0 ·Ö) ²é¿´ÍêÕûÆÀÂÛ
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .