PDA

View Full Version : Need don't show this message again check box in QMessageBox with YesNo button



Rajesh.Rathod
25th June 2014, 07:15
Dear All,
I want to have "Don't show this message again" check box in QMessageBox with Yes & No buttons , is there a way in Qt to achieve it ?
I know there is QErrorMessage which gives this check box but I also need yes no button.

Please help.

Thank you,
With Best Regards,

Ginsengelf
25th June 2014, 08:18
Hi, ugly hack:
you can derive a class from QMessageBox, and if you look at the Qt sources you can see that QMessageBox uses a QGridLayout internally (at least in Qt4). You can get this using layout (), and then add a widget like a checkbox to it.

This depends on Qt internals, so maybe/probably there is a better way to do it.

Ginsengelf

edit: of course you could just create a custom dialog for this...

stampede
25th June 2014, 08:18
Since Qt 5.2, QMessageBox supports setting custom check box with QMessageBox::setCheckBox (http://qt-project.org/doc/qt-5/qmessagebox.html#setCheckBox) method. If you are using older version, you will have to create a custom widget yourself.

Rajesh.Rathod
25th June 2014, 08:56
Dear All,
Thank you very much for you help, I am using Qt 4.7.3 so can't use the latest API.

Actaully, I found a workaround here...


QMessageBox msgBox;
QCheckBox dontShowCheckBox("don't show this message again");
msgBox.addButton(&dontShowCheckBox, QMessageBox::ActionRole);

This seems to work for me, of course I need to connect signal slot and then I need to maintain it's checked state somewhere in my application....

Thank you.

Rajesh.Rathod
26th June 2014, 09:23
Dear All,
I have managed to show "Don't show message again" check box on QMessageBox in Qt 4.7.3 but now problem is the moment I clicks on check box it terminates message box and I don't want it.
I wanted to mark the check box only, please help if anyone has any idea.
Here is the sample code.


QMessageBox msgBox;
msgBox.setText(WMC_ASSIGNMENT_OVERWRITE_MSG);
msgBox.setIcon(QMessageBox::Warning);
QCheckBox dontShowCheckBox("don't show this message again");
dontShowCheckBox.setCheckable(true);
msgBox.addButton(&dontShowCheckBox, QMessageBox::ApplyRole);
msgBox.addButton(QMessageBox::Ok);
msgBox.addButton(QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
int32_t userReply = msgBox.exec();

Added after 1 8 minutes:

Dear All,
Finally, I managed to get what I wanted and here is the sample code of QMessageBox with "Don't show this message box again" check box in Qt 4.7.3.


QMessageBox msgBox;
msgBox.setText("Sample message text");
msgBox.setIcon(QMessageBox::Warning);
msgBox.addButton(QMessageBox::Ok);
msgBox.addButton(QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
QCheckBox dontShowCheckBox("don't show this message again");
dontShowCheckBox.blockSignals(true);
msgBox.addButton(&dontShowCheckBox, QMessageBox::ResetRole);
int32_t userReply = msgBox.exec();
if(userReply == QMessageBox::Ok)
{
if(dontShowCheckBox.checkState() == Qt::Checked)
{
//Write function to handle this use case.
}
}

Thank you.

Wadashe
26th June 2014, 13:03
Why not just create your own dialog and display it as a message box (instead of using QMessageBox)?

Rajesh.Rathod
26th June 2014, 14:43
@Wadashe : According to me this is small and reliable solution compare to writing complete class for displaying message box, isn't it?