等待,直到按下按钮(QT)

Wait until button pressed(QT)

本文关键字:按钮 QT 等待      更新时间:2023-10-16

这是我在SO^_^上的第一篇文章

我正在尝试用QT框架实现一个字母争夺游戏。主要部分已经完成了,但现在我惊呆了——我不知道如何制作程序来等待游戏中的按钮被按下。

请帮我。提前谢谢

这是我游戏周期的伪代码:

//initialize grid, score, time
// accept words until timer expires
while (true)    {
    // draw the current state of the grid
    // log board
    // get current time
    // report score
    // check for game's end
    // report time remaining
    // prompt for word and converting it to char*
    //HOW TO WAIT UNTILL BUTTON PRESSED????!!!!
    // check whether to scramble grid
    // or to look for word on grid and in dictionary
}

附言:由于这是我在这里的第一篇文章,我将感谢你对如何更正确地撰写问题的任何建议。

在我看来,您缺少的是Qt是基于事件驱动编程的。Qt提供小部件/对象,如按钮、窗口、控制器等,您可以向用户显示。例如,QPushButton对象能够接收各种事件,例如单击鼠标按钮或按下键盘键等。

对于事件,Qt提供用于接收这些事件并对其作出反应的槽/信号机制。因此,举个例子,在QPushButton的情况下,有一个叫做"按下"的信号。当用户点击按钮时,就会发出按下的信号。作为开发人员,您可以将按下的信号连接到插槽,这是您定义的函数。

例如,在从QObject派生的名为MyClass的类中:-

QPushButton button("Select"); // create a QPushButton object
connect(button, SIGNAL(pressed()), this, SLOT(buttonPressed()));

connect语句将按钮的"按下"信号连接到名为"buttonPressed"的插槽函数。然后您可以定义该函数:-

void MyClass::buttonPressed()
{
    // handle the button pressed event....
}

因此,实际上你没有任何等待按钮按下的调用,因为框架和它的架构是这样设计的,你不应该把所有东西都放在一个while(true)循环中。