Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add button to QMessageBox that emits pressed() signal
Forum Updated to NodeBB v4.3 + New Features

How to add button to QMessageBox that emits pressed() signal

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    owent
    wrote on last edited by
    #1

    How do I add button to QMessageBox that emits pressed() signal

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      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!");});
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • O Offline
        O Offline
        owent
        wrote on last edited by
        #3

        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.

        VRoninV 1 Reply Last reply
        0
        • O owent

          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.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • O Offline
            O Offline
            owent
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • O Offline
              O Offline
              owent
              wrote on last edited by
              #6

              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");});
              #endif

              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");
              msgBox->show();

              1 Reply Last reply
              0
              • O Offline
                O Offline
                owent
                wrote on last edited by
                #7

                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");

                jsulmJ 1 Reply Last reply
                0
                • O owent

                  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");

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • O Offline
                    O Offline
                    owent
                    wrote on last edited by
                    #9

                    Going to mark this topic solved as VRonin did give the solution to original question. Have opened separate topic for follow up question.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved