Because of the needs of the project and the requirements of the supervisor, the project is ready to be transplanted into QT, which makes it easier to achieve cross-platform. Because the project was developed under Windows, the first platform was mobile so the Windows APIs and programming patterns that were used in addition to the underlying framework are now ported to QT, The first problem is how to transform the message mechanism of Windows into a processing mechanism in QT. Windows is a message, the QT is the event and signal, slot, in fact, the principle is the same.
1. Common Events
Because all of the interface-related classes are inherited from the Qwidget class, the Qwidget class has time and methods that are available in each interface-related class, with some of the most commonly used events as follows:
[CPP]View PlainCopyprint?
- <span style="FONT-SIZE:18PX;" >//BOOL Event (qevent *);
- virtual void mousepressevent (Qmouseevent *);
- virtual void mousereleaseevent (Qmouseevent *);
- virtual void mousedoubleclickevent (Qmouseevent *);
- virtual void mousemoveevent (Qmouseevent *);
- virtual void wheelevent (Qwheelevent *);
- virtual void keypressevent (Qkeyevent *);
- virtual void keyreleaseevent (Qkeyevent *);
- virtual void focusinevent (Qfocusevent *);
- virtual void focusoutevent (Qfocusevent *);
- virtual void enterevent (Qevent *);
- virtual void leaveevent (Qevent *);
- virtual void PaintEvent (Qpaintevent *);
- virtual void moveevent (Qmoveevent *);
- virtual void resizeevent (Qresizeevent *);
- virtual void closeevent (Qcloseevent *);
- virtual void contextmenuevent (Qcontextmenuevent *);
- virtual void tabletevent (Qtabletevent *);
- virtual void ActionEvent (Qactionevent *);
- virtual void dragenterevent (Qdragenterevent *);
- virtual void dragmoveevent (Qdragmoveevent *);
- virtual void dragleaveevent (Qdragleaveevent *);
- virtual void dropevent (Qdropevent *);
- virtual void showevent (Qshowevent *);
- virtual void hideevent (Qhideevent *);
- #if defined (Q_WS_MAC)
- virtual bool Macevent (Eventhandlercallref, eventref);
- #endif
- #if defined (Q_ws_win)
- virtual bool WinEvent (MSG *message, long *result);
- #endif
- #if defined (q_ws_x11)
- virtual BOOL X11event (XEvent *);
- #endif
- #if defined (Q_WS_QWS)
- virtual BOOL Qwsevent (qwsevent *);
- #endif
- Misc. Protected functions
- virtual void ChangeEvent (Qevent *);
- virtual void inputmethodevent (Qinputmethodevent *);
- virtual void mousemoveevent (qmouseevent *);
- virtual void mousepressevent (qmouseevent *);
- virtual void keypressevent (qkeyevent *);
- virtual void PaintEvent (qpaintevent *);
- virtual void inputmethodevent (qinputmethodevent *);</span>
If we want to use these events, we just need to inherit and override these event handlers in our own class.
There is another point in using
[CPP]View PlainCopy print?
- <span style="FONT-SIZE:18PX;" > virtual void inputmethodevent (qinputmethodevent *);</span>
We need to pay attention to this event, because this event is ignored by default in the Qwidget class, so if you want to intercept this event when using an input method, you need to set the property of your form class in your own class constructor, as follows:
[CPP]View PlainCopyprint?
- <span style="FONT-SIZE:18PX;" > this->setattribute (qt::wa_inputmethodenabled);
- This->setattribute (qt::wa_keycompression);
- This->setfocuspolicy (qt::wheelfocus);</span>
This event is still useful because when you use input methods, especially Chinese, you must use this event to get the input content.
The normal Latin alphabet gets the key value is very simple, overrides
[CPP]View PlainCopyprint?
- <span style="FONT-SIZE:18PX;" > virtual void keypressevent (qkeyevent *);</span>
The related events are on the line.
2. Simple 2D Drawing
Here to use the Qpainter class and some of its methods, and rewrite
[CPP]View PlainCopyprint?
- <span style="FONT-SIZE:18PX;" > virtual void PaintEvent (qpaintevent *);</span>
The event is OK, the simple use of the code is as follows:
[CPP]View PlainCopyprint?
- <span style="FONT-SIZE:18PX;" > Qpainter painter (this);
- Qpen pen; //Brushes
- Qbrush Brush; //Painting Brush
- Painter.drawline (0,0,100,200);
- Pen.setcolor (Qcolor (255,0,0));
- Brush.setcolor (Qcolor (0,255,0,255));
- Brush.setstyle (Qt::solidpattern);
- Painter.setpen (pen); //Add brushes
- Painter.setbrush (brush); //Add paint brushes
- Painter.drawrect (50,70,160,200); //Draw Rectangle </span>
Well, today is the return to C/S and QT after the first blog post, the content is relatively simple, but very basic, poor writing, I hope you forgive me.
It's over today!!
http://blog.csdn.net/songjinshi/article/details/7186524
The default in the Qwidget class is to ignore the Inputmethodevent event (the event must be used to get the input)