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

Signal and slot from other class not working

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 7 Posters 4.4k 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.
  • Cobra91151C Cobra91151

    @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 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
    0
    • U Ucn_

      @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 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
      4
      • mrjjM mrjj

        @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 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 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
          2
          • SGaistS SGaist

            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 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 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
              1
              • SGaistS SGaist

                Ensure your original parameter is a ClickableLabel instance.

                U Offline
                U Offline
                Ucn_
                wrote on 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 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
                  1
                  • SGaistS SGaist

                    Since you are using Designer, yes.

                    U Offline
                    U Offline
                    Ucn_
                    wrote on last edited by
                    #18

                    @SGaist Thanks.

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      Ucn_
                      wrote on 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
                      0
                      • U Ucn_

                        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 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
                        0
                        • Pl45m4P Pl45m4

                          @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.

                          U Offline
                          U Offline
                          Ucn_
                          wrote on last edited by
                          #21

                          @Pl45m4

                          This only works when I setup in main.cpp and when it fires in won't update anything in the function except print. When I set up in the constructor and place the emit in the function it does not fire,

                          QObject::connect(&timer, SIGNAL(printPerSecond()), &w, SLOT(anotherFuncion()));
                          
                          emit printPerSecond()
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • U Ucn_

                            @Pl45m4

                            This only works when I setup in main.cpp and when it fires in won't update anything in the function except print. When I set up in the constructor and place the emit in the function it does not fire,

                            QObject::connect(&timer, SIGNAL(printPerSecond()), &w, SLOT(anotherFuncion()));
                            
                            emit printPerSecond()
                            
                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #22

                            @Ucn_ What is timer in your connect?
                            Is there a warning at runtime that connect failed?
                            You should use the new connect syntax to get compiler error instead of runtime warning if your connect call is wrong.

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

                            U 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Ucn_ What is timer in your connect?
                              Is there a warning at runtime that connect failed?
                              You should use the new connect syntax to get compiler error instead of runtime warning if your connect call is wrong.

                              U Offline
                              U Offline
                              Ucn_
                              wrote on last edited by
                              #23

                              @jsulm It does not give any warning. I tried different connects, it just won't execute the function.

                              jsulmJ 1 Reply Last reply
                              0
                              • U Offline
                                U Offline
                                Ucn_
                                wrote on last edited by
                                #24
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • U Ucn_

                                  @jsulm It does not give any warning. I tried different connects, it just won't execute the function.

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #25

                                  @Ucn_ Without more code I can't tell you what the problem is. One possibility: "timer" is not the same instance from where you emit the signal.

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

                                  U 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @Ucn_ Without more code I can't tell you what the problem is. One possibility: "timer" is not the same instance from where you emit the signal.

                                    U Offline
                                    U Offline
                                    Ucn_
                                    wrote on last edited by
                                    #26

                                    @jsulm I have as follow in my MainWindow constructor:

                                        Someclass send;
                                        QObject::connect(&send, SIGNAL(valueChanged()), SLOT(someFunction()));
                                    

                                    I have this signal in Someclass

                                    signals:
                                      void valueChanged();
                                    

                                    And this Slot in my MainWindow

                                    private slots:
                                        void someFunction();
                                    

                                    then I emit from a function

                                    void Someclass::anotherfunction(){
                                    
                                        emit valueChanged();
                                    }
                                    
                                    jsulmJ 1 Reply Last reply
                                    0
                                    • U Ucn_

                                      @jsulm I have as follow in my MainWindow constructor:

                                          Someclass send;
                                          QObject::connect(&send, SIGNAL(valueChanged()), SLOT(someFunction()));
                                      

                                      I have this signal in Someclass

                                      signals:
                                        void valueChanged();
                                      

                                      And this Slot in my MainWindow

                                      private slots:
                                          void someFunction();
                                      

                                      then I emit from a function

                                      void Someclass::anotherfunction(){
                                      
                                          emit valueChanged();
                                      }
                                      
                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #27

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

                                      Someclass send;
                                      QObject::connect(&send, SIGNAL(valueChanged()), SLOT(someFunction()));

                                      Here send is a local variable and is destroyed as soon as it goes out of scope!

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

                                      U 1 Reply Last reply
                                      1
                                      • jsulmJ jsulm

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

                                        Someclass send;
                                        QObject::connect(&send, SIGNAL(valueChanged()), SLOT(someFunction()));

                                        Here send is a local variable and is destroyed as soon as it goes out of scope!

                                        U Offline
                                        U Offline
                                        Ucn_
                                        wrote on last edited by Ucn_
                                        #28

                                        @jsulm If I declare Someclass *send = new Someclass(); it gives error:

                                        mainwindow.cpp:18:14: error: no matching member function for call to 'connect'
                                        qobject.h:467:41: note: candidate function not viable: no known conversion from 'Someclass **' to 'const QObject *' for 1st argument; remove &
                                        qobject.h:264:13: note: candidate template ignored: requirement 'int(QtPrivate::FunctionPointer<const char *>::ArgumentCount) >= 0' was not satisfied [with Func1 = const char *, Func2 = const char *]
                                        qobject.h:304:13: note: candidate template ignored: substitution failure [with Func1 = const char *, Func2 = const char *]: no type named 'Object' in 'QtPrivate::FunctionPointer<const char *>'
                                        qobject.h:232:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
                                        qobject.h:273:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
                                        qobject.h:312:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
                                        qobject.h:212:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
                                        qobject.h:215:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
                                        

                                        I handle keypress event in Someclass, but is just an example. I want to emit valueChanged(); when a specific key is pressed or when a function is run, even though I did remove & to:

                                        Someclass *send = new Someclass()
                                        QObject::connect(send, SIGNAL(valueChanged()), SLOT(someFunction()));
                                        

                                        Still doesn't fire.

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • U Ucn_

                                          @jsulm If I declare Someclass *send = new Someclass(); it gives error:

                                          mainwindow.cpp:18:14: error: no matching member function for call to 'connect'
                                          qobject.h:467:41: note: candidate function not viable: no known conversion from 'Someclass **' to 'const QObject *' for 1st argument; remove &
                                          qobject.h:264:13: note: candidate template ignored: requirement 'int(QtPrivate::FunctionPointer<const char *>::ArgumentCount) >= 0' was not satisfied [with Func1 = const char *, Func2 = const char *]
                                          qobject.h:304:13: note: candidate template ignored: substitution failure [with Func1 = const char *, Func2 = const char *]: no type named 'Object' in 'QtPrivate::FunctionPointer<const char *>'
                                          qobject.h:232:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
                                          qobject.h:273:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
                                          qobject.h:312:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
                                          qobject.h:212:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
                                          qobject.h:215:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
                                          

                                          I handle keypress event in Someclass, but is just an example. I want to emit valueChanged(); when a specific key is pressed or when a function is run, even though I did remove & to:

                                          Someclass *send = new Someclass()
                                          QObject::connect(send, SIGNAL(valueChanged()), SLOT(someFunction()));
                                          

                                          Still doesn't fire.

                                          jsulmJ Online
                                          jsulmJ Online
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #29

                                          @Ucn_ Don't you have send as class member?
                                          Regarding error: simply remove & in front of send in connect as send is now already a pointer.

                                          QObject::connect(send, SIGNAL(valueChanged()), SLOT(someFunction()));
                                          

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

                                          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