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