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. Proper way to update widget from other class
Forum Updated to NodeBB v4.3 + New Features

Proper way to update widget from other class

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 3.1k Views 2 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.
  • Christian EhrlicherC Christian Ehrlicher

    Since you can't use signals and slots you can for example set a callback function in Counter which gets executed every time you want to send a signal from Counter to Qt. This callback function can then emit a Qt signal.

    H Offline
    H Offline
    hills
    wrote on last edited by
    #3

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

    Since you can't use signals and slots you can for example set a callback function in Counter which gets executed every time you want to send a signal from Counter to Qt. This callback function can then emit a Qt signal.

    Can you give me an example? I'm not clearly understand. I thought it's a common usage: i have isolated class with form and UI stuff and some other classes that connected with that form thru some method's. And i thought slots and signals implemented that routine.
    That what i'm trying to do is weird? And if it's does can you have a better workaround?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

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

      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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #5

        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
        0
        • Christian EhrlicherC Christian Ehrlicher

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

          H Offline
          H Offline
          hills
          wrote on last edited by hills
          #6

          @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
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #7

            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
            2
            • mrjjM mrjj

              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 Offline
              H Offline
              hills
              wrote on last edited by
              #8

              @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 EhrlicherC mrjjM 2 Replies Last reply
              0
              • H hills

                @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 EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @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
                1
                • H hills

                  @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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @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
                  1
                  • Christian EhrlicherC Christian Ehrlicher

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

                    H Offline
                    H Offline
                    hills
                    wrote on last edited by hills
                    #11

                    @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
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      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
                      2
                      • Christian EhrlicherC Christian Ehrlicher

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

                        H Offline
                        H Offline
                        hills
                        wrote on last edited by
                        #13

                        @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 EhrlicherC 1 Reply Last reply
                        0
                        • H hills

                          @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 EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

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

                          • Login

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