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. Signal and slot from other class not working

Signal and slot from other class not working

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 7 Posters 4.2k 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.
  • U Offline
    U Offline
    Ucn_
    wrote on 23 Nov 2019, 14:56 last edited by Ucn_
    #1

    I have two classes in my second ui. I'm trying to connect signal and slot emitted in another class on QLabel click event (I promoted this Label) so I can update QLabel->setText("Some texts") from other class. However it does not work, I have been trying to make it sometimes it will run, but the QLabel->setText("Some texts"); won't update on the ui. Here is my code:

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include "secondform.h"
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        SecondForm *captureWig;
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
    
        captureWig = new SecondForm(this);
        QVBoxLayout *sendWig = new QVBoxLayout;
        sendWig = captureWig->getTreeto();
    
        QWidget *w = new QWidget();
        w->setLayout(sendWig);
    
        ui->tabWidget->addTab(w, "Newtab");
    }
    

    secondform.h

    
    #ifndef SECONDFORM_H
    #define SECONDFORM_H
    
    #include <QObject>
    #include <QWidget>
    #include <QtWidgets>
    
    class ClickableLabels;
    
    namespace Ui {
    class SecondForm;
    }
    
    class SecondForm : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit SecondForm(QWidget *parent = nullptr);
        ~SecondForm();
        QVBoxLayout *getTreeto();
    
    signals:
    void send_fromMain();
    
    private slots:
        void on_Clickme_clicked();
        void ReceivedFromSecondForm();
    
    private:
        ClickableLabels *upLabText;
        QPushButton *button_first;
        Ui::SecondForm *ui;
    };
    
    
    class ClickableLabels : public QLabel {
    
        Q_OBJECT
    
    public:
        explicit ClickableLabels(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
        ~ClickableLabels();
    
    signals:
        void clicked();
        void SentFromSecondForm();
    
    protected:
        void mousePressEvent(QMouseEvent* event);
    
    private:
    
    };
    
    #endif // SECONDFORM_H
    

    secondform.cpp

    
    #include "secondform.h"
    #include "ui_secondform.h"
    
    SecondForm::SecondForm(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::SecondForm)
    {
        ui->setupUi(this);
    
        upLabText = new ClickableLabels(this);
    
        QObject::connect (upLabText, SIGNAL (SentFromSecondForm()), this, SLOT(ReceivedFromSecondForm()));
    }
    
    void SecondForm::ReceivedFromSecondForm()
    {
        ui->label_4->setText("Received");
        qDebug () << "Emitted: ";
    }
    
    QVBoxLayout *SecondForm::getTreeto(){
    
        return ui->verticalLayout_6;
    }
    
    ClickableLabels::ClickableLabels(QWidget* parent, Qt::WindowFlags f)
        : QLabel(parent) {
    
    }
    
    ClickableLabels::~ClickableLabels() {}
    
    void ClickableLabels::mousePressEvent(QMouseEvent* ) {
    
        emit SentFromSecondForm();
        emit clicked();
        qDebug() << "QLabel clicked clicked";
    
    }
    
    SecondForm::~SecondForm()
    {
        delete ui;
    }
    
    void SecondForm::on_Clickme_clicked()
    {
        ui->label_4->setText("label clicked");
    }
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>657</width>
        <height>663</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QTabWidget" name="tabWidget"/>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>add</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>657</width>
         <height>21</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    

    secondform.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>SecondForm</class>
     <widget class="QWidget" name="SecondForm">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>765</width>
        <height>508</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <widget class="QWidget" name="verticalLayoutWidget_2">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>30</y>
         <width>279</width>
         <height>137</height>
        </rect>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout_6">
        <item>
         <widget class="QGroupBox" name="groupBox">
          <property name="title">
           <string>GroupBox</string>
          </property>
          <layout class="QVBoxLayout" name="verticalLayout_2">
           <item>
            <widget class="QGroupBox" name="groupBox_2">
             <property name="title">
              <string>GroupBox</string>
             </property>
             <layout class="QHBoxLayout" name="horizontalLayout">
              <item>
               <widget class="QPushButton" name="Clickme">
                <property name="text">
                 <string>ckl</string>
                </property>
               </widget>
              </item>
              <item>
               <widget class="QPushButton" name="pushButton">
                <property name="text">
                 <string>PushButton</string>
                </property>
               </widget>
              </item>
              <item>
               <widget class="QPushButton" name="pushButton_4">
                <property name="text">
                 <string>PushButton</string>
                </property>
               </widget>
              </item>
             </layout>
            </widget>
           </item>
           <item>
            <layout class="QHBoxLayout" name="horizontalLayoutfv"/>
           </item>
           <item>
            <widget class="QLabel" name="label_4">
             <property name="text">
              <string>change here</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="ClickableLabels" name="label_5">
             <property name="text">
              <string>click here</string>
             </property>
            </widget>
           </item>
          </layout>
         </widget>
        </item>
       </layout>
      </widget>
     </widget>
     <customwidgets>
      <customwidget>
       <class>ClickableLabels</class>
       <extends>QLabel</extends>
       <header location="global">secondform.h</header>
      </customwidget>
     </customwidgets>
     <resources/>
     <connections/>
    </ui>
    
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
    
        QApplication a(argc, argv);
    
    //    ClickableLabels sender;
    //    SecondForm receiver;
    
    //    QObject::connect(&sender, SIGNAL(SentFromSecondForm()), &receiver, SLOT(ReceivedFromSecondForm()));
    
    
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    Thanks

    Pl45m4P 1 Reply Last reply 23 Nov 2019, 16:35
    0
    • U Ucn_
      23 Nov 2019, 21:36

      @Christian-Ehrlicher I would like to emit from the ClickableLabels class. How do I achieve that? I'm kind of lost.

      C Offline
      C Offline
      Cobra91151
      wrote on 23 Nov 2019, 22:30 last edited by Cobra91151
      #8

      @Ucn_

      Hello!

      I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.

      clickablelabel.h

      #ifndef CLICKABLELABEL_H
      #define CLICKABLELABEL_H
      
      #include <QWidget>
      #include <QLabel>
      #include <QEvent>
      
      class ClickableLabel : public QLabel
      {
          Q_OBJECT
      public:
          using QLabel::QLabel;
      
      signals:
          void clicked();
      
      protected:
          void mousePressEvent(QMouseEvent *event) override;
      };
      
      #endif // CLICKABLELABEL_H
      

      clickablelabel.cpp

      #include "clickablelabel.h"
      
      void ClickableLabel::mousePressEvent(QMouseEvent *event)
      {
          emit clicked();
          QLabel::mousePressEvent(event);
      }
      

      test.h

      #ifndef TEST_H
      #define TEST_H
      
      #include <QDialog>
      #include <QVBoxLayout>
      #include "clickablelabel.h"
      
      namespace Ui {
      class Test;
      }
      
      class Test : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Test(QWidget *parent = nullptr);
          ~Test();
      
      private:
          Ui::Test *ui;
      };
      
      #endif // TEST_H
      

      test.cpp

      #include "test.h"
      #include "ui_test.h"
      
      Test::Test(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Test)
      {
          ui->setupUi(this);
          setFixedSize(600, 500);
          setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
          ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this);
          connect(testLabel, &ClickableLabel::clicked, [testLabel]() {
             testLabel->setText("Some text from mouse click");
          });
          ui->pushButton->setText("Test button");
          connect(ui->pushButton, &QPushButton::clicked, [testLabel]() {
             testLabel->setText("Some text from button click");
          });
      
          QVBoxLayout *testLayout = new QVBoxLayout();
          testLayout->setAlignment(Qt::AlignCenter);
          testLayout->addWidget(testLabel);
          testLayout->addWidget(ui->pushButton);
          setLayout(testLayout);
      }
      
      Test::~Test()
      {
          delete ui;
      }
      

      Screenshot:

      ClickableLabel.gif

      Happy coding!

      U 2 Replies Last reply 23 Nov 2019, 23:11
      4
      • U Ucn_
        23 Nov 2019, 14:56

        I have two classes in my second ui. I'm trying to connect signal and slot emitted in another class on QLabel click event (I promoted this Label) so I can update QLabel->setText("Some texts") from other class. However it does not work, I have been trying to make it sometimes it will run, but the QLabel->setText("Some texts"); won't update on the ui. Here is my code:

        mainwindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        #include "secondform.h"
        #include <QMainWindow>
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        
        private slots:
            void on_pushButton_clicked();
        
        private:
            SecondForm *captureWig;
            Ui::MainWindow *ui;
        };
        
        #endif // MAINWINDOW_H
        

        mainwindow.cpp

        
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
        
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        
        
        void MainWindow::on_pushButton_clicked()
        {
        
            captureWig = new SecondForm(this);
            QVBoxLayout *sendWig = new QVBoxLayout;
            sendWig = captureWig->getTreeto();
        
            QWidget *w = new QWidget();
            w->setLayout(sendWig);
        
            ui->tabWidget->addTab(w, "Newtab");
        }
        

        secondform.h

        
        #ifndef SECONDFORM_H
        #define SECONDFORM_H
        
        #include <QObject>
        #include <QWidget>
        #include <QtWidgets>
        
        class ClickableLabels;
        
        namespace Ui {
        class SecondForm;
        }
        
        class SecondForm : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit SecondForm(QWidget *parent = nullptr);
            ~SecondForm();
            QVBoxLayout *getTreeto();
        
        signals:
        void send_fromMain();
        
        private slots:
            void on_Clickme_clicked();
            void ReceivedFromSecondForm();
        
        private:
            ClickableLabels *upLabText;
            QPushButton *button_first;
            Ui::SecondForm *ui;
        };
        
        
        class ClickableLabels : public QLabel {
        
            Q_OBJECT
        
        public:
            explicit ClickableLabels(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
            ~ClickableLabels();
        
        signals:
            void clicked();
            void SentFromSecondForm();
        
        protected:
            void mousePressEvent(QMouseEvent* event);
        
        private:
        
        };
        
        #endif // SECONDFORM_H
        

        secondform.cpp

        
        #include "secondform.h"
        #include "ui_secondform.h"
        
        SecondForm::SecondForm(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::SecondForm)
        {
            ui->setupUi(this);
        
            upLabText = new ClickableLabels(this);
        
            QObject::connect (upLabText, SIGNAL (SentFromSecondForm()), this, SLOT(ReceivedFromSecondForm()));
        }
        
        void SecondForm::ReceivedFromSecondForm()
        {
            ui->label_4->setText("Received");
            qDebug () << "Emitted: ";
        }
        
        QVBoxLayout *SecondForm::getTreeto(){
        
            return ui->verticalLayout_6;
        }
        
        ClickableLabels::ClickableLabels(QWidget* parent, Qt::WindowFlags f)
            : QLabel(parent) {
        
        }
        
        ClickableLabels::~ClickableLabels() {}
        
        void ClickableLabels::mousePressEvent(QMouseEvent* ) {
        
            emit SentFromSecondForm();
            emit clicked();
            qDebug() << "QLabel clicked clicked";
        
        }
        
        SecondForm::~SecondForm()
        {
            delete ui;
        }
        
        void SecondForm::on_Clickme_clicked()
        {
            ui->label_4->setText("label clicked");
        }
        

        mainwindow.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>657</width>
            <height>663</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralWidget">
           <layout class="QVBoxLayout" name="verticalLayout">
            <item>
             <widget class="QTabWidget" name="tabWidget"/>
            </item>
            <item>
             <widget class="QPushButton" name="pushButton">
              <property name="text">
               <string>add</string>
              </property>
             </widget>
            </item>
           </layout>
          </widget>
          <widget class="QMenuBar" name="menuBar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>657</width>
             <height>21</height>
            </rect>
           </property>
          </widget>
          <widget class="QToolBar" name="mainToolBar">
           <attribute name="toolBarArea">
            <enum>TopToolBarArea</enum>
           </attribute>
           <attribute name="toolBarBreak">
            <bool>false</bool>
           </attribute>
          </widget>
          <widget class="QStatusBar" name="statusBar"/>
         </widget>
         <layoutdefault spacing="6" margin="11"/>
         <resources/>
         <connections/>
        </ui>
        
        

        secondform.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>SecondForm</class>
         <widget class="QWidget" name="SecondForm">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>765</width>
            <height>508</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>Form</string>
          </property>
          <widget class="QWidget" name="verticalLayoutWidget_2">
           <property name="geometry">
            <rect>
             <x>10</x>
             <y>30</y>
             <width>279</width>
             <height>137</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_6">
            <item>
             <widget class="QGroupBox" name="groupBox">
              <property name="title">
               <string>GroupBox</string>
              </property>
              <layout class="QVBoxLayout" name="verticalLayout_2">
               <item>
                <widget class="QGroupBox" name="groupBox_2">
                 <property name="title">
                  <string>GroupBox</string>
                 </property>
                 <layout class="QHBoxLayout" name="horizontalLayout">
                  <item>
                   <widget class="QPushButton" name="Clickme">
                    <property name="text">
                     <string>ckl</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QPushButton" name="pushButton">
                    <property name="text">
                     <string>PushButton</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QPushButton" name="pushButton_4">
                    <property name="text">
                     <string>PushButton</string>
                    </property>
                   </widget>
                  </item>
                 </layout>
                </widget>
               </item>
               <item>
                <layout class="QHBoxLayout" name="horizontalLayoutfv"/>
               </item>
               <item>
                <widget class="QLabel" name="label_4">
                 <property name="text">
                  <string>change here</string>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="ClickableLabels" name="label_5">
                 <property name="text">
                  <string>click here</string>
                 </property>
                </widget>
               </item>
              </layout>
             </widget>
            </item>
           </layout>
          </widget>
         </widget>
         <customwidgets>
          <customwidget>
           <class>ClickableLabels</class>
           <extends>QLabel</extends>
           <header location="global">secondform.h</header>
          </customwidget>
         </customwidgets>
         <resources/>
         <connections/>
        </ui>
        
        

        main.cpp

        #include "mainwindow.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
        
            QApplication a(argc, argv);
        
        //    ClickableLabels sender;
        //    SecondForm receiver;
        
        //    QObject::connect(&sender, SIGNAL(SentFromSecondForm()), &receiver, SLOT(ReceivedFromSecondForm()));
        
        
            MainWindow w;
            w.show();
        
            return a.exec();
        }
        
        

        Thanks

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on 23 Nov 2019, 16:35 last edited by Pl45m4
        #2

        @Ucn_ said in Signal and slot from other class not working:

        QObject::connect (upLabText, SIGNAL (SentFromSecondForm() ()), this, SLOT(ReceivedFromSecondForm()));

        From just looking at your code: There's one "()" too much in your connection (signal part).

        Move your class ClickableLabels to its own header file and try again.


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

        ~E. W. Dijkstra

        U 1 Reply Last reply 23 Nov 2019, 17:01
        2
        • Pl45m4P Pl45m4
          23 Nov 2019, 16:35

          @Ucn_ said in Signal and slot from other class not working:

          QObject::connect (upLabText, SIGNAL (SentFromSecondForm() ()), this, SLOT(ReceivedFromSecondForm()));

          From just looking at your code: There's one "()" too much in your connection (signal part).

          Move your class ClickableLabels to its own header file and try again.

          U Offline
          U Offline
          Ucn_
          wrote on 23 Nov 2019, 17:01 last edited by Ucn_
          #3

          @Pl45m4 The "()" was an error while copying, I don't have extra "()" in my actual code. About the QLabel, I click it then emit the signal, where I want to update another QLabel text. The QLabel I click, I promoted because QLabel doesn't have On Clicked attribute. So, I used that custom event for QLabel clicking.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 23 Nov 2019, 17:41 last edited by Christian Ehrlicher
            #4

            QObject::connect() returns a boolean to indicate if the connect was successful. Also during runtime you will get a warning when the connect is not valid. To avoid this we suggest to use the new signal slot syntax.
            Do you actually see your debug output from ClickableLabels::mousePressEvent() ? I would guess no. At least not from the one you create to do the connection from since this is not the one from the ui file...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • U Offline
              U Offline
              Ucn_
              wrote on 23 Nov 2019, 20:59 last edited by
              #5

              The signal and slot connection I commented on the main.cpp fires, but it does not update the Qlabel and it does not emit on button click.

              C 1 Reply Last reply 23 Nov 2019, 21:31
              0
              • U Ucn_
                23 Nov 2019, 20:59

                The signal and slot connection I commented on the main.cpp fires, but it does not update the Qlabel and it does not emit on button click.

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 23 Nov 2019, 21:31 last edited by
                #6

                @Ucn_ But your current code can not work as I already explained - you are connecting to another instance of ClickableLabels than the one you're displaying.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                U 1 Reply Last reply 23 Nov 2019, 21:36
                0
                • C Christian Ehrlicher
                  23 Nov 2019, 21:31

                  @Ucn_ But your current code can not work as I already explained - you are connecting to another instance of ClickableLabels than the one you're displaying.

                  U Offline
                  U Offline
                  Ucn_
                  wrote on 23 Nov 2019, 21:36 last edited by
                  #7

                  @Christian-Ehrlicher I would like to emit from the ClickableLabels class. How do I achieve that? I'm kind of lost.

                  C 1 Reply Last reply 23 Nov 2019, 22:30
                  0
                  • U Ucn_
                    23 Nov 2019, 21:36

                    @Christian-Ehrlicher I would like to emit from the ClickableLabels class. How do I achieve that? I'm kind of lost.

                    C Offline
                    C Offline
                    Cobra91151
                    wrote on 23 Nov 2019, 22:30 last edited by Cobra91151
                    #8

                    @Ucn_

                    Hello!

                    I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.

                    clickablelabel.h

                    #ifndef CLICKABLELABEL_H
                    #define CLICKABLELABEL_H
                    
                    #include <QWidget>
                    #include <QLabel>
                    #include <QEvent>
                    
                    class ClickableLabel : public QLabel
                    {
                        Q_OBJECT
                    public:
                        using QLabel::QLabel;
                    
                    signals:
                        void clicked();
                    
                    protected:
                        void mousePressEvent(QMouseEvent *event) override;
                    };
                    
                    #endif // CLICKABLELABEL_H
                    

                    clickablelabel.cpp

                    #include "clickablelabel.h"
                    
                    void ClickableLabel::mousePressEvent(QMouseEvent *event)
                    {
                        emit clicked();
                        QLabel::mousePressEvent(event);
                    }
                    

                    test.h

                    #ifndef TEST_H
                    #define TEST_H
                    
                    #include <QDialog>
                    #include <QVBoxLayout>
                    #include "clickablelabel.h"
                    
                    namespace Ui {
                    class Test;
                    }
                    
                    class Test : public QDialog
                    {
                        Q_OBJECT
                    
                    public:
                        explicit Test(QWidget *parent = nullptr);
                        ~Test();
                    
                    private:
                        Ui::Test *ui;
                    };
                    
                    #endif // TEST_H
                    

                    test.cpp

                    #include "test.h"
                    #include "ui_test.h"
                    
                    Test::Test(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::Test)
                    {
                        ui->setupUi(this);
                        setFixedSize(600, 500);
                        setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
                        ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this);
                        connect(testLabel, &ClickableLabel::clicked, [testLabel]() {
                           testLabel->setText("Some text from mouse click");
                        });
                        ui->pushButton->setText("Test button");
                        connect(ui->pushButton, &QPushButton::clicked, [testLabel]() {
                           testLabel->setText("Some text from button click");
                        });
                    
                        QVBoxLayout *testLayout = new QVBoxLayout();
                        testLayout->setAlignment(Qt::AlignCenter);
                        testLayout->addWidget(testLabel);
                        testLayout->addWidget(ui->pushButton);
                        setLayout(testLayout);
                    }
                    
                    Test::~Test()
                    {
                        delete ui;
                    }
                    

                    Screenshot:

                    ClickableLabel.gif

                    Happy coding!

                    U 2 Replies Last reply 23 Nov 2019, 23:11
                    4
                    • C Cobra91151
                      23 Nov 2019, 22:30

                      @Ucn_

                      Hello!

                      I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.

                      clickablelabel.h

                      #ifndef CLICKABLELABEL_H
                      #define CLICKABLELABEL_H
                      
                      #include <QWidget>
                      #include <QLabel>
                      #include <QEvent>
                      
                      class ClickableLabel : public QLabel
                      {
                          Q_OBJECT
                      public:
                          using QLabel::QLabel;
                      
                      signals:
                          void clicked();
                      
                      protected:
                          void mousePressEvent(QMouseEvent *event) override;
                      };
                      
                      #endif // CLICKABLELABEL_H
                      

                      clickablelabel.cpp

                      #include "clickablelabel.h"
                      
                      void ClickableLabel::mousePressEvent(QMouseEvent *event)
                      {
                          emit clicked();
                          QLabel::mousePressEvent(event);
                      }
                      

                      test.h

                      #ifndef TEST_H
                      #define TEST_H
                      
                      #include <QDialog>
                      #include <QVBoxLayout>
                      #include "clickablelabel.h"
                      
                      namespace Ui {
                      class Test;
                      }
                      
                      class Test : public QDialog
                      {
                          Q_OBJECT
                      
                      public:
                          explicit Test(QWidget *parent = nullptr);
                          ~Test();
                      
                      private:
                          Ui::Test *ui;
                      };
                      
                      #endif // TEST_H
                      

                      test.cpp

                      #include "test.h"
                      #include "ui_test.h"
                      
                      Test::Test(QWidget *parent) :
                          QDialog(parent),
                          ui(new Ui::Test)
                      {
                          ui->setupUi(this);
                          setFixedSize(600, 500);
                          setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
                          ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this);
                          connect(testLabel, &ClickableLabel::clicked, [testLabel]() {
                             testLabel->setText("Some text from mouse click");
                          });
                          ui->pushButton->setText("Test button");
                          connect(ui->pushButton, &QPushButton::clicked, [testLabel]() {
                             testLabel->setText("Some text from button click");
                          });
                      
                          QVBoxLayout *testLayout = new QVBoxLayout();
                          testLayout->setAlignment(Qt::AlignCenter);
                          testLayout->addWidget(testLabel);
                          testLayout->addWidget(ui->pushButton);
                          setLayout(testLayout);
                      }
                      
                      Test::~Test()
                      {
                          delete ui;
                      }
                      

                      Screenshot:

                      ClickableLabel.gif

                      Happy coding!

                      U Offline
                      U Offline
                      Ucn_
                      wrote on 23 Nov 2019, 23:11 last edited by Ucn_
                      #9

                      @Cobra91151 Thanks, I will give it a try.

                      1 Reply Last reply
                      0
                      • C Cobra91151
                        23 Nov 2019, 22:30

                        @Ucn_

                        Hello!

                        I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.

                        clickablelabel.h

                        #ifndef CLICKABLELABEL_H
                        #define CLICKABLELABEL_H
                        
                        #include <QWidget>
                        #include <QLabel>
                        #include <QEvent>
                        
                        class ClickableLabel : public QLabel
                        {
                            Q_OBJECT
                        public:
                            using QLabel::QLabel;
                        
                        signals:
                            void clicked();
                        
                        protected:
                            void mousePressEvent(QMouseEvent *event) override;
                        };
                        
                        #endif // CLICKABLELABEL_H
                        

                        clickablelabel.cpp

                        #include "clickablelabel.h"
                        
                        void ClickableLabel::mousePressEvent(QMouseEvent *event)
                        {
                            emit clicked();
                            QLabel::mousePressEvent(event);
                        }
                        

                        test.h

                        #ifndef TEST_H
                        #define TEST_H
                        
                        #include <QDialog>
                        #include <QVBoxLayout>
                        #include "clickablelabel.h"
                        
                        namespace Ui {
                        class Test;
                        }
                        
                        class Test : public QDialog
                        {
                            Q_OBJECT
                        
                        public:
                            explicit Test(QWidget *parent = nullptr);
                            ~Test();
                        
                        private:
                            Ui::Test *ui;
                        };
                        
                        #endif // TEST_H
                        

                        test.cpp

                        #include "test.h"
                        #include "ui_test.h"
                        
                        Test::Test(QWidget *parent) :
                            QDialog(parent),
                            ui(new Ui::Test)
                        {
                            ui->setupUi(this);
                            setFixedSize(600, 500);
                            setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
                            ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this);
                            connect(testLabel, &ClickableLabel::clicked, [testLabel]() {
                               testLabel->setText("Some text from mouse click");
                            });
                            ui->pushButton->setText("Test button");
                            connect(ui->pushButton, &QPushButton::clicked, [testLabel]() {
                               testLabel->setText("Some text from button click");
                            });
                        
                            QVBoxLayout *testLayout = new QVBoxLayout();
                            testLayout->setAlignment(Qt::AlignCenter);
                            testLayout->addWidget(testLabel);
                            testLayout->addWidget(ui->pushButton);
                            setLayout(testLayout);
                        }
                        
                        Test::~Test()
                        {
                            delete ui;
                        }
                        

                        Screenshot:

                        ClickableLabel.gif

                        Happy coding!

                        U Offline
                        U Offline
                        Ucn_
                        wrote on 24 Nov 2019, 05:57 last edited by Ucn_
                        #10

                        @Cobra91151 said in Signal and slot from other class not working:

                        connect(ui->pushButton, &QPushButton::clicked, testLabel {
                        testLabel->setText("Some text from button click");
                        });

                        The example works, thank. But, is there a way I can pass functions or widgets created in designer to the lambda capture list? Let's say I want to run a function after QLabel click or change a specific label.

                        mrjjM 1 Reply Last reply 24 Nov 2019, 07:12
                        0
                        • U Ucn_
                          24 Nov 2019, 05:57

                          @Cobra91151 said in Signal and slot from other class not working:

                          connect(ui->pushButton, &QPushButton::clicked, testLabel {
                          testLabel->setText("Some text from button click");
                          });

                          The example works, thank. But, is there a way I can pass functions or widgets created in designer to the lambda capture list? Let's say I want to run a function after QLabel click or change a specific label.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 24 Nov 2019, 07:12 last edited by
                          #11

                          @Ucn_

                          Hi
                          You can capture "this" to allow to use both
                          UI->xxx and member functions

                           connect(ui->pushButton, &QPushButton::clicked, [this]() {
                              ui->xxxxx;
                              });
                          
                          U 1 Reply Last reply 24 Nov 2019, 21:32
                          4
                          • mrjjM mrjj
                            24 Nov 2019, 07:12

                            @Ucn_

                            Hi
                            You can capture "this" to allow to use both
                            UI->xxx and member functions

                             connect(ui->pushButton, &QPushButton::clicked, [this]() {
                                ui->xxxxx;
                                });
                            
                            U Offline
                            U Offline
                            Ucn_
                            wrote on 24 Nov 2019, 21:32 last edited by
                            #12

                            @mrjj Thanks, It works. however, I don't understand why it works in one example and in the other says

                            'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const': cannot convert argument 1 from 'QLabel *' to 'const ClickableLabels *'
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 24 Nov 2019, 21:36 last edited by
                              #13

                              Because you are using a pointer to a QLabel as first parameter and a signal from your ClickableLabel as second parameter.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              U 1 Reply Last reply 24 Nov 2019, 22:11
                              2
                              • SGaistS SGaist
                                24 Nov 2019, 21:36

                                Because you are using a pointer to a QLabel as first parameter and a signal from your ClickableLabel as second parameter.

                                U Offline
                                U Offline
                                Ucn_
                                wrote on 24 Nov 2019, 22:11 last edited by
                                #14

                                @SGaist what is the viable solution. Cast?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 24 Nov 2019, 22:13 last edited by
                                  #15

                                  Ensure your original parameter is a ClickableLabel instance.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  U 1 Reply Last reply 24 Nov 2019, 22:39
                                  1
                                  • SGaistS SGaist
                                    24 Nov 2019, 22:13

                                    Ensure your original parameter is a ClickableLabel instance.

                                    U Offline
                                    U Offline
                                    Ucn_
                                    wrote on 24 Nov 2019, 22:39 last edited by
                                    #16

                                    @SGaist Thanks, I promoted QLabel widget in designer to ClickableLabel and it's working. Is it the correct way?

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 24 Nov 2019, 22:42 last edited by
                                      #17

                                      Since you are using Designer, yes.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      U 1 Reply Last reply 24 Nov 2019, 22:43
                                      1
                                      • SGaistS SGaist
                                        24 Nov 2019, 22:42

                                        Since you are using Designer, yes.

                                        U Offline
                                        U Offline
                                        Ucn_
                                        wrote on 24 Nov 2019, 22:43 last edited by
                                        #18

                                        @SGaist Thanks.

                                        1 Reply Last reply
                                        0
                                        • U Offline
                                          U Offline
                                          Ucn_
                                          wrote on 25 Nov 2019, 10:54 last edited by Ucn_
                                          #19

                                          I know I marked this as solved, and thanks to everyone who helped me understand what I was missing. I'm able to make it work when is widget to widget or promoted with with custom events. However when there is nothing triggered or clicked like a button I'm not able to connect. For example. Function to function, if one function from another class is executed, connect and execute another function in another class. Where the signal is the first executed function and slot is the function executed after the first was triggered. Thanks

                                          Pl45m4P 1 Reply Last reply 25 Nov 2019, 12:50
                                          0
                                          • U Ucn_
                                            25 Nov 2019, 10:54

                                            I know I marked this as solved, and thanks to everyone who helped me understand what I was missing. I'm able to make it work when is widget to widget or promoted with with custom events. However when there is nothing triggered or clicked like a button I'm not able to connect. For example. Function to function, if one function from another class is executed, connect and execute another function in another class. Where the signal is the first executed function and slot is the function executed after the first was triggered. Thanks

                                            Pl45m4P Offline
                                            Pl45m4P Offline
                                            Pl45m4
                                            wrote on 25 Nov 2019, 12:50 last edited by
                                            #20

                                            @Ucn_

                                            Set up the connect in your constructor and emit the signal, you connect to, in your function A to execute function B in your second class.


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

                                            ~E. W. Dijkstra

                                            U 1 Reply Last reply 25 Nov 2019, 13:27
                                            0

                                            9/29

                                            23 Nov 2019, 23:11

                                            20 unread
                                            • Login

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