From: http://blog.csdn.net/wsrlyk/article/details/5631573
Qt is a good library. Therefore, in some cases, you can build programs and game frameworks based on Qt.
The following describes the problems and solutions of Qt as a game framework.
(1) buttons
Reload the keyPressEvent, keyReleaseEvent, mousePressEvent, mouseReleaseEvent, and mouseMoveEvent functions in the Widget.
However, there is a problem with keyPressEvent. In Windows (I don't know about other environments ......), When you press and hold a key, the system will first respond, pause for a while, and then start to respond continuously. In the game, this feature shows that a person takes a step first, stops, and continues.
This feature affects the gaming experience, so the common solution is: press, set a flag to true, release, set flag to false, then, in the game rendering loop, the actions of the character are determined based on the flag value (that is, by waiting rather than interrupting)
However, there is still a problem with the Qt keyboard function. It does not trigger the keyPressEvent only when the key is pressed, but triggers the keyReleaseEvent only when the key is played. Instead, it triggers the keyPressEvent before the message is output, trigger keyReleaseEvent after output ". When you press and hold a key, press, release, press, press, and release constantly ......
Fortunately, Qt provides another function, which provides autorepeat judgment in the keyboard event class QKeyEvent, that is, the keyboard events triggered when the key is pressed belong to the autorepeat type. Therefore, you can exclude the press and release in the middle.
However, Qt Keyboard Events still have a very strong phenomenon (I don't know why). When holding down a key:
1. Trigger keyPressEvent, isAutoRepeat () returns false
2.NoTrigger keyReleaseEvent, pause for a moment
3. Trigger keyPressEvent, isAutoRepeat () returns true
4. Trigger keyReleaseEvent
5. If the button is not released, isAutoRepeat () returns true, and 3; releases the button, and isAutoRepeat () returns false
So sometimes you need to set a flag to avoid the impact of step 2.
The final code is as follows:
KeyPress
[Cpp]
View plaincopyprint?
- VoidMyWidget: keyPressEvent (QKeyEvent * evt)
- {
- Switch(Evt-> key ()){
- CaseQt: Key_W:
- If(! Evt-> isAutoRepeat ()&&! MKeyW ){
- MKeyW =True;
- // Press the event processing statement w.
- }
- Break;
- Default:Break;
- }
- QWidget: keyPressEvent (evt );
- }
Void MyWidget: keyPressEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (! Evt-> isAutoRepeat ()&&! MKeyW) {<br/> mKeyW = true; <br/> // The event processing statement for pressing w <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyPressEvent (evt); <br/>}< br/>
KeyRelease
[C-sharp]
View plaincopyprint?
- VoidMyWidget: keyReleaseEvent (QKeyEvent * evt)
- {
- Switch(Evt-> key ()){
- CaseQt: Key_W:
- If(MKeyW &&! Evt-> isAutoRepeat ()){
- MKeyW =False;
- // Release w's event processing statement.
- }
- Break;
- Default:Break;
- }
- QWidget: keyReleaseEvent (evt );
- }
Void MyWidget: keyReleaseEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (mKeyW &&! Evt-> isAutoRepeat () {<br/> mKeyW = false; <br/> // release the w event processing statement <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyReleaseEvent (evt); <br/>}< br/>
To be continued.
From: http://blog.csdn.net/wsrlyk/article/details/5631573
Qt is a good library. Therefore, in some cases, you can build programs and game frameworks based on Qt.
The following describes the problems and solutions of Qt as a game framework.
(1) buttons
Reload the keyPressEvent, keyReleaseEvent, mousePressEvent, mouseReleaseEvent, and mouseMoveEvent functions in the Widget.
However, there is a problem with keyPressEvent. In Windows (I don't know about other environments ......), When you press and hold a key, the system will first respond, pause for a while, and then start to respond continuously. In the game, this feature shows that a person takes a step first, stops, and continues.
This feature affects the gaming experience, so the common solution is: press, set a flag to true, release, set flag to false, then, in the game rendering loop, the actions of the character are determined based on the flag value (that is, by waiting rather than interrupting)
However, there is still a problem with the Qt keyboard function. It does not trigger the keyPressEvent only when the key is pressed, but triggers the keyReleaseEvent only when the key is played. Instead, it triggers the keyPressEvent before the message is output, trigger keyReleaseEvent after output ". When you press and hold a key, press, release, press, press, and release constantly ......
Fortunately, Qt provides another function, which provides autorepeat judgment in the keyboard event class QKeyEvent, that is, the keyboard events triggered when the key is pressed belong to the autorepeat type. Therefore, you can exclude the press and release in the middle.
However, Qt Keyboard Events still have a very strong phenomenon (I don't know why). When holding down a key:
1. Trigger keyPressEvent, isAutoRepeat () returns false
2.NoTrigger keyReleaseEvent, pause for a moment
3. Trigger keyPressEvent, isAutoRepeat () returns true
4. Trigger keyReleaseEvent
5. If the button is not released, isAutoRepeat () returns true, and 3; releases the button, and isAutoRepeat () returns false
So sometimes you need to set a flag to avoid the impact of step 2.
The final code is as follows:
KeyPress
[Cpp]
View plaincopyprint?
- VoidMyWidget: keyPressEvent (QKeyEvent * evt)
- {
- Switch(Evt-> key ()){
- CaseQt: Key_W:
- If(! Evt-> isAutoRepeat ()&&! MKeyW ){
- MKeyW =True;
- // Press the event processing statement w.
- }
- Break;
- Default:Break;
- }
- QWidget: keyPressEvent (evt );
- }
Void MyWidget: keyPressEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (! Evt-> isAutoRepeat ()&&! MKeyW) {<br/> mKeyW = true; <br/> // The event processing statement for pressing w <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyPressEvent (evt); <br/>}< br/>
KeyRelease
[C-sharp]
View plaincopyprint?
- VoidMyWidget: keyReleaseEvent (QKeyEvent * evt)
- {
- Switch(Evt-> key ()){
- CaseQt: Key_W:
- If(MKeyW &&! Evt-> isAutoRepeat ()){
- MKeyW =False;
- // Release w's event processing statement.
- }
- Break;
- Default:Break;
- }
- QWidget: keyReleaseEvent (evt );
- }
Void MyWidget: keyReleaseEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (mKeyW &&! Evt-> isAutoRepeat () {<br/> mKeyW = false; <br/> // release the w event processing statement <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyReleaseEvent (evt); <br/>}< br/>
To be continued.