Tag: Call false. com source file creat setw PAC vector BSP
Original: http://www.linuxidc.com/Linux/2011-08/41220.htm
When I was learning MFC, the most classic example of getting started is the drawing program, which acts as the Hello World under the console application.
Now to start QT, can not help but nostalgia, so also wrote a drawing program, although simple, but also a prerequisite for the entry ah.
Environment
Os:ubuntu 11.04
IDE:QT Creator 2.2.1
qt:4.7.4 (32bit)
complier:gcc
1. Create a blank QT project
Files--New project or project--other projects--empty QT Project
For example, named Qt_instance_example.
2. Add a C + + source file
For example, named Main.cpp.
Add the following code
- #include <qapplication>
- #include <mypainterwidget.h>
- int main (int argc,char** argv)
- {
- Qapplication A (ARGC,ARGV);
- Mypainterwidget W (0);
- W.show ();
- return A.exec ();
- }
The Mypainterwidget class here is a subclass of the Qwidget class we write ourselves to implement the drawing widgets.
Let's add this class and write its code.
3. Add a C + + class named Mypainterwidget
The. h file is as follows
- #ifndef Mypainterwidget_h
- #define MYPAINTERWIDGET_H
- #include <QWidget>
- #include <QPoint>
- #include <vector>
- using namespace std;
- //Segment
- typedef struct myline{
- Qpoint Startpnt;
- Qpoint Endpnt;
- }myline;
- class Mypainterwidget: public Qwidget
- {
- Public:
- Mypainterwidget (qwidget* parent);
- ~mypainterwidget ();
- //Inheritance
- void paintevent (qpaintevent* p);
- void mousepressevent (qmouseevent *e);
- void mousemoveevent (qmouseevent *e);
- void mousereleaseevent (qmouseevent *e);
- Qpoint Startpnt; //Starting point
- Qpoint Endpnt; //End point
- bool ispressed; //The mouse is pressed
- Vector<myline*> lines; //Store all the segments www.linuxidc.com
- };
- #endif//Mypainterwidget_h
The. cpp file is as follows
- #include "mypainterwidget.h"
- #include <QString>
- #include <QMessageBox>
- #include <QPainter>
- #include <QPen>
- #include <QMouseEvent>
- Mypainterwidget::mypainterwidget (qwidget* parent)
- : Qwidget (parent) {
- Setminimumsize (240,120);
- Setmaximumsize (480,240);
- This->setmousetracking (true);
- This->ispressed = false;
- }
- Mypainterwidget::~mypainterwidget () {
- }
- void Mypainterwidget::p aintevent (qpaintevent*p) {
- qpainter painter (this);
- Qpen pen; //Create a brush
- Pen.setcolor (Qt::d Arkcyan);
- Pen.setwidth (5);
- Painter.setpen (pen);
- for (int i = 0;i<lines.size (); i++) {
- myline* pLine = lines[i];
- Painter.drawline (PLINE->STARTPNT,PLINE->ENDPNT);
- }
- }
- void Mypainterwidget::mousepressevent (Qmouseevent *e) {
- SetCursor (Qt::P ointinghandcursor);
- STARTPNT = E->pos ();
- ENDPNT = E->pos ();
- This->ispressed = true;
- //qstring msg = "(" +qstring::number (E->x ()) + "," +qstring::number (E->y ()) + ")";
- //qmessagebox::warning (this,tr ("Warning"), Msg,qmessagebox::ok);
- }
- void Mypainterwidget::mousemoveevent (Qmouseevent *e) {
- if (this->ispressed) {
- ENDPNT = E->pos ();
- myline* line = new myline; //put the new line into vector
- LINE->STARTPNT = STARTPNT;
- LINE->ENDPNT = ENDPNT;
- this->lines.push_back (line);
- Update (); //repainter,call paintevent
- STARTPNT = ENDPNT;
- }
- }
- void Mypainterwidget::mousereleaseevent (Qmouseevent *e) {
- SetCursor (Qt::arrowcursor);
- This->ispressed = false;
- }
3. The results of the operation are as follows
A simple QT drawing program under Ubuntu