Important: Please read the Qt Code of Conduct -

Takes 2 clicks of OK button to close Messag Box if Message Box originated from button press

  • If I call that code (MsgBoxCode() below) from say main window, no problem .. if the code is called from within a slot connected to a button press "pB_VerDlg" below then takes 2 clicks EVERY time to close the Message Box

    bool bConnected = (bool) connect(ui->pB_VerDlg, SIGNAL(clicked()),this,SLOT(MsgBoxCode()));
    if (!bConnected) printf("failed to connect clicked() to MsgBoxCode()\n");
    void void MainWindow::MsgBoxCode()
    QMessageBox * msgBox = new QMessageBox;
    msgBox->setText("Some Text");
    msgBox->setGeometry(200, 10, 400, 250);
    msgBox->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");

    [Added code tags ~kshegunov]

  • why not just create the message box on the stack? The you don't need the delete on close it will be deleted when it goes out of scope.

  • Lifetime Qt Champion

    @kenchan He is using show() not exec(). I guess he wants a non modal message box.

  • Lifetime Qt Champion

    @owent Is it possible that the slot is called twice for some reason?

  • Moderators

    @owent my guess would be, that your Messagebox is not the aktive window.
    -> First click makes MessageBox window aktive, aka accept mousevents now
    -> 2nd click gets to the Button

    You can try that, click on somewhere else on the Messagebox that is no button and than click on the Button.

    If it closes, than that is your problem.

  • OK so, since a QMessageBox is a QDialog maybe this item from the QDialog docs is relevant to using the QMessageBox in a modeless manner?
    I think the raise and activateWindow calls might be relevant in this case...

    if (!findDialog) {
            findDialog = new FindDialog(this);
            connect(findDialog, SIGNAL(findNext()), this, SLOT(findNext()));

  • Tried what you suggested J.Hilk ... and yup, you are correct ... needs 1 click to make window active, 2nd click to close. Nothing I have tried (including your suggestion kenchan) fixes it though. Other than call the raise() and activateWindow() is there anything else I should be doing to make this pop-up active? I even tried exec() method and got same results.

  • I found that if I add line :-


    then 2-click issue goes away ... but why? I want it to be modal (message box defaults to modal it appears) so this "fix" does not really help me.

  • if you want a modal dialog you should use the exec() function if the show() functions returns immediately and is for use with non-modal dialogs. that is why you must call setWindowModality() separately.
    The easiest way to message boxes is to use the static functions directly but then ,each to his own :-).

  • The problem is I cannot use the exec() or show() even (without setting modality to nonModal) because for some reason having modalwindow is requiring 2-clicks to close the window. Only way I can have a window that comes up active and allows close on 1 click is to use show() and making the call I mentioned above to set non modal

  • very strange? where are you calling the connect function, in the constructor of the main window?
    If you want it to be modal why not create it on the stack and use the exec() function to put it up? Then it will be modal and get deleted when it is dismissed. I am afraid I am missing your objective with the message box.

  • What I am trying to point out is that if dialog is MODAL then for some weird reason it does NOT WORK in that I have to click (touch screen) OK twice to close the dialog. I have tried msg box defined on stack .. but exec() is by definition modal and so my "need 2 clicks to close OK message box" issue still persists. I am running on linux device not windows btw QT 5.9.

    // Have tried without parent also
    QMessageBox msgBox(this); 
    msgBox.setText("Window Title");
    msgBox.setStyleSheet(QString::fromUtf8("background-color: rgb(0, 75, 141); color: white; "));
    QObject::connect(msgBox.button(QMessageBox::Ok), SIGNAL(pressed()), &msgBox, SLOT(close()));
    msgBox.button(QMessageBox::Ok)->setMinimumSize(QSize(70*HMI_SCALE, 40*HMI_SCALE));
    msgBox.setInformativeText("Test Info");
    // Close box after 30s if left open
    QTimer *mTmr = new QTimer(&msgBox);
    connect(mTmr, SIGNAL(timeout()), &msgBox, SLOT(close()));
    msgBox.exec() ;

  • OK great. So, you are not using the static functions for the message box because you want to style it etc.
    However, with the code you just posted I think the connect is redundant because it will close when the user closes it or the timer fires.
    I tried your code on a mac (not touch though) and did not see the issue you refer to. I will give it a try on a windows machine which has touch later.

    All I can say is you may have found a bug in the touch screen related behaviour on the linux platform, so you could email the interest mailing list or post a bug report about it and see what response you get.

    Good luck

  • OK, in case you are interested...
    I tried the code on my windows machine which has a touch screen and touch pad, it worked fine without the issue you are describing.

    Let's hope someone else with a Linux setup similar to yours can give you some insight into what is going on.

  • @owent Just OOI, you have tried without your code for closing the message box after a delay, haven't you?

  • The 2-click issue disappears if I use clicked() for the button connection instead of pressed() ... debug tells me that the pressed signal is NOT being emitted twice though, so no idea why using pressed() would have this side-effect. The reason I was using pressed() instead of clicked() was because the clicked() was not responsive, as though focus was being lost between the pressed and release actions. Maybe I have an issue with the touch screen itself ..

  • Moderators


    copied from the source code:

    \o pressed() is emitted when the left mouse button is pressed while
        the mouse cursor is inside the button.
        \o released() is emitted when the left mouse button is released.
        \o clicked() is emitted when the button is first pressed and then
        released, when the shortcut key is typed, or when click() or
        animateClick() is called.

    -> pressed can only be emited by the left mousebutton event, whereas clicked can also depend on click(), and IIRC that one gets also checked when the focus changes.

Log in to reply