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. Custom Class findChild<QPushButton *>(); Not Finding Child
QtWS25 Last Chance

Custom Class findChild<QPushButton *>(); Not Finding Child

Scheduled Pinned Locked Moved Solved General and Desktop
findchildconnect probleminvalidnull parameter
13 Posts 6 Posters 4.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.
  • B Burke212
    25 Feb 2019, 19:42

    @mrjj

    When I change it to your suggestion I get the error: 'makeCallDialog' does not refer to a value

    But I did not setObjectName. Making this change solve my problem. Thank you!

    How is the object name different from the variable name? I assumed that whatever I name a variable is its object name.

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 25 Feb 2019, 22:38 last edited by mrjj
    #4

    @Burke212 said in Custom Class findChild<QPushButton *>(); Not Finding Child:

    How is the object name different from the variable name? I assumed that whatever I name a variable is its object name.

    Hi
    the variable name is a local thing you use to store a pointer to the object in.
    FindChild has no way of knowing such names.
    The objectName however, is a property of the object, much like its size and color and
    Qt/FindChild know of this pr design.
    And just like its size, its part of the object. The variable name , you use is not part of the object and
    therefore not related to the variable name at all.

    1 Reply Last reply
    4
    • B Burke212
      25 Feb 2019, 19:42

      @mrjj

      When I change it to your suggestion I get the error: 'makeCallDialog' does not refer to a value

      But I did not setObjectName. Making this change solve my problem. Thank you!

      How is the object name different from the variable name? I assumed that whatever I name a variable is its object name.

      P Offline
      P Offline
      Pl45m4
      wrote on 26 Feb 2019, 00:48 last edited by Pl45m4
      #5

      @Burke212

      Hi,

      I think the easiest way would be, if you use an iterator to go through your QList of QPushButtons and connect the current QPushButton while iterating. You can also rename your Buttons during this process.

      EDIT: Where do you create your Buttons?

      QPushButton *num[10];
      

      Does not create an array of 10 (usable) PushButton-Objects. You define 10 pointers to a QPushButton there, but no QPushButton is initialized.
      Better:

      QPushButton myBtn; // Calls Std-Constructor of QPushButton
      // OR
      QPushButton *myBtn = new QPushButton ();
      

      I dont get the use of findChild there... If you are trying to "find" the buttons, that were created somewhere else (in parent class), to connect them, why dont you connect them directly in your parent class? (something like sending a signal to notify your Dialog that "Button Nr. X" was pressed / released)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      B 1 Reply Last reply 26 Feb 2019, 12:52
      1
      • B Burke212
        25 Feb 2019, 18:16
        QPushButton *numButtons[15];
            for(int i = 0; i<11; i++){
                QString buttonName = "Button" + QString::number(i);
                numButtons[i] = makeCallDialog::findChild<QPushButton *>(buttonName);
                qDebug() << "Button: " << numButtons[i];
                qDebug() << buttonName;
                connect(numButtons[i], &QPushButton::released, this, &makeCallDialog::numPressed);
            }
        

        I have 10 QPushButtons (Button0, Button1, ..., Button9). I am trying to connect them via this for() loop, but Connect() returns with invalid null parameter. I have determined that the findChild<...> code isn't finding the element/ child. Why would this happen?

        makeCallDialog is my class name. Also, I am not using the Designer to create the UI.

        J Offline
        J Offline
        Jerry.Wilson
        wrote on 26 Feb 2019, 02:35 last edited by
        #6

        @Burke212

        makeCallDialog is a Class Name, not an object. When you call findChild function, you need an object. like that:

        makeCallDialog dlg;
        dlg.findChild();

        makeCallDialog *pDlg = new makeCallDialog();
        pDlg->findChild();

        1 Reply Last reply
        0
        • P Pl45m4
          26 Feb 2019, 00:48

          @Burke212

          Hi,

          I think the easiest way would be, if you use an iterator to go through your QList of QPushButtons and connect the current QPushButton while iterating. You can also rename your Buttons during this process.

          EDIT: Where do you create your Buttons?

          QPushButton *num[10];
          

          Does not create an array of 10 (usable) PushButton-Objects. You define 10 pointers to a QPushButton there, but no QPushButton is initialized.
          Better:

          QPushButton myBtn; // Calls Std-Constructor of QPushButton
          // OR
          QPushButton *myBtn = new QPushButton ();
          

          I dont get the use of findChild there... If you are trying to "find" the buttons, that were created somewhere else (in parent class), to connect them, why dont you connect them directly in your parent class? (something like sending a signal to notify your Dialog that "Button Nr. X" was pressed / released)

          B Offline
          B Offline
          Burke212
          wrote on 26 Feb 2019, 12:52 last edited by
          #7

          @Pl45m4

          I don't have a QList of QPushButtons, I simply declare the buttons in the header, then define them in cpp.

          In header:

          QPushButton *Button0;
          ...
          QPushButton *Button9;
          

          In cpp:

          Button0 = new QPushButton(this);
          ....
          Button9 = new QPushButton(this);
          

          The *QPushButton num[10] is going to store the button's names & connect them to numPressed().

          numButtons[i] = makeCallDialog::findChild<QPushButton *>(buttonName);
          
          connect(numButtons[i], &QPushButton::released, this, &makeCallDialog::numPressed);
          

          So the idea is numButtons[0] is set to Button0, then Button0 is connected to numPressed(), and so forth with the other elements in the array.

          J K 2 Replies Last reply 26 Feb 2019, 12:58
          0
          • B Burke212
            26 Feb 2019, 12:52

            @Pl45m4

            I don't have a QList of QPushButtons, I simply declare the buttons in the header, then define them in cpp.

            In header:

            QPushButton *Button0;
            ...
            QPushButton *Button9;
            

            In cpp:

            Button0 = new QPushButton(this);
            ....
            Button9 = new QPushButton(this);
            

            The *QPushButton num[10] is going to store the button's names & connect them to numPressed().

            numButtons[i] = makeCallDialog::findChild<QPushButton *>(buttonName);
            
            connect(numButtons[i], &QPushButton::released, this, &makeCallDialog::numPressed);
            

            So the idea is numButtons[0] is set to Button0, then Button0 is connected to numPressed(), and so forth with the other elements in the array.

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 26 Feb 2019, 12:58 last edited by
            #8

            @Burke212 Why do you need all these Button0..Button9 variables if you're then storing the pointers in numButtons?
            Why not simply

            numButtons[i] = new QPushButton(this);
            

            ?

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

            B 1 Reply Last reply 26 Feb 2019, 13:42
            0
            • B Burke212
              26 Feb 2019, 12:52

              @Pl45m4

              I don't have a QList of QPushButtons, I simply declare the buttons in the header, then define them in cpp.

              In header:

              QPushButton *Button0;
              ...
              QPushButton *Button9;
              

              In cpp:

              Button0 = new QPushButton(this);
              ....
              Button9 = new QPushButton(this);
              

              The *QPushButton num[10] is going to store the button's names & connect them to numPressed().

              numButtons[i] = makeCallDialog::findChild<QPushButton *>(buttonName);
              
              connect(numButtons[i], &QPushButton::released, this, &makeCallDialog::numPressed);
              

              So the idea is numButtons[0] is set to Button0, then Button0 is connected to numPressed(), and so forth with the other elements in the array.

              K Offline
              K Offline
              KroMignon
              wrote on 26 Feb 2019, 13:15 last edited by KroMignon
              #9

              @Burke212 Just some side notes from my side.
              It is not common do define a class name with starts with lowcase. A good pratice is to have class names calmelcase starting with upper case (makeCallDialog should by MakeCallDialog) and variable name starting with low case.

              Second: why do you use sereval variables and not a container like QVector

              in header

              QVector<QPushButton*> myButtons;
              

              in cpp:

              ...
                  for(int idx = 0; idx < 10; ++idx)
                  {
                      myButtons << new QPushButton(this);
                      connect(myButtons.last(), &QPushButton::released, this, &makeCallDialog::numPressed);
                  }
              ...
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • J jsulm
                26 Feb 2019, 12:58

                @Burke212 Why do you need all these Button0..Button9 variables if you're then storing the pointers in numButtons?
                Why not simply

                numButtons[i] = new QPushButton(this);
                

                ?

                B Offline
                B Offline
                Burke212
                wrote on 26 Feb 2019, 13:42 last edited by
                #10

                @jsulm

                I'm following this tutorial. Since I'm working in source only, I'm not able to reference from the UI. So that's where my issue came from; in the tutorial he references the buttons because they're in the UI/ designer.

                K 1 Reply Last reply 26 Feb 2019, 14:51
                0
                • B Burke212
                  26 Feb 2019, 13:42

                  @jsulm

                  I'm following this tutorial. Since I'm working in source only, I'm not able to reference from the UI. So that's where my issue came from; in the tutorial he references the buttons because they're in the UI/ designer.

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 26 Feb 2019, 14:51 last edited by
                  #11

                  @Burke212 this works in tutorial, because QPushButton instance are defined with .ui and eah one has a name
                  0_1551192414591_08828eee-fb13-493c-81b6-450fba0fe5d8-image.png

                  "Button0" to "Button9", this has no link with variable name.

                  To made this work, you must do:

                  Button0 = new QPushButton(this);
                  Button0->setObjectName("Button0");
                  ....
                  Button9 = new QPushButton(this);
                  Button9->setObjectName("Button9");
                  

                  or

                  ...
                      for(int idx = 0; idx < 10; ++idx)
                      {
                          myButtons << new QPushButton(this);
                          myButtons.last()->setObjectName(QStringLiteral("Button%1").arg(idx));
                          connect(myButtons.last(), &QPushButton::released, this, &makeCallDialog::numPressed);
                      }
                  ...
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  4
                  • B Offline
                    B Offline
                    Burke212
                    wrote on 26 Feb 2019, 15:03 last edited by
                    #12

                    Thank you, everyone for your awesome help!

                    P 1 Reply Last reply 26 Feb 2019, 19:54
                    0
                    • B Burke212
                      26 Feb 2019, 15:03

                      Thank you, everyone for your awesome help!

                      P Offline
                      P Offline
                      Pl45m4
                      wrote on 26 Feb 2019, 19:54 last edited by Pl45m4
                      #13

                      @Burke212

                      One more thing... :)
                      So you create 10 Buttons and connect them somehow to "numPressed", that prints or computes the button-number, if a button is clicked + released, right?

                      The use of a QButtonGroup would make this even easier.
                      https://doc.qt.io/qt-5.6/qbuttongroup.html

                      Add your buttons to your buttonGroup and setID of each button.
                      ButtonGroup also provides signals and functions to work with. Maybe your own connection is not necessary anymore and you dont need to rename every single button, because you can work with the groupID.

                      When adding the buttons to your group, set your own IDs, as the default buttonID in a buttonGroup starts at -2 and decreases with every additional button (this gave me headache in one of my projects some time ago... Of course, I didnt set my own IDs and was expecting the ID to start at 0 increasing. And of course I didnt read the Docs of the addButton-Function back then) :)


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0

                      13/13

                      26 Feb 2019, 19:54

                      • Login

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