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. Using MessageBox buttonclicked signal
Forum Updated to NodeBB v4.3 + New Features

Using MessageBox buttonclicked signal

Scheduled Pinned Locked Moved Solved General and Desktop
qmessageboxsignal&slot
6 Posts 4 Posters 5.1k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I want to use the signal emitted by a messageBox to close a QDialog.

    I have the following code:

     QMessageBox::information (this, tr("Confirmation"), tr("<b><font size = '16' color = 'green'>The Friend was added to the database."));
     connect(QMessageBox,SIGNAL(buttonClicked(Ok)),this,SLOT(reject()));
    

    I get the following error message:
    C:\Programming\Projects\Folkfriends_1\additem.cpp:82: error: expected primary-expression before ',' token
    connect(QMessageBox,SIGNAL(buttonClicked(Ok)),this,SLOT(reject()));
    ^
    Please help me to figure out what's wrong.
    Thank you.

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Signals/slots don't work this way. You can only connect signals/slots using instances not just classes like you're trying. You have to instantiate QMessageBox:

      QMessageBox msgBox(this, tr("Confirmation"), tr("<b><font size = '16' color = 'green'>The Friend was added to the database."));
      // What is Ok in your example?
       connect(msgBox,SIGNAL(buttonClicked()),this,SLOT(reject()));
      msg.exec();
      

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

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Thank you.
        I tried this way:

        connect(&msgBox, SIGNAL(buttonClicked()),this,SLOT(reject()));
        

        and I got the following error message:
        take the address of the argument with &

        So I changed it for this:

        connect(&msgBox, SIGNAL(buttonClicked()),this,SLOT(reject()));
        

        This way there is no error message, but it doesn't do anything. Please help me to figure what else to add. Thank you.

        mrjjM 1 Reply Last reply
        0
        • G gabor53

          Thank you.
          I tried this way:

          connect(&msgBox, SIGNAL(buttonClicked()),this,SLOT(reject()));
          

          and I got the following error message:
          take the address of the argument with &

          So I changed it for this:

          connect(&msgBox, SIGNAL(buttonClicked()),this,SLOT(reject()));
          

          This way there is no error message, but it doesn't do anything. Please help me to figure what else to add. Thank you.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @gabor53
          hi
          is it not
          void QMessageBox::buttonClicked(QAbstractButton * button)

          so try

          qDebug() << connect(&msgBox, SIGNAL(buttonClicked(QAbstractButton *)),this,SLOT(MYreject(QAbstractButton *)));

          void thedialog::Myreject(QAbstractButton *) {
          reject();
          }

          1 Reply Last reply
          1
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            This is a very contrived way of using a message box.
            The way it's meant to be used is something like:

            //taken out just to shorten the lines
            QString title = tr("Confirmation");
            QString msg = tr("<b><font size = '16' color = 'green'>The Friend was added to the database.");
            
            if (QMessageBox::information (this, title, msg) == QMessageBox::Ok)
               reject();
            

            ...although it's a little weird that you call reject() when user confirmed something.

            1 Reply Last reply
            5
            • G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              Thank you.

              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