A small program that lets you know how many keys can be pressed at a time by QT (1)
The purpose of writing this log is to reencapsulate the QT key event system to create key processing that meets Game requirements. We know that sometimes the game needs to respond to multiple keys, this small program is a small attempt towards this goal.
Source code: here
The first is the header file of widget. H, which is the declaration of the window class.
#ifndef WIDGET_H#define WIDGET_H#include <QString>#include <QList>#include <QWidget>/*---------------------------------------------------------------------------*/class Widget: public QWidget{ Q_OBJECTpublic: Widget( void ); ~Widget( void );protected: void paintEvent( QPaintEvent* pEvent ); void keyPressEvent( QKeyEvent* pEvent ); void keyReleaseEvent( QKeyEvent* pEvent );private: void SetShowText( void ); QString m_Text; QString m_ShowText; QList<int> m_KeyPressedList;};/*---------------------------------------------------------------------------*/#endif // WIDGET_H
Here we have handled the pressing event, pop-up event, and draw event. We need to re-implement the drawing event because we need to update the text so that we can have a real-time understanding of the button processing situation.
Note that we use a qlist to save the pressed keys. That is to say, this data is the key to know how many keys can be pressed by QT at a time.
The following is the content of widget. cpp.
# Include <qpoint> # include <qtalgorithms> # include <qkeyevent> # include <qpainter> # include "widget. H "Widget: widget (void): qwidget (0) {// set some properties of the window setfixedsize (640,360); setwindowtitle (TR (" keytest ")); // set the text m_text = tr ("try to press Key. \ n "); m_keypressedlist.clear ();}/* ----------------------------------------------------------------------- */Widget ::~ Widget (void) {}/* alert */void Widget: paintevent (qpaintevent * pevent) {q_unused (pevent); qpainter painter (this); painter. drawtext (qpoint (32, 32), m_text); painter. drawtext (qpoint (32, 64), m_showtext);}/* prop */void Widget: keypressevent (qkeyevent * pevent) {m_keypressedlist.append (pevent-> key ()); m_text = tr ("the key % 1 pressed. "). arg (pevent-> key (); setshowtext (); Update ();}/* notify */void Widget: keyreleaseevent (qkeyevent * pevent) {m_keypressedlist.erase (qfind (m_keypressedlist.begin (), m_keypressedlist.end (), pevent-> key (); m_text = tr ("the key % 1 released. "). arg (pevent-> key (); setshowtext (); Update ();}/* show*/void Widget: setshowtext (void) {m_showtext = tr ("pressed keys:"); foreach (INT key, m_keypressedlist) {m_showtext + = key; m_showtext + = '\ n ';}}
When pressing the key, we add this key to the qlist. when the key is displayed, we search for this key and delete it by the qlist <t >:: erase method.
The following is a demo of the program:
I found that if you press 1 and 2 at the same time, the buttons after 3 cannot be pressed. If you press 2 and 3 at the same time, the keys 1 and 5 cannot be pressed, but the keys 6 can be pressed. What is this? I also hope that some people can give some advice. However, we can use this program to know the maximum number of keys supported by the QT application at a time.