单击消息框时,如何将其连接到插槽

  • 本文关键字:连接 插槽 消息 单击 qt
  • 更新时间 :
  • 英文 :


我想创建一个消息框,询问用户是否要再次播放。当用户单击一个按钮时,它执行任务。该任务是在插槽中定义的。如何将按钮单击连接到该插槽?

QMessageBox::StandardButton reply=QMessageBox::question(this,"GAME Over-Do you want to play again?");
connect(QMessageBox,SIGNAL(buttonClicked()),this,SLOT(box());

它显示QMessageBox是一类,并且无法将其连接到该插槽。我想连接到该插槽。

使用QmessageBox有不同的方法。您可以使用QMESSAGEBOX的阻止静态功能,然后检查响应:

QMessageBox::StandardButton reply = QMessageBox::question(this,"Title", "GAME Over-Do you want to play again?");
if(reply == QMessageBox::Yes)
{
    //call your slot
    //box();
    qDebug() << " Yes clicked";
}
else
{
    //Game over
    qDebug() << "game over";
}

,这将阻止您的代码执行,直到用户单击消息框中的某些按钮。

如果您需要代码向前运行而无需等待用户响应,则可以以非阻滞方式使用QmessageBox:

QMessageBox * msg = new QMessageBox(QMessageBox::Question, "Title", "GAME Over-Do you want to play again?", QMessageBox::Yes| QMessageBox::No, this);
connect(msg,SIGNAL(accepted()),this,SLOT(box()));
connect(msg,SIGNAL(rejected()),this,SLOT(gameover()));
msg->show();
qDebug() << "Not blocked";

最新更新