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

I am developing using the Qt Nokia SDK.

I am having trouble displaying MessageBox buttons when trying to display a message box inside a function. If I try to display it in the main window, there is no problem displaying the buttons.

The main window consists of a QStackWidget, which contains different widgets.

Here is the code that works in the 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 (a message box is displayed, but not a button.

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

Has anyone understood what is going on?

Thanks in advance!

+4
source share
3 answers

Try this code. This 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(); 
+7
source

Perhaps 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 call the following:

+3
source

Try changing this line:

 QMessageBox *msgBox = new QMessageBox(this); 

to

 QMessageBox *msgBox = new QMessageBox(0); 
0
source

Source: https://habr.com/ru/post/1343021/


All Articles