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 Updated to NodeBB v4.3 + New Features

Connect signals and slots of a class from another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 830 Views 3 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.
  • 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 Online
      Pl45m4P Online
      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 Online
          Pl45m4P Online
          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