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

Ikky picture Ikky · Mar 9, 2011 · Viewed 64.8k times · Source

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(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!

Answer

Jayasankar.K picture Jayasankar.K · Nov 7, 2011

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