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. Connect signals and slots of a class from another class
Forum Update on Monday, May 27th 2025

Connect signals and slots of a class from another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 830 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
    thewiggin
    wrote on last edited by thewiggin
    #1

    Hello,
    I am getting a compiler centered around this line:

    connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
    

    /no matching function for call to ‘ParamTab::connect(QPushButton*&, void (QAbstractButton::)(bool), QWidget, void (ParamPropPg::)(bool))’
    253 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
    | ^
    conversion from ‘QWidget
    ’ to ‘const Object*’ {aka ‘const ParamPropPg*’} [-fpermissive]
    253 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
    | ~~~~~~~~~~~~~^
    | |
    | QWidget*

    I tried various things and this line reduced it to one error:

    connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), static_cast<void (ParamPropPg::*) (qreal)>(&ParamPropPg::pushChanges));
    

    invalid static_cast from type ‘void (ParamPropPg::)()’ to type ‘void (ParamPropPg::)(qreal)’ {aka ‘void (ParamPropPg::)(double)’}
    252 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), static_cast<void (ParamPropPg::
    ) (qreal)>(&ParamPropPg::pushChanges));
    | ^

    All classes involved have Q_OBJECT macro in the header and are QWidgets or derived from some type of QWidget.

    I am creating the ui items and adding them to the ParamPropPg class from a different class, which is working. I am trying to connect the button that was created to the public slot of the ParamPropPg class. The ParamPropPg class is included (and is even in the same header file as the class calling the connect).

    Basically, I am trying to connect a class' signals and slots from another class. Is this possible or do I need I need to create some function in the ParamPropPg class that is called when I want to connect one of it's own objects/children signals to its own slot?

    CP71C 1 Reply Last reply
    0
    • T thewiggin

      Hello,
      I am getting a compiler centered around this line:

      connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
      

      /no matching function for call to ‘ParamTab::connect(QPushButton*&, void (QAbstractButton::)(bool), QWidget, void (ParamPropPg::)(bool))’
      253 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
      | ^
      conversion from ‘QWidget
      ’ to ‘const Object*’ {aka ‘const ParamPropPg*’} [-fpermissive]
      253 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
      | ~~~~~~~~~~~~~^
      | |
      | QWidget*

      I tried various things and this line reduced it to one error:

      connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), static_cast<void (ParamPropPg::*) (qreal)>(&ParamPropPg::pushChanges));
      

      invalid static_cast from type ‘void (ParamPropPg::)()’ to type ‘void (ParamPropPg::)(qreal)’ {aka ‘void (ParamPropPg::)(double)’}
      252 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), static_cast<void (ParamPropPg::
      ) (qreal)>(&ParamPropPg::pushChanges));
      | ^

      All classes involved have Q_OBJECT macro in the header and are QWidgets or derived from some type of QWidget.

      I am creating the ui items and adding them to the ParamPropPg class from a different class, which is working. I am trying to connect the button that was created to the public slot of the ParamPropPg class. The ParamPropPg class is included (and is even in the same header file as the class calling the connect).

      Basically, I am trying to connect a class' signals and slots from another class. Is this possible or do I need I need to create some function in the ParamPropPg class that is called when I want to connect one of it's own objects/children signals to its own slot?

      CP71C Offline
      CP71C Offline
      CP71
      wrote on last edited by
      #2

      @thewiggin
      Hi,
      Is applyBtn a pointer to QPushButton?
      Does propPgs->widget(i) return a pointer to ParamPropPg?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thewiggin
        wrote on last edited by
        #3

        @CP71 Yes to both, sorry that’s not clear. I will try to untangle my code to get provide a minimum reproducible example

        CP71C 1 Reply Last reply
        1
        • T thewiggin

          @CP71 Yes to both, sorry that’s not clear. I will try to untangle my code to get provide a minimum reproducible example

          CP71C Offline
          CP71C Offline
          CP71
          wrote on last edited by CP71
          #4

          @thewiggin
          does propPgs->widget(i) return a const pointer?
          Is yes, try to remove const

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

            Hi
            it kinda seems that you try to hook the clicked() signal up to a slot that expects a qreal ?

            • Basically, I am trying to connect a class' signals and slots from another class. Is this possible
              Yes its kinda the very foundation on why signal and slots are cool. :)
            Pl45m4P 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi
              it kinda seems that you try to hook the clicked() signal up to a slot that expects a qreal ?

              • Basically, I am trying to connect a class' signals and slots from another class. Is this possible
                Yes its kinda the very foundation on why signal and slots are cool. :)
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @mrjj

              Man, just deleted my text as I've seen your comment :)
              I need too much time to finish my replies :)

              @thewiggin If you slot really needs a qreal, try to connect using lambda connections.


              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
              • T Offline
                T Offline
                thewiggin
                wrote on last edited by
                #7

                Here's the example with the relative information included. the EnglishPage only has one layout and if the connect line is commented out the code compiles and does add the button to each page in the stack.

                gui.h.......

                #include <QDialog>
                 
                QT_BEGIN_NAMESPACE
                 
                QT_END_NAMESPACE
                 
                class gui : public QDialog
                {
                    Q_OBJECT
                 
                public:
                    gui(QWidget *parent = nullptr);
                    ~gui();
                 
                };
                

                gui.c........

                #include "gui.h"
                #include <QtWidgets>
                #include "mylistwidget.h"
                 
                gui::gui(QWidget *parent)
                    : QDialog(parent)
                {
                    setFixedSize(800,600);
                    setWindowTitle(tr("Gui Tests"));
                 
                    QVBoxLayout *mainLayout = new QVBoxLayout;
                    QTabWidget *tabWidget = new QTabWidget;
                    MyListWidget *myListWidget = new MyListWidget;
                    QScrollArea *myListWidgetScrollArea = new QScrollArea;
                    myListWidgetScrollArea->setWidget(myListWidget);
                    tabWidget->addTab(myListWidgetScrollArea, "List Widget");
                    mainLayout->addWidget(tabWidget);
                 
                    for(int i = 0; i < myListWidget->pageStack->count(); ++i)
                    {
                        QVBoxLayout *pageLay = myListWidget->pageStack->widget(i)->findChild<QVBoxLayout*>();
                        if(pageLay)
                        {
                            QPushButton *applyBtn = new QPushButton("push");
                            pageLay->addWidget(applyBtn, 0, Qt::AlignCenter);
                            connect(applyBtn, &QPushButton::clicked, myListWidget->pageStack->widget(i), &EnglishPage::btnClicked);
                        }
                    }
                

                Mylistwidget.h…..

                #include <QListWidget>
                #include <QStackedWidget>

                class MyListWidget : public QWidget
                {
                Q_OBJECT

                public:
                MyListWidget();
                QStackedWidget *pageStack;
                QListWidget *listWidget;
                }

                setLayout(mainLayout);
                setWindowTitle(QGuiApplication::applicationDisplayName());
                }

                class EnglishPage : public QWidget
                {
                Q_OBJECT
                public:
                EnglishPage(QWidget *parent = 0);

                public slots:
                void btnClicked(void);
                };

                @CP71 I widget does return const. I'm not too familiar with C++. The hover bubble over the function reads QWidget *QStackedWidget::(int index) const. I guess const on the end makes the return const? How do I "remove" the const?

                Pl45m4P CP71C 3 Replies Last reply
                0
                • T thewiggin

                  Here's the example with the relative information included. the EnglishPage only has one layout and if the connect line is commented out the code compiles and does add the button to each page in the stack.

                  gui.h.......

                  #include <QDialog>
                   
                  QT_BEGIN_NAMESPACE
                   
                  QT_END_NAMESPACE
                   
                  class gui : public QDialog
                  {
                      Q_OBJECT
                   
                  public:
                      gui(QWidget *parent = nullptr);
                      ~gui();
                   
                  };
                  

                  gui.c........

                  #include "gui.h"
                  #include <QtWidgets>
                  #include "mylistwidget.h"
                   
                  gui::gui(QWidget *parent)
                      : QDialog(parent)
                  {
                      setFixedSize(800,600);
                      setWindowTitle(tr("Gui Tests"));
                   
                      QVBoxLayout *mainLayout = new QVBoxLayout;
                      QTabWidget *tabWidget = new QTabWidget;
                      MyListWidget *myListWidget = new MyListWidget;
                      QScrollArea *myListWidgetScrollArea = new QScrollArea;
                      myListWidgetScrollArea->setWidget(myListWidget);
                      tabWidget->addTab(myListWidgetScrollArea, "List Widget");
                      mainLayout->addWidget(tabWidget);
                   
                      for(int i = 0; i < myListWidget->pageStack->count(); ++i)
                      {
                          QVBoxLayout *pageLay = myListWidget->pageStack->widget(i)->findChild<QVBoxLayout*>();
                          if(pageLay)
                          {
                              QPushButton *applyBtn = new QPushButton("push");
                              pageLay->addWidget(applyBtn, 0, Qt::AlignCenter);
                              connect(applyBtn, &QPushButton::clicked, myListWidget->pageStack->widget(i), &EnglishPage::btnClicked);
                          }
                      }
                  

                  Mylistwidget.h…..

                  #include <QListWidget>
                  #include <QStackedWidget>

                  class MyListWidget : public QWidget
                  {
                  Q_OBJECT

                  public:
                  MyListWidget();
                  QStackedWidget *pageStack;
                  QListWidget *listWidget;
                  }

                  setLayout(mainLayout);
                  setWindowTitle(QGuiApplication::applicationDisplayName());
                  }

                  class EnglishPage : public QWidget
                  {
                  Q_OBJECT
                  public:
                  EnglishPage(QWidget *parent = 0);

                  public slots:
                  void btnClicked(void);
                  };

                  @CP71 I widget does return const. I'm not too familiar with C++. The hover bubble over the function reads QWidget *QStackedWidget::(int index) const. I guess const on the end makes the return const? How do I "remove" the const?

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @thewiggin said in Connect signals and slots of a class from another class:

                  void btnClicked(void);

                  What happens if you remove the void parameter, since clicked signal passes a boolean by default to the slot?
                  (Just leave it empty)


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

                  ~E. W. Dijkstra

                  CP71C 1 Reply Last reply
                  2
                  • T thewiggin

                    Here's the example with the relative information included. the EnglishPage only has one layout and if the connect line is commented out the code compiles and does add the button to each page in the stack.

                    gui.h.......

                    #include <QDialog>
                     
                    QT_BEGIN_NAMESPACE
                     
                    QT_END_NAMESPACE
                     
                    class gui : public QDialog
                    {
                        Q_OBJECT
                     
                    public:
                        gui(QWidget *parent = nullptr);
                        ~gui();
                     
                    };
                    

                    gui.c........

                    #include "gui.h"
                    #include <QtWidgets>
                    #include "mylistwidget.h"
                     
                    gui::gui(QWidget *parent)
                        : QDialog(parent)
                    {
                        setFixedSize(800,600);
                        setWindowTitle(tr("Gui Tests"));
                     
                        QVBoxLayout *mainLayout = new QVBoxLayout;
                        QTabWidget *tabWidget = new QTabWidget;
                        MyListWidget *myListWidget = new MyListWidget;
                        QScrollArea *myListWidgetScrollArea = new QScrollArea;
                        myListWidgetScrollArea->setWidget(myListWidget);
                        tabWidget->addTab(myListWidgetScrollArea, "List Widget");
                        mainLayout->addWidget(tabWidget);
                     
                        for(int i = 0; i < myListWidget->pageStack->count(); ++i)
                        {
                            QVBoxLayout *pageLay = myListWidget->pageStack->widget(i)->findChild<QVBoxLayout*>();
                            if(pageLay)
                            {
                                QPushButton *applyBtn = new QPushButton("push");
                                pageLay->addWidget(applyBtn, 0, Qt::AlignCenter);
                                connect(applyBtn, &QPushButton::clicked, myListWidget->pageStack->widget(i), &EnglishPage::btnClicked);
                            }
                        }
                    

                    Mylistwidget.h…..

                    #include <QListWidget>
                    #include <QStackedWidget>

                    class MyListWidget : public QWidget
                    {
                    Q_OBJECT

                    public:
                    MyListWidget();
                    QStackedWidget *pageStack;
                    QListWidget *listWidget;
                    }

                    setLayout(mainLayout);
                    setWindowTitle(QGuiApplication::applicationDisplayName());
                    }

                    class EnglishPage : public QWidget
                    {
                    Q_OBJECT
                    public:
                    EnglishPage(QWidget *parent = 0);

                    public slots:
                    void btnClicked(void);
                    };

                    @CP71 I widget does return const. I'm not too familiar with C++. The hover bubble over the function reads QWidget *QStackedWidget::(int index) const. I guess const on the end makes the return const? How do I "remove" the const?

                    CP71C Offline
                    CP71C Offline
                    CP71
                    wrote on last edited by CP71
                    #9

                    @thewiggin
                    const at the end: "QWidget *QStackedWidget::(int index) const" means function body of class can not change its member varables ( variables are defined in .h )

                    1 Reply Last reply
                    1
                    • T thewiggin

                      Here's the example with the relative information included. the EnglishPage only has one layout and if the connect line is commented out the code compiles and does add the button to each page in the stack.

                      gui.h.......

                      #include <QDialog>
                       
                      QT_BEGIN_NAMESPACE
                       
                      QT_END_NAMESPACE
                       
                      class gui : public QDialog
                      {
                          Q_OBJECT
                       
                      public:
                          gui(QWidget *parent = nullptr);
                          ~gui();
                       
                      };
                      

                      gui.c........

                      #include "gui.h"
                      #include <QtWidgets>
                      #include "mylistwidget.h"
                       
                      gui::gui(QWidget *parent)
                          : QDialog(parent)
                      {
                          setFixedSize(800,600);
                          setWindowTitle(tr("Gui Tests"));
                       
                          QVBoxLayout *mainLayout = new QVBoxLayout;
                          QTabWidget *tabWidget = new QTabWidget;
                          MyListWidget *myListWidget = new MyListWidget;
                          QScrollArea *myListWidgetScrollArea = new QScrollArea;
                          myListWidgetScrollArea->setWidget(myListWidget);
                          tabWidget->addTab(myListWidgetScrollArea, "List Widget");
                          mainLayout->addWidget(tabWidget);
                       
                          for(int i = 0; i < myListWidget->pageStack->count(); ++i)
                          {
                              QVBoxLayout *pageLay = myListWidget->pageStack->widget(i)->findChild<QVBoxLayout*>();
                              if(pageLay)
                              {
                                  QPushButton *applyBtn = new QPushButton("push");
                                  pageLay->addWidget(applyBtn, 0, Qt::AlignCenter);
                                  connect(applyBtn, &QPushButton::clicked, myListWidget->pageStack->widget(i), &EnglishPage::btnClicked);
                              }
                          }
                      

                      Mylistwidget.h…..

                      #include <QListWidget>
                      #include <QStackedWidget>

                      class MyListWidget : public QWidget
                      {
                      Q_OBJECT

                      public:
                      MyListWidget();
                      QStackedWidget *pageStack;
                      QListWidget *listWidget;
                      }

                      setLayout(mainLayout);
                      setWindowTitle(QGuiApplication::applicationDisplayName());
                      }

                      class EnglishPage : public QWidget
                      {
                      Q_OBJECT
                      public:
                      EnglishPage(QWidget *parent = 0);

                      public slots:
                      void btnClicked(void);
                      };

                      @CP71 I widget does return const. I'm not too familiar with C++. The hover bubble over the function reads QWidget *QStackedWidget::(int index) const. I guess const on the end makes the return const? How do I "remove" the const?

                      CP71C Offline
                      CP71C Offline
                      CP71
                      wrote on last edited by
                      #10

                      @thewiggin
                      I don't see anything strange! or it seems so.
                      I know, what I say now could be strange, but try to remove the build folder via file explorer.
                      I don't know why, sometimes when I see something that is strange and it seems inexplicable this operation removes the issue.
                      I don't say this is the case, but you don't have anything to lose ;)

                      1 Reply Last reply
                      1
                      • Pl45m4P Pl45m4

                        @thewiggin said in Connect signals and slots of a class from another class:

                        void btnClicked(void);

                        What happens if you remove the void parameter, since clicked signal passes a boolean by default to the slot?
                        (Just leave it empty)

                        CP71C Offline
                        CP71C Offline
                        CP71
                        wrote on last edited by
                        #11

                        @Pl45m4
                        I have tried to do this.
                        I have made connect with and without void but I haven't seen the issue

                        1 Reply Last reply
                        1
                        • T Offline
                          T Offline
                          thewiggin
                          wrote on last edited by
                          #12

                          @CP71 no luck when removing build folder and retrying

                          @Pl45m4 changing the slot from void to nothing did not resolve this

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            thewiggin
                            wrote on last edited by
                            #13

                            Here's my workaround, it seems needlessly complex and has me begging the question of if its possible to connect to the slot of another class outside that class. All the declarations I can find use "this" as the third augment.

                            gui.c I added this at the bottom of the for loop, right after adding the button the the layout.

                            EnglishPage *pg = dynamic_cast<EnglishPage*>(myListWidget->pageStack->widget(i));
                                        if(pg)
                                        {
                                             pg->setBtn(applyBtn);
                                        }
                                        else
                                        {
                                            qDebug() << "failed to connect apply btn for english page";
                                       }
                            

                            I changed the public slot in EnglishPage to a private slot and added a public function called setBtn.

                            void EnglishPage::setBtn(QPushButton *btn)
                            {
                                connect(btn, &QPushButton::clicked, this, &EnglishPage::btnClicked);
                            }
                            
                            CP71C 1 Reply Last reply
                            1
                            • T Offline
                              T Offline
                              thewiggin
                              wrote on last edited by
                              #14

                              Well shockingly it was needlessly complex.

                              The below line works in context with the other code posted. I will aggregate it together in this post eventually for a clean solution unless someone provides a better one. I had to change the slot back to public from private.

                              connect(applyBtn, &QPushButton::clicked, dynamic_cast<EnglishPage*>(myListWidget->pageStack->widget(i)), &EnglishPage::btnclicked);
                              
                              CP71C 1 Reply Last reply
                              1
                              • T thewiggin

                                Well shockingly it was needlessly complex.

                                The below line works in context with the other code posted. I will aggregate it together in this post eventually for a clean solution unless someone provides a better one. I had to change the slot back to public from private.

                                connect(applyBtn, &QPushButton::clicked, dynamic_cast<EnglishPage*>(myListWidget->pageStack->widget(i)), &EnglishPage::btnclicked);
                                
                                CP71C Offline
                                CP71C Offline
                                CP71
                                wrote on last edited by CP71
                                #15

                                @thewiggin
                                Sorry, my mistake :(
                                There is something strange!
                                myListWidget-> pageStack-> widget (i) returns QWidget * and not EnglishPage *, for this it didn't found btnClicked.
                                Sometimes the solution is under our eyes but it hides under them:;

                                1 Reply Last reply
                                2
                                • T thewiggin

                                  Here's my workaround, it seems needlessly complex and has me begging the question of if its possible to connect to the slot of another class outside that class. All the declarations I can find use "this" as the third augment.

                                  gui.c I added this at the bottom of the for loop, right after adding the button the the layout.

                                  EnglishPage *pg = dynamic_cast<EnglishPage*>(myListWidget->pageStack->widget(i));
                                              if(pg)
                                              {
                                                   pg->setBtn(applyBtn);
                                              }
                                              else
                                              {
                                                  qDebug() << "failed to connect apply btn for english page";
                                             }
                                  

                                  I changed the public slot in EnglishPage to a private slot and added a public function called setBtn.

                                  void EnglishPage::setBtn(QPushButton *btn)
                                  {
                                      connect(btn, &QPushButton::clicked, this, &EnglishPage::btnClicked);
                                  }
                                  
                                  CP71C Offline
                                  CP71C Offline
                                  CP71
                                  wrote on last edited by CP71
                                  #16

                                  @thewiggin said in Connect signals and slots of a class from another class:

                                  Here's my workaround, it seems needlessly complex and has me begging the question of if its possible to connect to the slot of another class outside that class. All the declarations I can find use "this" as the third augment.

                                  Yes of course!
                                  you see "this" because normally the sender or/and the receiver is the class itself, but you can connect one or two classes that arent the classe itself!
                                  But this you already see in your last post!

                                  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