21
Adding a New Option to the Framework

Adding a New Option to the Framework. Introduction This is intended as a step by step guide to adding a new action to the menu or toolbar. The order of

Embed Size (px)

Citation preview

Adding a New Option to the Framework

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.

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

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);

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.

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.

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

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 thatwill fire an event

The object thatwill handle the event

The signal to be fired

The method to handle the event

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.

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);

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 menugraphicsMenu = menuBar()->addMenu(tr("&Graphics"));drawMenu = graphicsMenu->addMenu(tr("&Draw"));drawMenu->addAction(diagLineAct);drawMenu->addAction(diagCircleAct);drawMenu->addAction(diagEllipseAct);drawMenu->addAction(drawTrapezoidAct);

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;

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;

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.

Creating a New Slot

• Create the slot drawTrapezoid in GLWindow.h within the public slots section.

/*<<<<<<<<<<<<<<<<<<<<<drawMousePolygon>>>>>>>>>>>>>>>>>>>>>>> Enables interactive polygon drawing */ void drawMousePolygon();

/*<<<<<<<<<<<<<<<<<<<<<<drawTrapezoid>>>>>>>>>>>>>>>>>>>>>>> Enables the drawing of a trapezoid*/void drawTrapezoid();

Creating a New Slot

• Implement the slot drawTrapezoid in GLWindow.cpp.

/*<<<<<<<<<<<<<<<<<<<<<drawMouseLine>>>>>>>>>>>>>>>>>>>>>>>*/void GLWidget::drawMouseLine(){ drawMode = GLWidget::MOUSE; shapeMode = GLWidget::LINE; clearShapeVariables();}

/*<<<<<<<<<<<<<<<<<<<<<<drawTrapezoid>>>>>>>>>>>>>>>>>>>>>>>*/void GLWidget::drawTrapezoid(){ shapeMode = GLWidget::TRAPEZOID; areShapesClear = false;}

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.

Adding the Drawing Code

• Add a new case to the switch statement based on the variable we declared.

case GLWidget::POLYGON: //stuff herebreak;

case GLWidget::POLYLINE: //stuff herebreak;

case GLWidget::TRAPEZOID: //stuff herebreak;

Adding the Drawing Code

• Here is some code that would draw a trapezoid based on the drawLine algorithm.

case GLWidget::POLYLINE: //stuff herebreak;

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;

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.

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.