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. best way to tune behavior of a QPushButton?

best way to tune behavior of a QPushButton?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.0k Views 4 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi, all -

    Very basic question here, I'm sure. I'm trying to learn Qt through some of the online examples, and am currently here.

    So, the exercise is to enable the two buttons with distinct behavior. I can think of three ways of doing this, but I'm not in love with any of them:

    • create subclasses for both buttons. Seems like overkill.
    • within the slotButtonClicked() method, examine the return from text() and act accordingly. Seems kind of clunky.
    • create a new constructor with an additional parameter to point to the routine to invoke when triggered. Seems unduly complex.

    So, are these the choices, or am I unaware of another (hopefully better) way of doing this? I welcome any recommendations.

    Thanks.

    FlotisableF 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi, all -

      Very basic question here, I'm sure. I'm trying to learn Qt through some of the online examples, and am currently here.

      So, the exercise is to enable the two buttons with distinct behavior. I can think of three ways of doing this, but I'm not in love with any of them:

      • create subclasses for both buttons. Seems like overkill.
      • within the slotButtonClicked() method, examine the return from text() and act accordingly. Seems kind of clunky.
      • create a new constructor with an additional parameter to point to the routine to invoke when triggered. Seems unduly complex.

      So, are these the choices, or am I unaware of another (hopefully better) way of doing this? I welcome any recommendations.

      Thanks.

      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @mzimmers
      connect the buttons to specific slots

      ex. connect buttonInfo clicked signal to app aboutQt() slot and connect buttonQuit clicked signal to app quit() slot

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        Here's the example I mentioned:

        #include <QApplication>
        #include <QPushButton>
         
        int main(int argc, char **argv)
        {
         QApplication app (argc, argv);
         
        QWidget window;
         window.setFixedSize(100, 80);
         
        QPushButton *buttonInfo = new QPushButton("Info", &window);
         buttonInfo->setGeometry(10, 10, 80, 30);
         
        QPushButton *buttonQuit = new QPushButton("Quit", &window);
         buttonQuit->setGeometry(10, 40, 80, 30);
         
        window.show();
         
        // Add your code here
         
        return app.exec();
        }
        

        These buttons aren't defined within the context of an object -- how can I connect them to anything?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          QObject::connect(buttonQuit, &QPushButton::clicked, qApp, &QApplication::quit);
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            Thanks, RGaist. So...is "connect" an actual function, then? I thought it was something for the moc to use.

            I assume that you meant "app" where you said "qApp".

            I'm not clear on the syntax here: shouldn't the 2nd and 4th arguments be functions, and therefore look like "QPushButton::clicked()"?

            Thanks again.

            ? 1 Reply Last reply
            0
            • mzimmersM mzimmers

              Thanks, RGaist. So...is "connect" an actual function, then? I thought it was something for the moc to use.

              I assume that you meant "app" where you said "qApp".

              I'm not clear on the syntax here: shouldn't the 2nd and 4th arguments be functions, and therefore look like "QPushButton::clicked()"?

              Thanks again.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @mzimmers Hi! For the signals and slots syntax that was introduced with Qt 5, see this wiki article.

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                connect is a static method of QObject, nothing to do with moc itself.

                This example uses this overload that takes point to member function has parameter for the signal and the slot.

                No no, I meant qApp which is a pointer to the unique application object created with app. You can replace it with &app in this context.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

                  Wieland: thanks for that link. That new syntax does appear preferable, and is about as intuitive as C++ will let it be.

                  SGaist: I should have known you wouldn't have an error in one of your responses! Thanks for the explanation.

                  As an aside, I realize this is just an example, but is it considered good form to program connects outside of an object? I seem to remember reading somewhere that they were intended to be used by and for objects...

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #9

                    Don't be so sure, I'm only human and I can make mistakes in my answers :)

                    Usually you use connect in QObject based classes however it can happen that you need to connect stuff in your main code.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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