QT Learning Pathway (31): implementation of a simple artboard (qwidget)

Source: Internet
Author: User

To tell the truth, originally I did not intend to put a big example, a more complex, and perhaps need many times to say. However, now that the drawing part has been finished, the plan is the previous example. Here I will only make a simple artboard program, in general, can draw a straight line and rectangular bar. In this way, I plan to split into two implementations, one using the normal qwidget as the artboard, and the second using the Graphcis View framework. Because a friend in the front said that they don't understand graphics view, so the plan.

OK, now let's take a look at our main frame. Our framework still uses QT creator to create a GUI application project.

The simple main () function is no longer in the details, here first look at the MainWindow. By the way, I don't normally use a UI file, so all of this stuff is handwritten. First let's take a look at the final running results:

It may be simple, but at least we are able to connect the various kinds of knowledge that we have mentioned, and that is the end of it.

Now let's look at MainWindow's code:

mainwindow.h


#ifndef Mainwindow_h


#define MAINWINDOW_H


#include <QtGui>


#include "shape.h"


#include "paintwidget.h"


class Mainwindow:public Qmainwindow


{


Q_object


Public:


MainWindow (Qwidget *parent = 0);


Signals:


void Changecurrentshape (Shape::code newshape);


Private Slots:


void drawlineactiontriggered ();


void drawrectactiontriggered ();


};


#endif//Mainwindow_h





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


Setcentralwidget (Paintwidget);


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


}

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.