The default in the Qwidget class is to ignore the Inputmethodevent event (the event must be used to get the input)

Source: Internet
Author: User

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?
  1. <span style="FONT-SIZE:18PX;"  >//BOOL Event (qevent *);
  2. virtual void mousepressevent (Qmouseevent *);
  3. virtual void mousereleaseevent (Qmouseevent *);
  4. virtual void mousedoubleclickevent (Qmouseevent *);
  5. virtual void mousemoveevent (Qmouseevent *);
  6. virtual void wheelevent (Qwheelevent *);
  7. virtual void keypressevent (Qkeyevent *);
  8. virtual void keyreleaseevent (Qkeyevent *);
  9. virtual void focusinevent (Qfocusevent *);
  10. virtual void focusoutevent (Qfocusevent *);
  11. virtual void enterevent (Qevent *);
  12. virtual void leaveevent (Qevent *);
  13. virtual void PaintEvent (Qpaintevent *);
  14. virtual void moveevent (Qmoveevent *);
  15. virtual void resizeevent (Qresizeevent *);
  16. virtual void closeevent (Qcloseevent *);
  17. virtual void contextmenuevent (Qcontextmenuevent *);
  18. virtual void tabletevent (Qtabletevent *);
  19. virtual void ActionEvent (Qactionevent *);
  20. virtual void dragenterevent (Qdragenterevent *);
  21. virtual void dragmoveevent (Qdragmoveevent *);
  22. virtual void dragleaveevent (Qdragleaveevent *);
  23. virtual void dropevent (Qdropevent *);
  24. virtual void showevent (Qshowevent *);
  25. virtual void hideevent (Qhideevent *);
  26. #if defined (Q_WS_MAC)
  27. virtual bool Macevent (Eventhandlercallref, eventref);
  28. #endif
  29. #if defined (Q_ws_win)
  30. virtual bool WinEvent (MSG *message, long *result);
  31. #endif
  32. #if defined (q_ws_x11)
  33. virtual BOOL X11event (XEvent *);
  34. #endif
  35. #if defined (Q_WS_QWS)
  36. virtual BOOL Qwsevent (qwsevent *);
  37. #endif
  38. Misc. Protected functions
  39. virtual void ChangeEvent (Qevent *);
  40. virtual void inputmethodevent (Qinputmethodevent *);
  41. virtual void mousemoveevent (qmouseevent *);
  42. virtual void mousepressevent (qmouseevent *);
  43. virtual void keypressevent (qkeyevent *);
  44. virtual void PaintEvent (qpaintevent *);
  45. 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?
    1. <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?
    1. <span style="FONT-SIZE:18PX;"  > this->setattribute (qt::wa_inputmethodenabled);
    2. This->setattribute (qt::wa_keycompression);
    3. 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?
    1. <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?
    1. <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?
  1. <span style="FONT-SIZE:18PX;"  > Qpainter painter (this);
  2. Qpen pen; //Brushes
  3. Qbrush Brush; //Painting Brush
  4. Painter.drawline (0,0,100,200);
  5. Pen.setcolor (Qcolor (255,0,0));
  6. Brush.setcolor (Qcolor (0,255,0,255));
  7. Brush.setstyle (Qt::solidpattern);
  8. Painter.setpen (pen); //Add brushes
  9. Painter.setbrush (brush); //Add paint brushes
  10. 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)

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.