Download presentation
Presentation is loading. Please wait.
Published byMoses Evans Modified over 9 years ago
1
Adding a New Option to the Framework
2
Introduction This is intended as a step by step guide to adding a new action to the menu or toolbar. The order of steps is not necessary but useful from an instructional point of view. For this example I am going to have a button that draws a trapezoid when I press it.
3
Declare a New QAction QActions are generic items used by Qt to be added to toolbars and menus which provide a caption, a connection to the rest of the application and are triggerable. In MainWindow.h within the private section there is a set of QActions already declared so add it there. QAction *mousePolylineAct; QAction *mousePolygonAct; QAction *drawTrapezoidAct
4
Create QAction Within MainWindow.cpp is a method createActions() which creates all the actions used by the MainWindow widget. Instantiate the QAction with the caption Trapezoid drawTrapezoidAct = new QAction(tr(“Trapezoid"), this);
5
Create QAction Set the status tip which displays text in the status bar when the mouse cursor is over it. drawTrapezoidAct->setStatusTip(tr(“Draws a trapezoid")); Before we finish a brief discussion of Signals and Slots.
6
Signals and Slots Signals and Slots are the methods used by Qt to wire different widgets together Signals are messages transmitted when an event happens (a button is clicked, a slider is moved, etc.). Most Qt widgets have premade Signals like the QAction has triggered() which is fired when the mouse clicks it. You can also create your own but that is for another time.
7
Signals and Slots Slots are methods that are fired when it hears a signal is it listening for. Widgets often have slots already implemented but we will need to create our own for this example. Trapezoid QAction - drawTrapezoidAct GLWidget - glWindow Slot: setTrapezoidDrawing() Signal: triggered() connect
8
Signals and Slots Qt has a macro set up that provides the connection between a slot and a signal Usage: connect(object1, SIGNAL(object1Signal()), object2, SLOT(object2Slot())); The object that will fire an event The object that will handle the event The signal to be fired The method to handle the event
9
Create QAction Make the connection between the QAction’s trigger signal and the GLWidget’s slot (which will be created soon). connect(drawTrapezoidAct, SIGNAL(triggered()), glWindow, SLOT(drawTrapezoid()); To be declared and implemented in GLWidget class.
10
Add a New Toolbar Button Further down in MainWindow.cpp is the method createToolBar() which creates the toolbar and also adds the buttons to it. The toolbar’s addAction method adds a new item to the toolbar based on a created QAction. interactToolBar = addToolBar(tr("Interact")); interactToolBar->addAction(mouseLineAct); interactToolBar->addAction(mouseCircleAct); interactToolBar->addAction(mouseEllipseAct); interactToolBar->addAction(mousePolylineAct); interactToolBar->addAction(mousePolygonAct); interactToolBar->addAction(drawTrapezoidAct);
11
Add a Menu Item Alternately you can add a menu item within the method createMenus(). To add the QAction to the drawing menu you can use its addAction method. //The graphics menu graphicsMenu = menuBar()->addMenu(tr("&Graphics")); drawMenu = graphicsMenu->addMenu(tr("&Draw")); drawMenu->addAction(diagLineAct); drawMenu->addAction(diagCircleAct); drawMenu->addAction(diagEllipseAct); drawMenu->addAction(drawTrapezoidAct);
12
Add New Shape Option The job of the slot we are going to create is to set the shape we are going to be drawing. Inside GLWidget.h at the bottom of the public section is a set of variables which we will add a new constant class variable. static const int ELLIPSE; static const int POLYLINE; static const int POLYGON; static const int TRAPEZOID;
13
Add New Shape Option Now we need to initialize the class variable. At the top of GLWidget.cpp we will initialize the new constant class variable. const int GLWidget::ELLIPSE = 3; const int GLWidget::POLYGON = 4; const int GLWidget::POLYLINE = 5; const int GLWidget::TRAPEZOID = 6;
14
Creating a New Slot A slot is just like any other function / method but is declared as a slot. In GLWidget.h is a declaration section labeled public slots:. As would be expected these are slots with public scope.
15
Creating a New Slot Create the slot drawTrapezoid in GLWindow.h within the public slots section. /* >>>>>>>>>>>>>>>>>>>>>> Enables interactive polygon drawing */ void drawMousePolygon(); /* >>>>>>>>>>>>>>>>>>>>>> Enables the drawing of a trapezoid */ void drawTrapezoid();
16
Creating a New Slot Implement the slot drawTrapezoid in GLWindow.cpp. /* >>>>>>>>>>>>>>>>>>>>>>*/ void GLWidget::drawMouseLine() { drawMode = GLWidget::MOUSE; shapeMode = GLWidget::LINE; clearShapeVariables(); } /* >>>>>>>>>>>>>>>>>>>>>>*/ void GLWidget::drawTrapezoid() { shapeMode = GLWidget::TRAPEZOID; areShapesClear = false; }
17
Adding the Drawing Code All the drawing is called within the paintGL() method inside GLWidget.cpp. This method is called 50 times a second by a QTimer in the GLWidget constructor. The widget is cleared and then a shape to be drawn is selected in the switch statement.
18
Adding the Drawing Code Add a new case to the switch statement based on the variable we declared. case GLWidget::POLYGON: //stuff here break; case GLWidget::POLYLINE: //stuff here break; case GLWidget::TRAPEZOID: //stuff here break;
19
Adding the Drawing Code Here is some code that would draw a trapezoid based on the drawLine algorithm. case GLWidget::POLYLINE: //stuff here break; case GLWidget::TRAPEZOID: DrawingAlgorithms::drawLine(-10, 10, 10, 10); DrawingAlgorithms::drawLine(-20, -10, 20, -10); DrawingAlgorithms::drawLine(-10, 10, -20, -10); DrawingAlgorithms::drawLine(10, 10, 20, -10); break;
20
Compiling the Code Since we have done something with slots and signals we need to run qmake again to generate a new Makefile. QMake not only generates the Makefile but it also creates the support moc files which actually handle all this slots and signal stuff behind the scenes.
21
Finished This should allow you to add new toolbar or menus items to your applications. This does not cover everything you would ever need to know about Qt but should send you in the right direction.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.