One interesting way to get close to it, which worked for me, is to completely turn off the signals for the created target button, and then add the intended functionality again. This will not work for everyone, especially if the button is not created in this way and / or you still want to close the dialog box correctly. (Perhaps there is a way to add it back and / or simulate the behavior using QDialog::accept , QDialog::reject , QDialog::done - have not tried it yet.)
Example:
QMessageBox *msgBox = new QMessageBox(this); QAbstractButton *doNotCloseButton = msgBox->addButton(tr("This button will not close anything"), QMessageBox::ActionRole); // Disconnect all events - this will prevent the button from closing the dialog doNotCloseButton->disconnect(); connect(doNotCloseButton, &QAbstractButton::clicked, this, [=](){ doNotCloseButton->setText("See? Still open!"); });
source share