ÍøÕ¾µ¼º½£ºÃâ·ÑÂÛÎÄ Ô­´´ÂÛÎÄ ÂÛÎÄËÑË÷ Ô­´´ÂÛÎÄ ÍøѧÈí¼þ ѧÊõ´ó¼Ò ×ÊÁÏÖÐÐÄ »áÔ±ÖÐÐÄ ÎÊÌâ½â´ð Ô­´´ÂÛÎÄ ÂÛÎÄËØ²Ä Éè¼ÆÏÂÔØ ×îÐÂÂÛÎÄ ÏÂÔØÅÅÐÐ ÂÛÎÄÉÏ´« ÔÚÏßͶ¸å ÁªÏµÎÒÃÇ
·µ»ØÍøѧÊ×Ò³
ÍøѧÁªÏµ
×îÐÂÂÛÎÄ ÍƼöרÌâ ÈÈÃÅÂÛÎÄ ËزÄרÌâ
µ±Ç°Î»Ö㺠Íøѧ > ±à³ÌÎĵµ > C/C++ > ÕýÎÄ
Qtѧϰ֮·(31): Ò»¸ö¼òÒ×»­°åµÄʵÏÖ(QWidget)_C/C+
À´Ô´£ºHttp://myeducs.cn ÁªÏµQQ£ºµã»÷ÕâÀï¸øÎÒ·¢ÏûÏ¢ ×÷Õߣº Óû§Í¶¸å À´Ô´£º ÍøÂç ·¢²¼Ê±¼ä£º 12/11/27
ÏÂÔØ{$ArticleTitle}Ô­´´ÂÛÎÄÑùʽ

1 2 3 4 ÏÂÒ»Ò³

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

¡¡¡¡ºÃÁË£¬ÏÖÔÚÏÈÀ´¿´¿´ÎÒÃǵÄÖ÷Ìå¿ò¼Ü¡£ÎÒÃǵĿò¼Ü»¹ÊÇʹÓÃQt Creator´´½¨Ò»¸öGui Application¹¤³Ì¡£

¡¡¡¡¼òµ¥µÄmain()º¯Êý¾Í²»ÔÙ׸ÊöÁË£¬ÕâÀïÊ×ÏÈÀ´¿´MainWindow¡£Ë³±ã˵һÏ£¬ÎÒÒ»°ã²»»áʹÓÃuiÎļþ£¬ËùÒÔÕâЩÄÚÈݶ¼ÊÇÊÖдµÄ¡£Ê×ÏÈÏÈÀ´¿´¿´×îÖÕµÄÔËÐнá¹û£º

Qtѧϰ֮·(31): Ò»¸ö¼òÒ×»­°åµÄʵÏÖ(QWidget)

¡¡¡¡»òÐíºÜ¼òµ¥£¬µ«ÊÇÖÁÉÙÎÒÃÇÄܹ»°ÑÇ°ÃæËù˵µÄ¸÷ÖÖ֪ʶ´®Á¬ÆðÀ´£¬ÕâÒ²¾Í´ïµ½Ä¿µÄÁË¡£

¡¡¡¡ÏÖÔÚÏÈÀ´¿´¿´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);
}

(ÔðÈα༭£ºadmin)

ÍøѧÍƼö

Ãâ·ÑÂÛÎÄ

Ô­´´ÂÛÎÄ

ä¯ÀÀ£º
ÉèΪÊ×Ò³ | ¼ÓÈëÊÕ²Ø | ÂÛÎÄÊ×Ò³ | ÂÛÎÄרÌâ | Éè¼ÆÏÂÔØ | ÍøѧÈí¼þ | ÂÛÎÄÄ£°å | ÂÛÎÄ×ÊÔ´ | ³ÌÐòÉè¼Æ | ¹ØÓÚÍøѧ | Õ¾ÄÚËÑË÷ | ÍøѧÁôÑÔ | ÓÑÇéÁ´½Ó | ×ÊÁÏÖÐÐÄ
°æȨËùÓÐ QQ:3710167 ÓÊÏ䣺3710167@qq.com ÍøѧÍø [Myeducs.cn] ÄúµçÄԵķֱæÂÊÊÇ ÏñËØ
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
ÏæICP±¸09003080ºÅ