A simple QT drawing program under Ubuntu

Source: Internet
Author: User

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

  1. #include <qapplication>
  2. #include <mypainterwidget.h>
  3. int main (int argc,char** argv)
  4. {
  5. Qapplication A (ARGC,ARGV);
  6. Mypainterwidget W (0);
  7. W.show ();
  8. return A.exec ();
  9. }

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

  1. #ifndef Mypainterwidget_h
  2. #define MYPAINTERWIDGET_H
  3. #include <QWidget>
  4. #include <QPoint>
  5. #include <vector>
  6. using namespace std;
  7. //Segment
  8. typedef struct myline{
  9. Qpoint Startpnt;
  10. Qpoint Endpnt;
  11. }myline;
  12. class Mypainterwidget: public Qwidget
  13. {
  14. Public:
  15. Mypainterwidget (qwidget* parent);
  16. ~mypainterwidget ();
  17. //Inheritance
  18. void paintevent (qpaintevent* p);
  19. void mousepressevent (qmouseevent *e);
  20. void mousemoveevent (qmouseevent *e);
  21. void mousereleaseevent (qmouseevent *e);
  22. Qpoint Startpnt; //Starting point
  23. Qpoint Endpnt; //End point
  24. bool ispressed; //The mouse is pressed
  25. Vector<myline*> lines; //Store all the segments www.linuxidc.com
  26. };
  27. #endif//Mypainterwidget_h

The. cpp file is as follows

  1. #include "mypainterwidget.h"
  2. #include <QString>
  3. #include <QMessageBox>
  4. #include <QPainter>
  5. #include <QPen>
  6. #include <QMouseEvent>
  7. Mypainterwidget::mypainterwidget (qwidget* parent)
  8. : Qwidget (parent) {
  9. Setminimumsize (240,120);
  10. Setmaximumsize (480,240);
  11. This->setmousetracking (true);
  12. This->ispressed = false;
  13. }
  14. Mypainterwidget::~mypainterwidget () {
  15. }
  16. void Mypainterwidget::p aintevent (qpaintevent*p) {
  17. qpainter painter (this);
  18. Qpen pen; //Create a brush
  19. Pen.setcolor (Qt::d Arkcyan);
  20. Pen.setwidth (5);
  21. Painter.setpen (pen);
  22. for (int i = 0;i<lines.size (); i++) {
  23. myline* pLine = lines[i];
  24. Painter.drawline (PLINE->STARTPNT,PLINE->ENDPNT);
  25. }
  26. }
  27. void Mypainterwidget::mousepressevent (Qmouseevent *e) {
  28. SetCursor (Qt::P ointinghandcursor);
  29. STARTPNT = E->pos ();
  30. ENDPNT = E->pos ();
  31. This->ispressed = true;
  32. //qstring msg = "(" +qstring::number (E->x ()) + "," +qstring::number (E->y ()) + ")";
  33. //qmessagebox::warning (this,tr ("Warning"), Msg,qmessagebox::ok);
  34. }
  35. void Mypainterwidget::mousemoveevent (Qmouseevent *e) {
  36. if (this->ispressed) {
  37. ENDPNT = E->pos ();
  38. myline* line = new myline; //put the new line into vector
  39. LINE->STARTPNT = STARTPNT;
  40. LINE->ENDPNT = ENDPNT;
  41. this->lines.push_back (line);
  42. Update (); //repainter,call paintevent
  43. STARTPNT = ENDPNT;
  44. }
  45. }
  46. void Mypainterwidget::mousereleaseevent (Qmouseevent *e) {
  47. SetCursor (Qt::arrowcursor);
  48. This->ispressed = false;
  49. }

3. The results of the operation are as follows

A simple QT drawing program under Ubuntu

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.