开发者

Qt: How to display a Messagebox when you are within a function?

I'm developing using the 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.

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.

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(QMessag开发者_JS百科eBox::Save | QMessageBox::Discard | 
                               QMessageBox::Cancel);
    msgBox->setDefaultButton(QMessageBox::Save);
    int ret = msgBox->exec();
}

Anyone got an idea of what is happening?

Thanks in advance!


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();


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:

Qt: How to display a Messagebox when you are within a function?


Try changing this line:

QMessageBox *msgBox = new QMessageBox(this);

to

QMessageBox *msgBox = new QMessageBox(0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜