Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Proper way to update widget from other class

    General and Desktop
    4
    14
    1740
    Loading More Posts
    • 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.
    • SGaist
      SGaist Lifetime Qt Champion last edited by

      Hi and welcome to devnet,

      If I may, adding a "connect" method to a QObject subclass is not really a good idea. You might want to consider a more accurate name.

      That said, you do not call said method in your code, hence "WOW" will never be shown.

      On a side note, signals should only be called from within the class that defines them.

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

      1 Reply Last reply Reply Quote 0
      • H
        hills @Christian Ehrlicher last edited by hills

        @SGaist said in Update widget from nonQT class (signal & slot problem):

        If I may, adding a "connect" method to a QObject subclass is not really a good idea. You might want to consider a more accurate name.

        Thanks!
        It just an example, for real: i have CLI-based class that has function's with many std::cout log messages inside and i want to translate them into input widgets .

        @Christian-Ehrlicher said in Update widget from nonQT class (signal & slot problem):

        @hills said in Update widget from nonQT class (signal & slot problem):

        I thought it's a common usage:

        Emitting a signal from a n object which is not derived from QObject is not common usage. So either derive from QObject or use a callback function.

        So, i trying to realize callback function, but it fails on QtWidgetsApplication1::UpdateText (error C2228: left of '.textEdit' must have class/struct/union).
        How to make this works?

        FORM H

        class QtWidgetsApplication1 : public QMainWindow {
            Q_OBJECT
            Ui::QtWidgetsApplication1Class ui; 
            static void updateText(const QString& text);
        public:
            explicit QtWidgetsApplication1(QWidget* parent = Q_NULLPTR);
        };
        

        FORM CPP

        QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent) : QMainWindow(parent) {
            ui.setupUi(this);
            Counter* c = new Counter();
            c->setCallbackFunc(updateText);
        }
        
        void QtWidgetsApplication1::updateText(const QString& text) {
            ui.textEdit->setText(text);                    // error C2228: 
            ui.plainTextEdit->appendPlainText(text);       //  left of '.textEdit' must have class/struct/union
        }
        

        Counter H

        class Counter : public QMainWindow {
        public:
            explicit Counter(QObject* parent = 0) {
                connect();
            }
            void setCallbackFunc(void (*func) (const QString& text));
            void connect();
        private:
            void (*callbackFunc)(const QString& text);
            Q_OBJECT;
        };
        

        Counter CPP

        void Counter::setCallbackFunc(void(*func)(const QString& text)) {
            callbackFunc = func;
        }
        
        void Counter::connect() {
            //QString text = "Maybe-maybe";
            //callbackFunc(text);
            callbackFunc("WOW");
        }
        
        1 Reply Last reply Reply Quote 0
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          Hi
          When you say nonQT class, we think of class that does not inherit QObject
          but in your code shown, it does ! `?

          so if the real issues were

          Counter c;
          connect(&c, Counter::sendText, this, QtWidgetsApplication1::updateText);

          only works in main.cpp,
          i was wondering if your actual issue was just the use of a local variable that will
          get deleted as soon as the function ends. ( the variable c has a short life span if in a function)

          So maybe the real fix would have been

          Counter * c = new Counter (this);
          connect(c, Counter::sendText, this, QtWidgetsApplication1::updateText);

          H 1 Reply Last reply Reply Quote 2
          • H
            hills @mrjj last edited by

            @mrjj said in Update widget from nonQT class (signal & slot problem):

            Hi
            When you say nonQT class, we think of class that does not inherit QObject
            but in your code shown, it does ! `?

            so if the real issues were

            Counter c;
            connect(&c, Counter::sendText, this, QtWidgetsApplication1::updateText);

            only works in main.cpp,
            i was wondering if your actual issue was just the use of a local variable that will
            get deleted as soon as the function ends. ( the variable c has a short life span if in a function)

            So maybe the real fix would have been

            Counter * c = new Counter (this);
            connect(c, Counter::sendText, this, QtWidgetsApplication1::updateText);

            Hello!
            Unfortunately it didn't work. Fail with error:
            error C3867: 'Counter::sendText': non-standard syntax; use '&' to create a pointer to member
            error C3867: 'QtWidgetsApplication1::updateText': non-standard syntax; use '&' to create a pointer to member
            And if i add &

            connect(c, &Counter::sendText, this, &QtWidgetsApplication1::updateText);
            

            It compiles without errors and warnings but nothing happened at text widget

            Christian Ehrlicher mrjj 2 Replies Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion @hills last edited by

              @hills said in Update widget from nonQT class (signal & slot problem):

              It compiles without errors and warnings but nothing happened at text widget

              Are you sure you call sendText() and updateText() is actually doing something? Please add some debug output and make sure the correct instance of Counter is connected.

              And please change the title of your question - it's irritating.

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

              H 1 Reply Last reply Reply Quote 1
              • mrjj
                mrjj Lifetime Qt Champion @hills last edited by

                @hills

                Hi. well, it has to have &, as we take the address of a member function.

                Also, make sure you mean to actually mean create a new Counter variable.
                The one you connect with - is the only one connected so if you have other Counters, you created, they
                might not been connected.
                That can explain why nothing happens if you have code in the slot that should set some text on the screen.

                1 Reply Last reply Reply Quote 1
                • H
                  hills @Christian Ehrlicher last edited by hills

                  @Christian-Ehrlicher said in Update widget from nonQT class (signal & slot problem):

                  @hills said in Update widget from nonQT class (signal & slot problem):

                  It compiles without errors and warnings but nothing happened at text widget

                  Are you sure you call sendText() and updateText() is actually doing something? Please add some debug output and make sure the correct instance of Counter is connected.

                  Yeah, i tested it in main.cpp - from main he works fine

                  And please change the title of your question - it's irritating.

                  No problem, that's better?

                  @mrjj hi! there is only one variable called Counter an it's within QtWidgetsApplication1.cpp

                  Here the full project, maybe this will clarify something
                  https://pastebin.com/h0NFXJrP - main.cpp
                  https://pastebin.com/YHnYZkwp - QtWidgetsApplication1.h
                  https://pastebin.com/Z45b5uWx - QtWidgetsApplication1.cpp
                  https://pastebin.com/c502CUjy - QtWidgetsApplication1.ui
                  https://pastebin.com/BfKCdUdr - test_class.h
                  https://pastebin.com/WU3ThMu3 - test_class.cpp

                  1 Reply Last reply Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion last edited by

                    Where do you call Counter::connect(). And no, calling it in the ctor will not work since the connection is done afterwards.

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

                    H 1 Reply Last reply Reply Quote 2
                    • H
                      hills @Christian Ehrlicher last edited by

                      @Christian-Ehrlicher said in Proper way to update widget from other class:

                      Where do you call Counter::connect(). And no, calling it in the ctor will not work since the connection is done afterwards.

                      Oh, sweet lord, i finally got something... I call the constructor and after that make connection between form and Counter class. That leads to the fact that all messages that were before the connection disappeared.
                      So i need to re-write my real code, coz i make stuff in the default constructor that show up some messages.
                      Thanks a lot @Christian-Ehrlicher!

                      Christian Ehrlicher 1 Reply Last reply Reply Quote 0
                      • Christian Ehrlicher
                        Christian Ehrlicher Lifetime Qt Champion @hills last edited by

                        @hills Glad you found the problem. Please mark the topic as solved then.

                        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 Reply Quote 0
                        • First post
                          Last post