这是我在 SO 上发表的第一篇文章 ^_^
我正在尝试实现 letter scramble game与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 基于 event-driven programming 。 Qt 提供向用户显示的小部件/对象,例如按钮、窗口、 Controller 等。例如,QPushButton 对象能够接收各种事件,例如单击鼠标按钮或按下键盘按键等等。
对于事件,Qt 提供了一个槽/信号机制来接收这些事件并对它们使用react。因此,举个例子,在 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) 循环中。
关于c++ - 等待按钮按下(QT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17858847/