"Don't show this meesage again" checkbox in QMessagebox
-
Hello,
I want to have a checkbox saying "Don't show this message again" like shown below in a QMessageBox -->
Is it possible to do so? If yes, then how?
-
Create a small own dialog.
-
Create a small own dialog.
@Christian-Ehrlicher I have created a message box and this check box is appearing in it. Now I want to know how this message box will not appear again after clicking into this checkbox.
this is my code -->
msg = QtWidgets.QMessageBox()
msg.setWindowTitle("What's New")
msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
os.path.realpath(file)), 'gui_images', 'logo_icon.png'))))
msg.setText(ui_helper.WHAT_IS_NEW)
check_box = QtWidgets.QCheckBox("Don't show this again")
msg.setCheckBox(check_box)
msg.exec_() -
@Christian-Ehrlicher I have created a message box and this check box is appearing in it. Now I want to know how this message box will not appear again after clicking into this checkbox.
this is my code -->
msg = QtWidgets.QMessageBox()
msg.setWindowTitle("What's New")
msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
os.path.realpath(file)), 'gui_images', 'logo_icon.png'))))
msg.setText(ui_helper.WHAT_IS_NEW)
check_box = QtWidgets.QCheckBox("Don't show this again")
msg.setCheckBox(check_box)
msg.exec_()@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
how this message box will not appear again after clicking into this checkbox
Well, you need to store the state of that checkbox in a variable and next time you want to show the dialog you check this variable to decide whether you need to show the dialog or not...
-
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
how this message box will not appear again after clicking into this checkbox
Well, you need to store the state of that checkbox in a variable and next time you want to show the dialog you check this variable to decide whether you need to show the dialog or not...
@jsulm Ok understood. I have written something like this -->
msg = QtWidgets.QMessageBox()
msg.setWindowTitle("What's New")
msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
os.path.realpath(file)), 'gui_images', 'logo_icon.png'))))
msg.setText(ui_helper.WHAT_IS_NEW)
check_box = QtWidgets.QCheckBox("Don't show this again")
msg.setCheckBox(check_box)
check_box.blockSignals(True)
msg.addButton(check_box, QtWidgets.QMessageBox.ResetRole)
msg.addButton(QtWidgets.QMessageBox.Ok)
user_reply = msg.exec_()
if user_reply == QtWidgets.QMessageBox.Ok:
if check_box.checkState() == QtCore.Qt.Checked:
pass
Is it correct? Because the message box is appearing again when i launch my gui. It should not appear again. -
@jsulm Ok understood. I have written something like this -->
msg = QtWidgets.QMessageBox()
msg.setWindowTitle("What's New")
msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
os.path.realpath(file)), 'gui_images', 'logo_icon.png'))))
msg.setText(ui_helper.WHAT_IS_NEW)
check_box = QtWidgets.QCheckBox("Don't show this again")
msg.setCheckBox(check_box)
check_box.blockSignals(True)
msg.addButton(check_box, QtWidgets.QMessageBox.ResetRole)
msg.addButton(QtWidgets.QMessageBox.Ok)
user_reply = msg.exec_()
if user_reply == QtWidgets.QMessageBox.Ok:
if check_box.checkState() == QtCore.Qt.Checked:
pass
Is it correct? Because the message box is appearing again when i launch my gui. It should not appear again.@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
Is it correct?
Apparently it is not.
You are not doing what I suggested, which is actually easy... -
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
Is it correct?
Apparently it is not.
You are not doing what I suggested, which is actually easy...@jsulm Ok,
state = check_box.checkState()
if state == Qt.Checked:
passNow is it ok or am I still missing something? because it is appearing again..
-
@jsulm Ok,
state = check_box.checkState()
if state == Qt.Checked:
passNow is it ok or am I still missing something? because it is appearing again..
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
if state == Qt.Checked:
passWhat do you want to achieve with this useless code?
Don't you think you should also check the stored state of the check box BEFORE showing ther dialog? As I suggested? -
@jsulm Ok,
state = check_box.checkState()
if state == Qt.Checked:
passNow is it ok or am I still missing something? because it is appearing again..
@Prathamesh
And in addition to @jsulm's comment: if you want to know the user previously checked the box so that you don't show the dialog again, you are going to have save somewhere in persistent state that the user previously checked that box. You can't get that from the dialog previously, because you create a new dialog each time. -
@Prathamesh
And in addition to @jsulm's comment: if you want to know the user previously checked the box so that you don't show the dialog again, you are going to have save somewhere in persistent state that the user previously checked that box. You can't get that from the dialog previously, because you create a new dialog each time.@JonB Checked all the methods given in documentation, tried to store checkbox status in a variable. But same thing is happening.
-
@JonB Checked all the methods given in documentation, tried to store checkbox status in a variable. But same thing is happening.
@Prathamesh It's your code then.
-
Here's one way to do something like this:
int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setOrganizationName("MyCompany"); QCoreApplication::setOrganizationDomain("MyApplication"); QApplication app(argc, argv); QSettings s; QMessageBox msgB(QMessageBox::Critical,"One TimeBox", "This MessageBox, when confirmed will never be shown again"); msgB.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); QTimer t; t.setSingleShot(true); if(!s.value("OneTimeOK",false).toBool()){ QObject::connect(&t, &QTimer::timeout,&msgB, &QMessageBox::exec); QObject::connect(&msgB, &QMessageBox::finished, [&s,&t, &app](int result){ qDebug() << "Finished" << result; if(result == QMessageBox::Ok){ s.setValue("OneTimeOK", true); app.quit(); } else { t.start(1000); } }); msgB.exec(); } else { qDebug() << "Nothing to show"; return -1; } return app.exec(); }
-
@Prathamesh It's your code then.
def __gui_initial_state(self): """ This method initializes the gui state. """ self.initial_state.emit() self.__update_recent_configurations() version_str, version_tuple = global_vars.check_latest_version() if global_vars.LOCAL_VERSION_TUPLE >= version_tuple: msg = QtWidgets.QMessageBox() msg.setWindowTitle("What's New") msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname( os.path.realpath(__file__)), 'gui_images', 'logo_icon.png')))) msg.setText(ui_helper.WHAT_IS_NEW) check_box = QtWidgets.QCheckBox("Don't show this again") msg.setCheckBox(check_box) msg.exec_() check_box_status = check_box.isChecked() if check_box_status: print("abcd")
This method executes everytime when a gui in launched. My task is to check whether new version of framework is launched or not, and if launched user will download it. After downloading my code will check if it is downloaded and then a pop-up containing all the information about new version will come up.
So,this pop-up will appear first time when user downloads new version, and then in this pop-up i have to put a checkbox. So I have done that,and using ischecked() method its status is getting stored in a variable and as i have written print("abcd") if status is true. So it is working fine.
But the problem is, everytime a gui is launched this pop-up will come up,so even after storing True status from previos run ,it is appearing. I am not understanding how to handle this situation.
-
def __gui_initial_state(self): """ This method initializes the gui state. """ self.initial_state.emit() self.__update_recent_configurations() version_str, version_tuple = global_vars.check_latest_version() if global_vars.LOCAL_VERSION_TUPLE >= version_tuple: msg = QtWidgets.QMessageBox() msg.setWindowTitle("What's New") msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname( os.path.realpath(__file__)), 'gui_images', 'logo_icon.png')))) msg.setText(ui_helper.WHAT_IS_NEW) check_box = QtWidgets.QCheckBox("Don't show this again") msg.setCheckBox(check_box) msg.exec_() check_box_status = check_box.isChecked() if check_box_status: print("abcd")
This method executes everytime when a gui in launched. My task is to check whether new version of framework is launched or not, and if launched user will download it. After downloading my code will check if it is downloaded and then a pop-up containing all the information about new version will come up.
So,this pop-up will appear first time when user downloads new version, and then in this pop-up i have to put a checkbox. So I have done that,and using ischecked() method its status is getting stored in a variable and as i have written print("abcd") if status is true. So it is working fine.
But the problem is, everytime a gui is launched this pop-up will come up,so even after storing True status from previos run ,it is appearing. I am not understanding how to handle this situation.
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
But the problem is, everytime a gui is launched this pop-up will come up,so even after storing True status from previos run
Of course, you are still not doing what was suggested!
def __gui_initial_state(self): """ This method initializes the gui state. """ self.initial_state.emit() self.__update_recent_configurations() version_str, version_tuple = global_vars.check_latest_version() if global_vars.LOCAL_VERSION_TUPLE >= version_tuple and not self.__check_box_status: msg = QtWidgets.QMessageBox() msg.setWindowTitle("What's New") msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname( os.path.realpath(__file__)), 'gui_images', 'logo_icon.png')))) msg.setText(ui_helper.WHAT_IS_NEW) check_box = QtWidgets.QCheckBox("Don't show this again") msg.setCheckBox(check_box) msg.exec_() self.__check_box_status = check_box.isChecked() if self.__check_box_status: print("abcd")
-
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
But the problem is, everytime a gui is launched this pop-up will come up,so even after storing True status from previos run
Of course, you are still not doing what was suggested!
def __gui_initial_state(self): """ This method initializes the gui state. """ self.initial_state.emit() self.__update_recent_configurations() version_str, version_tuple = global_vars.check_latest_version() if global_vars.LOCAL_VERSION_TUPLE >= version_tuple and not self.__check_box_status: msg = QtWidgets.QMessageBox() msg.setWindowTitle("What's New") msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) msg.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname( os.path.realpath(__file__)), 'gui_images', 'logo_icon.png')))) msg.setText(ui_helper.WHAT_IS_NEW) check_box = QtWidgets.QCheckBox("Don't show this again") msg.setCheckBox(check_box) msg.exec_() self.__check_box_status = check_box.isChecked() if self.__check_box_status: print("abcd")
@jsulm I did half part of storing status.But I am not getting how to check it while launching a gui next time? That is where I am stuck. Any suggesstion please?
-
@jsulm I did half part of storing status.But I am not getting how to check it while launching a gui next time? That is where I am stuck. Any suggesstion please?
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
how to check it while launching a gui next time?
What exactly do you mean by that? Do you mean you close the app and then start it again and you want to check this check status? If so, then use QSettings to store such information permanently.
-
@Prathamesh said in "Don't show this meesage again" checkbox in QMessagebox:
how to check it while launching a gui next time?
What exactly do you mean by that? Do you mean you close the app and then start it again and you want to check this check status? If so, then use QSettings to store such information permanently.
@jsulm yeah this is what I mean exactly. Once I tick a checkbox and close an app then pop-up should not appear until new version of framework is launched.
-
@jsulm yeah this is what I mean exactly. Once I tick a checkbox and close an app then pop-up should not appear until new version of framework is launched.
-
@jsulm yeah this is what I mean exactly. Once I tick a checkbox and close an app then pop-up should not appear until new version of framework is launched.
Then you need to store something like
QSettings
.
So that your app will know what your settings from your last run are. -
@jsulm Thanks for this. I have tried this just now. I have written something like this
--> msg = QtWidgets.QMessageBox()
msg.setWindowTitle("What's New")
msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
msg.setText(ui_helper.WHAT_IS_NEW)
check_box = QtWidgets.QCheckBox("Don't show this again")
msg.setCheckBox(check_box)
msg.exec_()
check_box_status = check_box.isChecked()
settings = QtCore.QSettings()
settings.setValue(check_box_status, True)I know this is not the correct way of doing and that's why I am getting an error --> setValue has unexpected type 'bool'. But then how do I pass the status of checkbox into this function?