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 emitted but Slot won't work
Qt 6.11 is out! See what's new in the release blog

Signal emitted but Slot won't work

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 6.6k Views 1 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.
  • C Offline
    C Offline
    checkers
    wrote on last edited by
    #1

    Hello, I am relatively a new to be using Qt and studying Signals and Slot recently.

    My first try is that when QString variable is set via a certain function outside of UI, append it to the QPlainTextEdit on UI.

    So I write the code like

    customconnectobject.h
    class CustomConnectObject : public QObject
    {
    Q_OBJECT

    signals:
    void mySignal(QString);

    public:
    void setString(QString msg)
    {
    emit mySignal(msg);
    }

    mainwindow.cpp
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    CustomConnectObject * cco =new CustomConnectObject();
    connect(cco,&CustomConnectObject::mySignal,ui->plainTextOutput,&QPlainTextEdit::appendPlainText)

    otherfunction.cpp
    CustomConnectObject * co =new CustomConnectObject();
    co->setString("test");

    I expected whenever I call co->setString, I could get the text updated in the QPlainTextEdit object but nothing happens.

    I checked if connect is made well and it was ok.

    Please help me out

    E 1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      Just guess, perhaps that signal should have public scope?

      C 1 Reply Last reply
      2
      • C checkers

        Hello, I am relatively a new to be using Qt and studying Signals and Slot recently.

        My first try is that when QString variable is set via a certain function outside of UI, append it to the QPlainTextEdit on UI.

        So I write the code like

        customconnectobject.h
        class CustomConnectObject : public QObject
        {
        Q_OBJECT

        signals:
        void mySignal(QString);

        public:
        void setString(QString msg)
        {
        emit mySignal(msg);
        }

        mainwindow.cpp
        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        CustomConnectObject * cco =new CustomConnectObject();
        connect(cco,&CustomConnectObject::mySignal,ui->plainTextOutput,&QPlainTextEdit::appendPlainText)

        otherfunction.cpp
        CustomConnectObject * co =new CustomConnectObject();
        co->setString("test");

        I expected whenever I call co->setString, I could get the text updated in the QPlainTextEdit object but nothing happens.

        I checked if connect is made well and it was ok.

        Please help me out

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

        @checkers You create two different objects of class CustomConnectObject (badly named btw, because it's a class, not an object). When in the otherfunction.cpp you call co->setString that object's signal isn't connected.

        C 1 Reply Last reply
        1
        • K kenchan

          Just guess, perhaps that signal should have public scope?

          C Offline
          C Offline
          checkers
          wrote on last edited by
          #4

          @kenchan Thank you for a replay, did you mean I should rewrite it

          public:
          void setString(QString msg)

          instead of

          signals:
          void setString(QString msg)

          but it didn't work

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            wrote on last edited by
            #5

            Signal shud be emitted from cco object not co object. That is the issue here.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            https://www.pthinks.com

            1 Reply Last reply
            2
            • K Offline
              K Offline
              kenchan
              wrote on last edited by
              #6

              No, more like
              public signals:
              or
              public
              signals:

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kenchan
                wrote on last edited by
                #7

                @dheerendra yes, that too :-)

                1 Reply Last reply
                1
                • E Eeli K

                  @checkers You create two different objects of class CustomConnectObject (badly named btw, because it's a class, not an object). When in the otherfunction.cpp you call co->setString that object's signal isn't connected.

                  C Offline
                  C Offline
                  checkers
                  wrote on last edited by
                  #8

                  @Eeli-K
                  Thank you for a reply. So apparently the object 'co' in otherfunction.cpp and 'cco' in mainwindow.cpp are different.

                  Then should I call 'cco' in mainwindow.cpp?

                  Sorry I know I am asking basic things but I wanted to understand Signals and Slot better.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kenchan
                    wrote on last edited by
                    #9

                    Two different instances of the same type of object.
                    coo is a pointer so you have to use it where you can "see" it. You connected that objects signal to the slot so yes you must use the same pointer to the object to make it emit the signal. as @Eeli-K and @dheerendra have said.

                    C 1 Reply Last reply
                    0
                    • K kenchan

                      Two different instances of the same type of object.
                      coo is a pointer so you have to use it where you can "see" it. You connected that objects signal to the slot so yes you must use the same pointer to the object to make it emit the signal. as @Eeli-K and @dheerendra have said.

                      C Offline
                      C Offline
                      checkers
                      wrote on last edited by
                      #10

                      @kenchan
                      Thanks a lot.
                      It is true that I created cco in mainwindow.cpp and connected it in mainwindow.cpp

                      but is there a way to 'see' cco in otherfunction.cpp ?

                      E 1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kenchan
                        wrote on last edited by kenchan
                        #11

                        where is other function relative to main window and cco?
                        you could:
                        pass the pointer to the other function directly
                        or
                        make cco a member of mainwindow and make mainwidow visible to other function and have a getter function in mainwindow
                        ... etc.

                        C 1 Reply Last reply
                        0
                        • C checkers

                          @kenchan
                          Thanks a lot.
                          It is true that I created cco in mainwindow.cpp and connected it in mainwindow.cpp

                          but is there a way to 'see' cco in otherfunction.cpp ?

                          E Offline
                          E Offline
                          Eeli K
                          wrote on last edited by
                          #12

                          @checkers This is quite basic thing in C++, you clearly need practice in it and unfortunately there's no other way to learn it than practice, fail first and then succeed :) Eventually you will understand how to govern the lifetime of objects, where and when to create and destroy them and how objects can have access to or reach other objects. In C++ you can for example pass pointers and references as arguments to functions and store objects as member data. Kenchan already gave tips for this situation. It's difficult to say more without seeing the whole code.

                          C 1 Reply Last reply
                          0
                          • K kenchan

                            where is other function relative to main window and cco?
                            you could:
                            pass the pointer to the other function directly
                            or
                            make cco a member of mainwindow and make mainwidow visible to other function and have a getter function in mainwindow
                            ... etc.

                            C Offline
                            C Offline
                            checkers
                            wrote on last edited by checkers
                            #13

                            @kenchan
                            I call some functions in otherfunction.cpp from mainwindow.cpp by including otherfunction.h
                            I wanted to call cco from those functions in otherfunction.cpp like

                            otherfunction.cpp
                            void testfunction()
                            {
                            cco->setString("test");
                            }
                            mainwindow.cpp
                            void MainWindow::on_pushButton_clicked()
                            {
                            testfunction();
                            }

                            I will try passing cco directly to my function in otherfunction.cpp

                            thank you very much

                            jsulmJ 1 Reply Last reply
                            0
                            • E Eeli K

                              @checkers This is quite basic thing in C++, you clearly need practice in it and unfortunately there's no other way to learn it than practice, fail first and then succeed :) Eventually you will understand how to govern the lifetime of objects, where and when to create and destroy them and how objects can have access to or reach other objects. In C++ you can for example pass pointers and references as arguments to functions and store objects as member data. Kenchan already gave tips for this situation. It's difficult to say more without seeing the whole code.

                              C Offline
                              C Offline
                              checkers
                              wrote on last edited by
                              #14

                              @Eeli-K
                              Thank you. I know I need some practice in here as I am a novice to C++ also.
                              I will try to solve current situation.

                              1 Reply Last reply
                              0
                              • C checkers

                                @kenchan
                                I call some functions in otherfunction.cpp from mainwindow.cpp by including otherfunction.h
                                I wanted to call cco from those functions in otherfunction.cpp like

                                otherfunction.cpp
                                void testfunction()
                                {
                                cco->setString("test");
                                }
                                mainwindow.cpp
                                void MainWindow::on_pushButton_clicked()
                                {
                                testfunction();
                                }

                                I will try passing cco directly to my function in otherfunction.cpp

                                thank you very much

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @checkers Why does otherfunction.cpp need access to cco if cco is part of main window? Doing it like this is bad design. This testfunction could just return the string and you call cco then in mainwindow:

                                QString testfunction()
                                {
                                    return "test";
                                }
                                
                                void MainWindow::on_pushButton_clicked()
                                {
                                    cco->setString(testfunction());
                                }
                                

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

                                C 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @checkers Why does otherfunction.cpp need access to cco if cco is part of main window? Doing it like this is bad design. This testfunction could just return the string and you call cco then in mainwindow:

                                  QString testfunction()
                                  {
                                      return "test";
                                  }
                                  
                                  void MainWindow::on_pushButton_clicked()
                                  {
                                      cco->setString(testfunction());
                                  }
                                  
                                  C Offline
                                  C Offline
                                  checkers
                                  wrote on last edited by checkers
                                  #16

                                  @jsulm
                                  Thank you for advice
                                  You sound correct

                                  I may be confused the concept of Signals and Slot that Signals must be called from other place than Slot

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • C checkers

                                    @jsulm
                                    Thank you for advice
                                    You sound correct

                                    I may be confused the concept of Signals and Slot that Signals must be called from other place than Slot

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @checkers Well, slots are usually defined in other classes than signals. That is the whole idea behind signals/slots: that an object can notify other objects via signals. The object which emits a signal usually does not know who is connected to this signal nor does it care - it just emits the signal, who ever wants receives it.

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

                                    1 Reply Last reply
                                    2

                                    • Login

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