Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pointer to another class' slot
Qt 6.12 LTS is out! See what's new in the release blog

Pointer to another class' slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 6 Posters 5.1k Views 2 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.
  • RyanRR RyanR

    Sorry I should've mentioned,
    "menu" isn't a variable, it is a field in an enum declaration

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by aha_1980
    #4

    Hi @RyanR,

    as @SGaist said, it's not possible to add parameters in a connect statement.

    You could try something like this (only brain compiled):

    connect(ui->pbMenu, &QMenu::clicked, this, [menu, master]() {
        master->changeForm(menu);
    });
    

    Regards

    Qt has to stay free or it will die.

    J.HilkJ RyanRR 3 Replies Last reply
    4
    • aha_1980A aha_1980

      Hi @RyanR,

      as @SGaist said, it's not possible to add parameters in a connect statement.

      You could try something like this (only brain compiled):

      connect(ui->pbMenu, &QMenu::clicked, this, [menu, master]() {
          master->changeForm(menu);
      });
      

      Regards

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #5

      @aha_1980 šŸ¤” I think you would at least need to capture Master and menu, to use it in the lambda
      šŸ¤—


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      aha_1980A 1 Reply Last reply
      3
      • J.HilkJ J.Hilk

        @aha_1980 šŸ¤” I think you would at least need to capture Master and menu, to use it in the lambda
        šŸ¤—

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @J.Hilk said in Pointer to another class' slot:

        @aha_1980 šŸ¤” I think you would at least need to capture Master and menu, to use it in the lambda
        šŸ¤—

        Done

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • aha_1980A aha_1980

          Hi @RyanR,

          as @SGaist said, it's not possible to add parameters in a connect statement.

          You could try something like this (only brain compiled):

          connect(ui->pbMenu, &QMenu::clicked, this, [menu, master]() {
              master->changeForm(menu);
          });
          

          Regards

          RyanRR Offline
          RyanRR Offline
          RyanR
          wrote on last edited by
          #7
          This post is deleted!
          1 Reply Last reply
          0
          • aha_1980A aha_1980

            Hi @RyanR,

            as @SGaist said, it's not possible to add parameters in a connect statement.

            You could try something like this (only brain compiled):

            connect(ui->pbMenu, &QMenu::clicked, this, [menu, master]() {
                master->changeForm(menu);
            });
            

            Regards

            RyanRR Offline
            RyanRR Offline
            RyanR
            wrote on last edited by RyanR
            #8

            Thanks for the reply @aha_1980
            That looks like it'll accomplish exactly what I was hoping for, however I'm not sure I fully understand the syntax/parameters.
            The function declaration is the part that is throwing me off.

            Is it

            connect(myButton, 
                 signal-clicked, 
                 *class1, 
                 [class1, class2]() 
            {
                 //Function definition
            });
            

            If that's correct, why do I need to include class2 there?
            To me, it looks like I'm essentially defining a new SLOT/function as my 4th parameter, is that right?

            I appreciate all the help you guys have been providing!


            @SGaist was spot on that you cannot pass variables into the Connect parameters, so thanks for clearing that up for me!

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #9

              Assuming master is of type Master* and menu is a pointer you can also use

              connect(ui->pbMenu, &QMenu::clicked, this, std::bind(&Master::changeForm,master,menu));
              

              "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

              VRoninV 1 Reply Last reply
              4
              • RyanRR Offline
                RyanRR Offline
                RyanR
                wrote on last edited by RyanR
                #10

                I gave that a shot, simply changing &QMenu to &QAbstractButton, as so:

                connect(ui->pbMenu, &QAbstractButton::clicked, this, std::bind(&Master::changeForm,master,menu));
                

                And it does compile and run fine, however, a Segmentation fault occurs whenever the button is clicked.
                I do have Master::changeForm(pageList p1) defined as a public slot:, but I'm assuming the error lies in the fact that the seperate class is unable to access the the changeForm function?

                VRoninV 1 Reply Last reply
                0
                • VRoninV VRonin

                  Assuming master is of type Master* and menu is a pointer you can also use

                  connect(ui->pbMenu, &QMenu::clicked, this, std::bind(&Master::changeForm,master,menu));
                  
                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #11

                  Look like you are breaking one of my assumptions:

                  @VRonin said in Pointer to another class' slot:

                  and menu is a pointer

                  What is menu?

                  "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

                  aha_1980A 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    Look like you are breaking one of my assumptions:

                    @VRonin said in Pointer to another class' slot:

                    and menu is a pointer

                    What is menu?

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @VRonin

                    What is menu?

                    ->

                    Sorry I should've mentioned,
                    "menu" isn't a variable, it is a field in an enum declaration

                    Regards

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    1
                    • RyanRR RyanR

                      I gave that a shot, simply changing &QMenu to &QAbstractButton, as so:

                      connect(ui->pbMenu, &QAbstractButton::clicked, this, std::bind(&Master::changeForm,master,menu));
                      

                      And it does compile and run fine, however, a Segmentation fault occurs whenever the button is clicked.
                      I do have Master::changeForm(pageList p1) defined as a public slot:, but I'm assuming the error lies in the fact that the seperate class is unable to access the the changeForm function?

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

                      @RyanR said in Pointer to another class' slot:

                      a Segmentation fault occurs whenever the button is clicked

                      Could you post the stack trace?

                      "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
                      • RyanRR Offline
                        RyanRR Offline
                        RyanR
                        wrote on last edited by
                        #14

                        For sure, in master.cpp within the void Master::changeForm(pageList p1) function:

                        Segmentation fault occurs at this->setCurrentIndex(menu_index);

                        where menu_index is a member variable (int) defined in the Master as
                        menu_index = addWidget(menu_pg);

                        Master also extends/inherits public QStackedWidget

                        Hope this helps clarify where I'm at

                        1 Reply Last reply
                        0
                        • RyanRR Offline
                          RyanRR Offline
                          RyanR
                          wrote on last edited by RyanR
                          #15

                          If I simply use

                          connect(ui->pbMenu, SIGNAL(clicked()), this, SLOT(pbMenuClicked()));

                          with the function/slot

                          void myclass::pbMenuClicked()
                          {
                              master->changeForm(menu);
                          }
                          

                          It works flawlessly, but I'm trying to avoid doing this.
                          Essentially what I'm trying to do is eliminate the need to write out this function for every instance of a Form-changing button on each page, as I would therefore need to have this function defined across every different Form/class.
                          Instead I'd like to have 1 function defined in Master that I can use Connect to point to this function every time one of my Form Change buttons are clicked.

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            Hi
                            But how does each button know, what menu enum it should use ?

                            1 Reply Last reply
                            0
                            • RyanRR Offline
                              RyanRR Offline
                              RyanR
                              wrote on last edited by RyanR
                              #17

                              As of right now, I have

                              typedef enum _pageList
                              {
                                   menu,
                                   page2,
                                   //and so on...
                              }pageList;
                              

                              There is 1 field per page. This is used to be able to keep track of the Current page, Previous page, and in this case, which page I'd like to change to (handled by the changeForm(pageList))

                              Each form has a separate button for changing to another form (1 button per form, in my case, 4). What I was hoping to do was use 3 connect's per form/class, pointing tochangeForm(PAGE-TO-CHANGE-TO) with the parameter specifying which page to change to (all handled by Master)

                              As mentioned before, it seems I cannot pass parameters into SLOTS/Connect, so I also had tried simply defining 4 SLOTS in Master to call that same function to again, change pages.

                              void Master::pbMenuClicked()
                              {
                                  changeForm(menu);
                              }
                              

                              But that brings me back to my original issue, being that I am unable to point to another class' SLOT in my connect statements which are located in each Form.

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #18

                                Hi
                                When you say

                                • connect statements which are located in each Form.
                                  You mean different actual different Forms that are not in same
                                  Qstackedwidget(master) or by Forms you mean pages in the same
                                  qstackedwidget ?

                                Anyway, the syntax
                                connect(ui->pbMenu, &QMenu::clicked, this, std::bind(&Master::changeForm,master, menu ));
                                does allow you to attach "menu" int to the clicking of the button.

                                If you crash in
                                this->setCurrentIndex(menu_index);
                                it must mean that the master variable is invalid as
                                setCurrentIndex does range check/dont crash.

                                So can you show the surrounding code where u use the std::bind connect?
                                I was wondering why master is not in ui?
                                like
                                connect(ui->pbMenu, &QAbstractButton::clicked, this, std::bind(&Master::changeForm,ui->master,menu));
                                Its inserted dynamically ?

                                1 Reply Last reply
                                2

                                • Login

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