C++ Qt:如何在函数内显示消息框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5247979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt: How to display a Messagebox when you are within a function?
提问by Ikky
I'm developing using the Qt Nokia SDK.
我正在使用 Qt Nokia SDK 进行开发。
I'm having trouble displaying the buttons of a MessageBox, when trying to display a messagebox within a function. If i try to display it within the main window, there is no problem showing the buttons.
尝试在函数中显示消息框时,我无法显示消息框的按钮。如果我尝试在主窗口中显示它,则显示按钮没有问题。
The mainwindow consist of a QStackWidget which holds different widgets.
主窗口由一个 QStackWidget 组成,其中包含不同的小部件。
Here is the code that works in main window:
这是在主窗口中工作的代码:
QMessageBox msgBox;
msgBox.setText("Name");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard |
QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
Here is the function and code that i run after receiving a response from a web request (The messagebox displays, but not the buttons.
这是我在收到来自 Web 请求的响应后运行的函数和代码(显示消息框,但不显示按钮。
void MainWindow::RequestReceived()
{
QMessageBox *msgBox = new QMessageBox(this);
msgBox->setText("Test");
msgBox->setWindowModality(Qt::NonModal);
msgBox->setInformativeText("Do you want to save your changes?");
msgBox->setStandardButtons(QMessageBox::Save | QMessageBox::Discard |
QMessageBox::Cancel);
msgBox->setDefaultButton(QMessageBox::Save);
int ret = msgBox->exec();
}
Anyone got an idea of what is happening?
任何人都知道发生了什么?
Thanks in advance!
提前致谢!
回答by Jayasankar.K
Try this code.It will help you.
试试这个代码。它会帮助你。
QMessageBox Msgbox;
int sum;
sum = ui->textEdit->toPlainText().toInt()+ ui->textEdit_2->toPlainText().toInt();
Msgbox.setText("sum of numbers are...."+sum);
Msgbox.exec();
回答by Mara Black
Maybe this will help:
也许这会有所帮助:
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Save", "Do you want to save your changes?",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (reply == QMessageBox::Save) {
qDebug() << "Yes was clicked";
// code for saving...
}
if (reply == QMessageBox::Discard)
{
// toDo
}
if(reply == QMessageBox::Cancel)
{
//toDo
}
This code will produce the following:
此代码将产生以下内容:
回答by Muhammad Anjum Kaiser
Try changing this line:
尝试更改此行:
QMessageBox *msgBox = new QMessageBox(this);
to
到
QMessageBox *msgBox = new QMessageBox(0);