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. QPushButton onClick without explicit connect-call
Forum Updated to NodeBB v4.3 + New Features

QPushButton onClick without explicit connect-call

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.5k Views
  • 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.
  • T Offline
    T Offline
    Torsten S.
    wrote on last edited by Torsten S.
    #1

    Hi,

    I am working on an existing QT application und I try to understand something:

    The application has a QPushButton and when it is clicked, a function called void MainWindow::on_sendCommandPushButton_clicked() is called. What I don't understand is that there is NO connect(...) call anywhere in the code. I thought I would have to connect the click-signal to the function for it to work.

    My current guess is that there is an implicit connect somewhere, the buttons name is sendCommandPushButton and if there is a function on_<name>_clicked it will get called without an explicit connect-call. Is this correct?

    Unfortunately I did not find anything browsing the documentation.

    And I can rename the function (both declaration and implementation) and it still compiles and the renamed function is not called when the button is clicked. If there were a connect-call I missed in the code, I would expect to get a compile error.

    Can anyone shed light on this issue?

    Cheers,
    Torsten

    matthew.kuiashM 1 Reply Last reply
    0
    • T Torsten S.

      Hi,

      I am working on an existing QT application und I try to understand something:

      The application has a QPushButton and when it is clicked, a function called void MainWindow::on_sendCommandPushButton_clicked() is called. What I don't understand is that there is NO connect(...) call anywhere in the code. I thought I would have to connect the click-signal to the function for it to work.

      My current guess is that there is an implicit connect somewhere, the buttons name is sendCommandPushButton and if there is a function on_<name>_clicked it will get called without an explicit connect-call. Is this correct?

      Unfortunately I did not find anything browsing the documentation.

      And I can rename the function (both declaration and implementation) and it still compiles and the renamed function is not called when the button is clicked. If there were a connect-call I missed in the code, I would expect to get a compile error.

      Can anyone shed light on this issue?

      Cheers,
      Torsten

      matthew.kuiashM Offline
      matthew.kuiashM Offline
      matthew.kuiash
      wrote on last edited by matthew.kuiash
      #2

      @Torsten-S. IIRC this is handled by the code generated by the form compiler. It uses QMetaObject to introspect the objects involved and binds signals to slots in exactly the way you describe. That is "on_" + ui_object_name + "_" + signal_name is bound for you.

      Just to clarify. The QPushButton is in a UI form right? That is in you MainWindow ctor your have something like "ui->setupUi(this);".

      The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

      T 1 Reply Last reply
      1
      • matthew.kuiashM matthew.kuiash

        @Torsten-S. IIRC this is handled by the code generated by the form compiler. It uses QMetaObject to introspect the objects involved and binds signals to slots in exactly the way you describe. That is "on_" + ui_object_name + "_" + signal_name is bound for you.

        Just to clarify. The QPushButton is in a UI form right? That is in you MainWindow ctor your have something like "ui->setupUi(this);".

        T Offline
        T Offline
        Torsten S.
        wrote on last edited by
        #3

        Hi Matthew,

        @matthew.kuiash said in QPushButton onClick without explicit connect-call:

        @Torsten-S. IIRC this is handled by the UI loader. I can introspect the object doing the loading and binds signals to slots in exactly the way you describe. That is "on_" + ui_object_name + "_" + signal_name is bound for you.

        Just to clarify. The QPushButton is in a UI form right? That is in you MainWindow ctor your have something like "ui->setupUi(this);".

        Yes, that is correct. The QPushButton is in a UI form and there is a ui->setupUi(this) in the MainWindw-class.

        So this is expected behaviour?! It is just that I have not found any documentation on this. Everything I read emphasizes to use connect(...) to connect to the signals.

        Thanks,
        Torsten

        matthew.kuiashM 1 Reply Last reply
        0
        • T Torsten S.

          Hi Matthew,

          @matthew.kuiash said in QPushButton onClick without explicit connect-call:

          @Torsten-S. IIRC this is handled by the UI loader. I can introspect the object doing the loading and binds signals to slots in exactly the way you describe. That is "on_" + ui_object_name + "_" + signal_name is bound for you.

          Just to clarify. The QPushButton is in a UI form right? That is in you MainWindow ctor your have something like "ui->setupUi(this);".

          Yes, that is correct. The QPushButton is in a UI form and there is a ui->setupUi(this) in the MainWindw-class.

          So this is expected behaviour?! It is just that I have not found any documentation on this. Everything I read emphasizes to use connect(...) to connect to the signals.

          Thanks,
          Torsten

          matthew.kuiashM Offline
          matthew.kuiashM Offline
          matthew.kuiash
          wrote on last edited by matthew.kuiash
          #4

          @Torsten-S. OK, I think the magic function is this one http://doc.qt.io/qt-5/qmetaobject.html#connectSlotsByName

          Very cool. Look in the ui_MainWindow.h generated by the form compiler and you'll find that lots of UI objects are created and parented to MainWindow then the above function binds everything for you.

          The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

          1 Reply Last reply
          3
          • T Offline
            T Offline
            Torsten S.
            wrote on last edited by
            #5

            Great. Thanks for the quick clarification. I am going to read more about this. One less point of confusion :)

            Cheers,
            Torsten

            matthew.kuiashM 1 Reply Last reply
            0
            • T Torsten S.

              Great. Thanks for the quick clarification. I am going to read more about this. One less point of confusion :)

              Cheers,
              Torsten

              matthew.kuiashM Offline
              matthew.kuiashM Offline
              matthew.kuiash
              wrote on last edited by
              #6

              @Torsten-S. No problem. Have a nice day!

              The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

              1 Reply Last reply
              1

              • Login

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