Display message box on top of ui in the beginning
-
I want to display a QMessageBox as soon as I run the application. But, I want it to be displayed on top of the ui. For this, I wrote the following code in my mainWindow constructor:
loadJSONData("/path/to/json"); if(!flag) { ui->setupUi(this); QMessageBox msgBox; msgBox.setWindowTitle("Error"); msgBox.setText("The flag is false. Do you still want to continue."); msgBox.setStandardButtons(QMessageBox::Ok); QAbstractButton *okButton = msgBox.button(QMessageBox::Ok); okButton->setText("Exit"); msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint); msgBox.exec(); if(msgBox.clickedButton() == okButton) { QApplication::quit(); return; } } else { ui->setupUi(this); }
When I run the application, I first see the QMessageBox and then on clicking the exit button (in the QMessageBox), the blank ui is visible. Instead, I want that the QMessage box should be displayed on top of a blank ui and the application should quit on clicking the exit button.
Note: We obtain the value of the flag from loadJSONData() function.
-
I want to display a QMessageBox as soon as I run the application. But, I want it to be displayed on top of the ui. For this, I wrote the following code in my mainWindow constructor:
loadJSONData("/path/to/json"); if(!flag) { ui->setupUi(this); QMessageBox msgBox; msgBox.setWindowTitle("Error"); msgBox.setText("The flag is false. Do you still want to continue."); msgBox.setStandardButtons(QMessageBox::Ok); QAbstractButton *okButton = msgBox.button(QMessageBox::Ok); okButton->setText("Exit"); msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint); msgBox.exec(); if(msgBox.clickedButton() == okButton) { QApplication::quit(); return; } } else { ui->setupUi(this); }
When I run the application, I first see the QMessageBox and then on clicking the exit button (in the QMessageBox), the blank ui is visible. Instead, I want that the QMessage box should be displayed on top of a blank ui and the application should quit on clicking the exit button.
Note: We obtain the value of the flag from loadJSONData() function.
@shreya_agrawal Don't create the message box in main window constructor - at that time main window is not fully constructed and is not shown. Do it in your main where you create the main window: create and show main window, then create and show the message box.
-
I want to display a QMessageBox as soon as I run the application. But, I want it to be displayed on top of the ui. For this, I wrote the following code in my mainWindow constructor:
loadJSONData("/path/to/json"); if(!flag) { ui->setupUi(this); QMessageBox msgBox; msgBox.setWindowTitle("Error"); msgBox.setText("The flag is false. Do you still want to continue."); msgBox.setStandardButtons(QMessageBox::Ok); QAbstractButton *okButton = msgBox.button(QMessageBox::Ok); okButton->setText("Exit"); msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint); msgBox.exec(); if(msgBox.clickedButton() == okButton) { QApplication::quit(); return; } } else { ui->setupUi(this); }
When I run the application, I first see the QMessageBox and then on clicking the exit button (in the QMessageBox), the blank ui is visible. Instead, I want that the QMessage box should be displayed on top of a blank ui and the application should quit on clicking the exit button.
Note: We obtain the value of the flag from loadJSONData() function.
Having a button named OK button with text saying "Exit" to quit the app is very confusing...
-
@shreya_agrawal Don't create the message box in main window constructor - at that time main window is not fully constructed and is not shown. Do it in your main where you create the main window: create and show main window, then create and show the message box.
@jsulm
Thank you for your reply!
Yes, it worked by creating it in the main! -
Having a button named OK button with text saying "Exit" to quit the app is very confusing...
@Pl45m4
Yes, you are right. I was just testing the display of the message box for now. Thanks for pointing it out though. -