CodePudding user response:
1. Create a new Qt project
File - & gt; New construction or project - & gt; Other projects - & gt; Empty Qt project
Such as named Qt_Instance_Example
2. Add a c + + source file
Such as named main. The CPP
Add the following code
[HTML] view plaincopyprint?
01. # include & lt; QApplication>
02. # include & lt; Mypainterwidget. H>
03.
04. Int main (int arg c, char * * argv)
05. {
06. QApplication a (arg c, argv);
7.
08. MyPainterWidget w (0);
09. Baron how ();
10. Return a.e xec ();
11.}
# include & lt; QApplication>
# include & lt; Mypainterwidget. H>
Int main (int arg c, char * * argv)
{
QApplication a (arg c, argv);
MyPainterWidget w (0);
Baron how ();
Return a.e xec ();
}
Here MyPainterWidget class is a subclass of our own writing QWidget class, used to implement the draw widgets,
We add this class and write the code below,
3. Add a c + + class, named MyPainterWidget
.h files below
(CPP) view plaincopyprint?
01. # # ifndef MYPAINTERWIDGET_H
02. # define MYPAINTERWIDGET_H
03.
04. # include & lt; QWidget>
05. # include & lt; QPoint>
06. # include
7.
08. Using namespace STD.
09.
10.
11.//line
12. Typedef struct myLine {
13. QPoint startPnt;
14. QPoint endPnt;
15.} myLine;
16.
17. The class MyPainterWidget: public QWidget
18. {
19. Public:
20. MyPainterWidget (QWidget * parent);
21. ~ MyPainterWidget ();
22.
23.//inheritance
24. The void paintEvent (QPaintEvent * p);
25. The void mousePressEvent (QMouseEvent * e);
26. The void mouseMoveEvent (QMouseEvent * e);
27. The void mouseReleaseEvent (QMouseEvent * e);
28.
29. The QPoint startPnt;//start
30. QPoint endPnt;//the end
31. Bool isPressed;//whether the mouse press
32.
33. Vector34.};
35.
36 # endif//MYPAINTERWIDGET_H
# # ifndef MYPAINTERWIDGET_H
# define MYPAINTERWIDGET_H
# include & lt; QWidget>
# include & lt; QPoint>
# include
using namespace std;
//line
Typedef struct myLine {
The QPoint startPnt;
The QPoint endPnt;
} myLine;
The 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);
The QPoint startPnt;//start
The QPoint endPnt;//the end
Bool isPressed;//whether the mouse press
Vector};
# endif//MYPAINTERWIDGET_H
.cpp file the following
(CPP) view plaincopyprint?
01. # include "mypainterwidget. H"
02. # include & lt; QString>
03. # include & lt; QMessageBox>
04. # include & lt; QPainter>
05. # include & lt; QPen>
06. # include & lt; QMouseEvent>
7.
08.
09. MyPainterWidget: : MyPainterWidget (QWidget * parent)
10. : QWidget (parent) {
11. SetMinimumSize (240120);
12. SetMaximumSize (480240);
13. This - & gt; SetMouseTracking (true);
14. This - & gt; IsPressed=false;
15.}
16.
17. MyPainterWidget: : ~ MyPainterWidget () {
18.
19.}
20.
21. The void MyPainterWidget: : paintEvent (QPaintEvent * p) {
22. QPainter painter (this);
23. The QPen pen;//create a brush
24. The pen. SetColor (Qt: : darkCyan);
25. The pen. SetWidth (5);
26. The painter. SetPen (pen);
27.
28. For (int I=0; i29. MyLine * pLine=lines [I];
30. Painter. DrawLine (pLine - & gt; StartPnt pLine - & gt; EndPnt);
31.}
32.}
33.
34. Void MyPainterWidget: : mousePressEvent (QMouseEvent * e) {
35. SetCursor (Qt: : PointingHandCursor);
36 startPnt=e - & gt; Pos ();
37. EndPnt=e - & gt; Pos ();
38. This - & gt; IsPressed=true;
39.//a QString MSG="(" + QString: : number (e - & gt; (x)) + ", "+ QString: : number (e - & gt; Y ()) + ") ";
40.//QMessageBox: : warning (this, tr (" warning "), MSG, QMessageBox: : Ok);
41.}
42.
43. The void MyPainterWidget: : mouseMoveEvent (QMouseEvent * e) {
44. The if (this - & gt; IsPressed) {
45. EndPnt=e - & gt; Pos ();
46.
47. MyLine * line=new myLine;//put the new line into the vector
48. The line - & gt; StartPnt=startPnt;
49. The line - & gt; EndPnt=endPnt;
50. This - & gt; Lines. The push_back (line);
51.
52. The update ();//repainter, call paintEvent
53. StartPnt=endPnt;
54.}
55.}
56.
57. The void MyPainterWidget: : mouseReleaseEvent (QMouseEvent * e) {
58. SetCursor (Qt: : ArrowCursor);
59. This - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull