如何使关键字在Qt :: 4.6(C ++)中立即生效而没有延迟? [英] How to make keyword to work instantly with no delay in Qt::4.6 (C++)?

查看:70
本文介绍了如何使关键字在Qt :: 4.6(C ++)中立即生效而没有延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OS :: win xp sp3.

OS:: win xp sp3.

Qt :: 4.6

Qt:: 4.6

我有一个Gameboard类,其中有一些矩形.我为该矩形定义了keyPressEvent以便在屏幕上移动它.Key_D :: rectangle.moveToRight.Problem是按键延迟工作.当我释放一个键并按下另一个键时,该键需要一些时间才能开始工作.我检查了Qt在线文档,并确认了这种效果,但是现在不知道如何使这些键立即工作而又不会延迟它们之间的工作?

I have class Gameboard in which i have some rectangle.I defined keyPressEvent for that rectangle in order to move him around the screen.Key_A :: rectangle.moveToLeft & Key_D :: rectangle.moveToRight.Problem is that keys work with delay. When i release one key and press another one it takes some time before that one begin to work.I checked Qt online documentation and now for that effect but dont now how to make those keys to work instantly without delay beetween them?

代码段:

//in Gameboard class    

ship = new QRect(x,y,w,h);

void Gameboard::keyPressEvent(QKeyEvent* event)
{
switch(event->key())  {

case Qt::Key_A :
{ 
    x = x-10;
    ship->moveTo(x,y);
    break; 
}

case Qt::Key_D :
{
    x = x+10;
    ship->moveTo(x,y);
    break; 
}


}



}

推荐答案

将输入光标置于任何适用的文本框中,然后按'A'键.您会看到的是,一旦按下键,将打印字母'A',然后会有一个暂停,然后第一个字母'A'将被其他许多字母快速跟随.这就是按键事件的工作方式.并且您的程序正完全像这样接收它们.首先,当实际按下该键时,您会收到一个一个事件,然后在延迟一段时间后,您会收到很多自动重复的事件,以防用户要多次输入一个字符.

Put input cursor into any applicable text box and press the 'A' key. What you'll see is once you press the key, letter 'A' will be printed, then there will be a pause, and then first letter 'A' will be quickly followed by many others. That's the way keypress events work. And your program is receiving them exactly like this. First you receive one event when the key is actually pressed, and then after a delay you get a lot of automatically repeated events, in case user wants to enter one character many-many times.

它非常适合文本输入,但是在游戏中,您通常需要平稳移动.在这种情况下,您不需要在收到事件时就移动船舶,而应定期(例如在计时器事件中)移动船舶.并且您将需要同时捕获keyPressEvent和keyRelease事件,并使用它们来记住当前按下的移动键.因此,您可以例如执行以下操作:

It works perfectly for text input, but in games you usually need smooth movement. If that's the case, you need to move your ship not upon receiving the event, but regularly, for example, on timer event. And you will need to catch both keyPressEvent and keyRelease event and use them to remember what movement keys are currently pressed. So you could for example do this:

struct Ship {
    bool is_moving_left, is_moving_right;
    QPoint position;
    int speed;

    ...

    void timerEvent()
    {
        if (is_moving_left) position.setX (position.x() - speed);
        if (is_moving_right) position.setX (position.x() + speed);
    }

    ...
};

...

void Gameboard::keyPressEvent (OKeyEvent *_event)
{
    switch(event->key())  {

    case Qt::Key_A :
        ship->is_moving_left = true;
        break; 

    case Qt::Key_D :
        ship->is_moving_right = true;
        break; 
    }
}

...

void Gameboard::keyReleaseEvent (OKeyEvent *_event)
{
    switch(event->key())  {

    case Qt::Key_A :
        ship->is_moving_left = false;
        break; 

    case Qt::Key_D :
        ship->is_moving_right = false;
        break; 
    }
}

然后只需确保在游戏中的每个计时器事件上调用Ship :: timerEvent().

Then just make sure Ship::timerEvent() gets called on every timer event in the game.

这篇关于如何使关键字在Qt :: 4.6(C ++)中立即生效而没有延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆