1 2 3 4 5 ÏÂÒ»Ò³ ¡¡¡¡ÕâÒ»´Î½«½éÉÜÈçºÎʹÓÃGraphics ViewÀ´ÊµÏÖÇ°ÃæËù˵µÄ»°å¡£Ç°Ãæ˵Á˺ܶàÓйØGraphics ViewµÄºÃ»°£¬µ«ÊÇûÓоßÌåµÄʵÀýºÜÄÑ˵¾¿¾¹ºÃÔÚÄÄÀï¡£ÏÖÔÚÎÒÃǾͰÑÇ°ÃæµÄÄÚÈÝʹÓÃGraphics ViewÖØÐÂʵÏÖһϣ¬´ó¼Ò¿ÉÒÔ¶Ô±ÈһϿ´ÓÐʲôÇø±ð¡£ ¡¡¡¡Í¬Ç°ÃæÏàËƵÄÄÚÈݾͲ»ÔÙÐðÊöÁË£¬ÎÒÃÇ´ÓÉϴδúÂëµÄ»ù´¡ÉϽøÐÐÐ޸ģ¬ÒÔ±ã·ûºÏÎÒÃǵÄÐèÒª¡£Ê×ÏÈÀ´¿´MainWindowµÄ´úÂ룺 ¡¡¡¡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); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡QGraphicsView¡¡*view¡¡=¡¡new¡¡QGraphicsView(paintWidget,¡¡this); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡setCentralWidget(view); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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) |