This article supporting source code
This time, we'll explain how to use graphics view to implement the artboard as described earlier. There are a lot of good words about graphics view, but there is no concrete example of where it is. Now we will put the previous content using graphics view to realize, we can compare to see what the difference.
Similar to the previous content is no longer described, we are based on the last code to modify, in order to meet our needs. First look at the MainWindow code:
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::d rawlineactiontriggered ()
{
Emit changecurrentshape (shape::line);
}
void MainWindow::d rawrectactiontriggered ()
{
Emit changecurrentshape (shape::rect);
}
Since the Mainwindow.h code is the same as the previous text, it is no longer posted here. The CPP file contains only a few lines that are different from the previous text. Because we use graphics View, we have to add the item to the Qgprahicsscene. Here, we create the scene object, and the scene object needs to be observed through the view, so we need to use a Qgraphcisview object and add the view to the MainWindow.