QT is different from many GUI Libraries (such as MFC). It cannot draw images on the Interface anytime, anywhere, but only in the painterevent of the interface class. As a result, it is a little difficult to use the State mode (one of the 23 gof design modes) when drawing the QT interface. As a solution, I will first draw the image on the Interface on an image (qpixmap), and then "Draw" The pixmap ON THE painterevent interface. The following is a small example of this method.
:
Source code:
# Include <qtgui> // The base class of the Status class, which defines each public interface. // among them, setpixmap is a pure Virtual Interface Class basepen {protected: // these three class members should be private members of the basepen, and then access them through the interface. // For convenience, I set them as the protection member's qpixmap m_pixmap; qpoint m_startpoint; qpoint m_endpoint; virtual void setpixmap () = 0; public: basepen () {m_startpoint = m_endpoint = qpoint (0, 0); m_pixmap = qpixmap (500,500 );} void setstartpoint (qpoint point) {m_startpoint = point;} void setendpoint (qpoint point) {m_en Dpoint = point; setpixmap () ;}qpixmap getpixmap () {return m_pixmap ;}; // rectangle class, draw a red rectangle class rectpen on the interface: public basepen {protected: void setpixmap () {m_pixmap.fill (QT: White); qpainter painter (& m_pixmap); qrect rect (m_startpoint, m_endpoint); painter. setpen (QT: Red); painter. drawrect (rect) ;}}; // line class, draw a blue line class linepen: Public basepen {protected: void setpixmap () {m_pixmap.fill (QT :: white); qpainter Pai Nter (& m_pixmap); painter. setpen (QT: Blue); painter. drawline (m_startpoint, m_endpoint) ;}; // circular class, draw a green elliptical class circlepen: Public basepen {protected: void setpixmap () {m_pixmap.fill (QT:: White); qpainter painter (& m_pixmap); qrect rect (m_startpoint, m_endpoint); painter. setpen (QT: Green); painter. drawellipse (rect) ;}}; class Widget: Public qwidget {q_objectprivate: bool m_mousedown; basepen * m_basepen; rectpen * M_rectpen; linepen * m_linepen; circlepen * m_circlepen; // put three buttons on the interface to control the drawing status: Optional * m_linebutton; optional * m_rectbutton; qradiobutton * m_circlebutton; protected: void mousepressevent (qmouseevent * event); void mousemoveevent (qmouseevent * event); void mousereleaseevent (qmouseevent * event); void paintevent (qpaintevent * event); public: widget (qwidget * parent = 0 );~ Widget (); Private slots: void publish () {m_basepen = m_linepen;} void clickedrectbutton () {m_basepen = m_rectpen;} void clickedcirclebutton () {m_basepen = m_circlepen ;}}; widget: widget (qwidget * parent/* = 0 */): qwidget (parent) {m_mousedown = false; m_rectpen = new rectpen; m_linepen = new linepen; m_circlepen = new circlepen; m_linebutton = new qradiobutton ("line", this); m_rectbutton = new Qradiobutton ("rect", this); m_circlebutton = new qradiobutton ("circle", this); m_linebutton-> move (10, 10); m_rectbutton-> move (100, 10); m_circlebutton-> move (200, 10); Connect (m_linebutton, signal (clicked (), this, slot (clickedlinebutton (); Connect (m_rectbutton, signal (clicked (), this, slot (clickedrectbutton (); Connect (m_circlebutton, signal (clicked (), this, slot (clickedcirclebutton (); m_basepen = M_linepen; m_linebutton-> setchecked (true); setfixedsize (500,500);} Widget ::~ Widget () {Delete m_linepen; Delete m_rectpen; Delete m_circlepen;} void Widget: mousepressevent (qmouseevent * event) {If (Event-> button () = QT: leftbutton) {m_mousedown = true; m_basepen-> setstartpoint (Event-> pos () ;}} void Widget: mousemoveevent (qmouseevent * event) {If (m_mousedown) {m_basepen-> setendpoint (Event-> pos (); Update () ;}} void Widget: mousereleaseevent (qmouseevent * event) {If (Event-> button () ==qt: leftbutton) {m_mousedown = false ;}} void Widget: paintevent (qpaintevent * event) {qpixmap temp = m_basepen-> getpixmap (); qpainter painter (this); painter. drawpixmap (0, 0, temp);} # include "Main. MOC "int main (INT argc, char ** argv) {qapplication app (argc, argv); widget * ww = new widget; ww-> show (); return app.exe C ();}