QT Learning Pathway (32): implementation of a simple artboard (Graphics View)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.