14

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!

Andrew Prock
  • 6,900
  • 6
  • 40
  • 60
Ikky
  • 2,826
  • 14
  • 47
  • 68

3 Answers3

14

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();
Rob W
  • 341,306
  • 83
  • 791
  • 678
Jayasankar.K
  • 141
  • 1
  • 3
6

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:

result

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Mara Black
  • 1,666
  • 18
  • 23
1

Try changing this line:

QMessageBox *msgBox = new QMessageBox(this);

to

QMessageBox *msgBox = new QMessageBox(0);
Muhammad Anjum Kaiser
  • 3,947
  • 1
  • 18
  • 18
  • 2
    Not setting a parent is never a good idea as it breaks the window stacking (how odd the consequences of that are depends on the platform). It's good practice too always pass a parent. – Frank Osterfeld Nov 07 '11 at 19:02
  • 2
    Plus, you don't get the QObject hierarchy-based memory management (so it leaks unless you manually delete). – Tamás Szelei Jul 14 '12 at 17:52