Solved How to add button to QMessageBox that emits pressed() signal
-
How do I add button to QMessageBox that emits pressed() signal
-
QPushButton* myButton =new QPushButton("Click ME!"); msgBox.addButton(myButton ,QMessageBox::AcceptRole); QObject::connect(myButton,&QPushButton::pressed,[](){qDebug("They pressed me!");});
or use a default one:
QMessageBox msgBox(QMessageBox::Information,"Message","I'm a message box",QMessageBox::Ok); QObject::connect(msgBox.button(QMessageBox::Ok),&QAbstractButton::pressed,[](){qDebug("They pressed ok!");});
-
Thank you. Tried this and I see the debug so I know the connection definition took when I clicked the ok button. However being new to QT I was surprised that the window closed .. it did not always close to coincide with debug but if I kept pressing ok it did eventually close.
Reason I want to emit pressed() is to attempt to make the ok button more receptive .. figured using pressed() instead of clicked() might do trick, it did with other stand-alone push buttons I have defined , those inside messageboxes still slow ... so based on your answer I thought I could maybe do this
QObject::connect(myButton,&QPushButton::pressed,msgBox{msgBox->close();}); But alas no quicker. -
@owent said in How to add button to QMessageBox that emits pressed() signal:
Reason I want to emit pressed() is to attempt to make the ok button more receptive
Not sure what you mean here
-
The clicked() (ie presssed&release) not working well .. may be touchscreen calibration/ADD issue so that release not properly matched to the press) .. in mean time using pressed() .. works better .. not in messagebox case tho so far
-
Using the code you kindly supplied, I have to press ok button twice for window to close ... 1st press results in window staying open, no debug. 2nd press results in window closing and debug appears. If I do not use your code and revert back to default button which would use clicked() signal as default (ie do not add my own button and connect) takes many clicks for window to close.
QMessageBox * msgBox = new QMessageBox;
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setWindowTitle("");
msgBox->setText("Some Text");
msgBox->setFont(fontBold);
msgBox->setGeometry(200, 10, 400, 250);
msgBox->resize(400,250);#if 1 // use this to revert back to default button
QPushButton* myButton = new QPushButton("Ok");
msgBox->addButton(myButton ,QMessageBox::AcceptRole);
QObject::connect(myButton,&QPushButton::pressed,{printf("==== Ok Pressed =====\n");});
#endifmsgBox->setStyleSheet(QString::fromUtf8(("QPushButton{ width:200px; height:50px; background-color: rgb(0, 75, 141); color: white;} QMessageBox{ background-color: rgb(0, 75, 141); color: white; } ")));
msgBox->setInformativeText("Important stuff");
msgBox->show(); -
To add more information, If I call that code (MsgBoxCode()) from say main window, no problem .. the "takes 2-click issue" arises only if the code is called from within a slot connected to a button press
bool bConnected = (bool) connect(ui->pB_VerDlg, SIGNAL(clicked()),this,SLOT(MsgBoxCode()));
if (!bConnected) printf("failed to connect clicked() to MsgBoxCode()\n"); -
@owent said in How to add button to QMessageBox that emits pressed() signal:
bool bConnected = (bool) connect(ui->pB_VerDlg, SIGNAL(clicked()),this,SLOT(MsgBoxCode()));
Why do you cast the result of connect() to bool? connect() returns QMetaObject::Connection which has operator_bool(), so compiler will cast to bool automatically (also avoid using C style cast in C++).
As I asked in your duplicate post: is it possible that the slot is called twice? Try to add Qt::UniqueConnection to connect. -
Going to mark this topic solved as VRonin did give the solution to original question. Have opened separate topic for follow up question.