Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Add to Qt GUI logger functionality
Forum Updated to NodeBB v4.3 + New Features

Add to Qt GUI logger functionality

Scheduled Pinned Locked Moved Unsolved Qt 6
27 Posts 4 Posters 4.4k 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.
  • G Gaetano03

    @JonB said in Add to Qt GUI logger functionality:

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();

    You mean on the "a" object in the application main? How would this be possible in such a case?

    my understanding of what you are pointing out is that even if g_loggerCallback has global visibility, not sure what happens when it points to a QObject function (the call in the external logger library does not know what to do)?

    What about making both the function pointer and the log function static? Maybe I am telling blasfemies, sorry for that

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #13

    @Gaetano03
    That a object in main() --- or rather, the QApplication object --- can be accessed from anywhere in a Qt program (because it's a singleton) via qApp macro or qGuiApp macro or QCoreApplication *QCoreApplication::instance(). That means you can use those anywhere in a Qt application, without worrying about the a variable in main().

    You could use that to do something like create your own signal (must subclass QAppplication, but that's no problem), which you could connect() to a slot somewhere to update your QPlainText widget. Then even a static C function which is called from the function pointer we discussed can get at a Qt object to emit a signal. Or, you could skip signal/slot if you have, by whatever means, a global pointer to your desired QPlainText, which the static log function can use to set its text directly. Just be careful with coding, but it should be doable!

    not sure what happens when it points to a QObject function (the call in the external logger library does not know what to do)?

    All the external library knows is the outside world has set g_loggerCallback to the address of some function which takes a std:string (or whatever) as a parameter. That is all it needs to know to be able to call it. What happens at the other end in the function pointed to --- log(std::string message), which is in your Qt program, not the library --- is the other end's problem, so far as the library is concerned. [Which is why it would be nice if it worked and didn't crash :) ]

    G 1 Reply Last reply
    1
    • JonBJ JonB

      @Gaetano03
      That a object in main() --- or rather, the QApplication object --- can be accessed from anywhere in a Qt program (because it's a singleton) via qApp macro or qGuiApp macro or QCoreApplication *QCoreApplication::instance(). That means you can use those anywhere in a Qt application, without worrying about the a variable in main().

      You could use that to do something like create your own signal (must subclass QAppplication, but that's no problem), which you could connect() to a slot somewhere to update your QPlainText widget. Then even a static C function which is called from the function pointer we discussed can get at a Qt object to emit a signal. Or, you could skip signal/slot if you have, by whatever means, a global pointer to your desired QPlainText, which the static log function can use to set its text directly. Just be careful with coding, but it should be doable!

      not sure what happens when it points to a QObject function (the call in the external logger library does not know what to do)?

      All the external library knows is the outside world has set g_loggerCallback to the address of some function which takes a std:string (or whatever) as a parameter. That is all it needs to know to be able to call it. What happens at the other end in the function pointed to --- log(std::string message), which is in your Qt program, not the library --- is the other end's problem, so far as the library is concerned. [Which is why it would be nice if it worked and didn't crash :) ]

      G Offline
      G Offline
      Gaetano03
      wrote on last edited by Gaetano03
      #14

      @JonB Getting a little bit of shakes now XD

      "You could use that to do something like create your own signal (must subclass QAppplication, but that's no problem), which you could connect() to a slot somewhere to update your QPlainText widget. "

      The other problem would be that signal and slot should be static as well? can you use connect on static objects?

      If I understand well, your first option would be to create a class that inherits Qapplication and get an instance of the Qapplication. Here I define my own signal (static?) which I then emits thorugh the log function defined above (still static). This should be catched by the QPlainText widget, but at this point how? Should I make the slot static as well? Actually it seems I am getting real troubles when I try to use connect with static methods.

      Will investigate also second option

      Thanks a lot

      JonBJ 1 Reply Last reply
      0
      • G Gaetano03

        @JonB Getting a little bit of shakes now XD

        "You could use that to do something like create your own signal (must subclass QAppplication, but that's no problem), which you could connect() to a slot somewhere to update your QPlainText widget. "

        The other problem would be that signal and slot should be static as well? can you use connect on static objects?

        If I understand well, your first option would be to create a class that inherits Qapplication and get an instance of the Qapplication. Here I define my own signal (static?) which I then emits thorugh the log function defined above (still static). This should be catched by the QPlainText widget, but at this point how? Should I make the slot static as well? Actually it seems I am getting real troubles when I try to use connect with static methods.

        Will investigate also second option

        Thanks a lot

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #15

        @Gaetano03
        The only thing which needs to be "static" is the global, non-member function void log(std::string message), to which you set g_loggerCallback = log;, so that the library can invoke it via g_loggerCallback(message).

        That can go something like:

        void log(std::string message)
        {
            emit qApp()->libraryLogged(message);
            // or probably
            MyApplication *myApp = qobject_cast<MyApplication *>(qApp());
            emit myApp->libraryLogged(message);
        }
        

        You would need to subclass and use QApplication something like:

        class MyApplication : public QApplication
        {
        signals:
            void libraryLogged(std::string message);
        }
        

        Now it remains to have done the connect(). Somewhere in your code which can "see" wherever you create the QTextEdit *logmessageTextEdit = new QTextEdit, or afterwards, and can see the class MyApplication definition, needs e.g.

        MyApplication *myApp = qobject_cast<MyApplication *>(qApp());
        connect(myApp(), &MyApplication::libraryLogged, this, &ThisClass::onLibraryLogged);
        
        class ThisClass
        {
        slots:
            void onLibraryLogged(std::string message) { this->logmessageTextEdit->append(message); }
        }
        

        Alternatively, if you don't want to use signals/slots, keep some global reference to the logmessageTextEdit pointer to the QTextEdit and just go

        void log(std::string message)
        {
            if (g_logmessageTextEdit != nullptr)
                g_logmessageTextEdit->append(message);
        }
        
        G 1 Reply Last reply
        0
        • JonBJ JonB

          @Gaetano03
          The only thing which needs to be "static" is the global, non-member function void log(std::string message), to which you set g_loggerCallback = log;, so that the library can invoke it via g_loggerCallback(message).

          That can go something like:

          void log(std::string message)
          {
              emit qApp()->libraryLogged(message);
              // or probably
              MyApplication *myApp = qobject_cast<MyApplication *>(qApp());
              emit myApp->libraryLogged(message);
          }
          

          You would need to subclass and use QApplication something like:

          class MyApplication : public QApplication
          {
          signals:
              void libraryLogged(std::string message);
          }
          

          Now it remains to have done the connect(). Somewhere in your code which can "see" wherever you create the QTextEdit *logmessageTextEdit = new QTextEdit, or afterwards, and can see the class MyApplication definition, needs e.g.

          MyApplication *myApp = qobject_cast<MyApplication *>(qApp());
          connect(myApp(), &MyApplication::libraryLogged, this, &ThisClass::onLibraryLogged);
          
          class ThisClass
          {
          slots:
              void onLibraryLogged(std::string message) { this->logmessageTextEdit->append(message); }
          }
          

          Alternatively, if you don't want to use signals/slots, keep some global reference to the logmessageTextEdit pointer to the QTextEdit and just go

          void log(std::string message)
          {
              if (g_logmessageTextEdit != nullptr)
                  g_logmessageTextEdit->append(message);
          }
          
          G Offline
          G Offline
          Gaetano03
          wrote on last edited by
          #16

          @JonB said in Add to Qt GUI logger functionality:

          Not sure where to declare the log function though

          At this point what I did is

          I created a QApplication subclass in a .h file (integer_interface.h) which looks like this

          class integer_interface : public QApplication
          {
              Q_OBJECT
          public:
              integer_interface(int &argc, char *argv[]) {}; // empty constructor
          
          signals:
              void libraryLogged(std::string message);
          
          };
          

          then in my logger_widget class, logger_widget.h (the one that contains the QPlainTextWidget) I have the following class definition:

          namespace Ui {
          class logger_widget; 
          }
          
          class logger_widget : public QWidget
          {
              Q_OBJECT
          
          public:
              explicit logger_widget(QWidget *parent = nullptr);
              ~logger_widget();
          
          signals:
          
          
          public slots:
              void onLibraryLogged(std::string message);
          
          
          private:
              Ui::logger_widget *ui;
          
              integer::logger::errors err_init;
              integer::logger::integer_logger& logger = integer::logger::integer_logger::get_instance();
              integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
          
          };
          

          and a declaration of the log static function outside the class

          static void log(std::string message); 
          

          In the logger_widget constructor I have the following two lines to define the connection

          integer::logger::g_loggerCallback = log;
          connect(myApp,&integer_interface::libraryLogged,this,&logger_widget::onLibraryLogged);
          

          while log and onLibraryLogged implementations look like this

          void logger_widget::onLibraryLogged(std::string message)
          {
              ui->plainTextEdit_logger->appendPlainText(QString::fromStdString(message));
          }
          
          void log(std::string message)
          {
              integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
              emit myApp->libraryLogged(message);
          }
          

          constructor and function implementations are obviously in the corresponding logger_widget.cpp file

          It compiles fine, but I get a runtime error "abort()" and the following message

          qt.core.qobject.connect: QObject::connect(integer_interface, logger_widget): invalid nullptr parameter
          
          JonBJ 1 Reply Last reply
          0
          • G Gaetano03

            @JonB said in Add to Qt GUI logger functionality:

            Not sure where to declare the log function though

            At this point what I did is

            I created a QApplication subclass in a .h file (integer_interface.h) which looks like this

            class integer_interface : public QApplication
            {
                Q_OBJECT
            public:
                integer_interface(int &argc, char *argv[]) {}; // empty constructor
            
            signals:
                void libraryLogged(std::string message);
            
            };
            

            then in my logger_widget class, logger_widget.h (the one that contains the QPlainTextWidget) I have the following class definition:

            namespace Ui {
            class logger_widget; 
            }
            
            class logger_widget : public QWidget
            {
                Q_OBJECT
            
            public:
                explicit logger_widget(QWidget *parent = nullptr);
                ~logger_widget();
            
            signals:
            
            
            public slots:
                void onLibraryLogged(std::string message);
            
            
            private:
                Ui::logger_widget *ui;
            
                integer::logger::errors err_init;
                integer::logger::integer_logger& logger = integer::logger::integer_logger::get_instance();
                integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
            
            };
            

            and a declaration of the log static function outside the class

            static void log(std::string message); 
            

            In the logger_widget constructor I have the following two lines to define the connection

            integer::logger::g_loggerCallback = log;
            connect(myApp,&integer_interface::libraryLogged,this,&logger_widget::onLibraryLogged);
            

            while log and onLibraryLogged implementations look like this

            void logger_widget::onLibraryLogged(std::string message)
            {
                ui->plainTextEdit_logger->appendPlainText(QString::fromStdString(message));
            }
            
            void log(std::string message)
            {
                integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                emit myApp->libraryLogged(message);
            }
            

            constructor and function implementations are obviously in the corresponding logger_widget.cpp file

            It compiles fine, but I get a runtime error "abort()" and the following message

            qt.core.qobject.connect: QObject::connect(integer_interface, logger_widget): invalid nullptr parameter
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #17

            @Gaetano03
            So I assume that occurs at runtime when it hits

            connect(myApp,&integer_interface::libraryLogged,this,&logger_widget::onLibraryLogged);
            

            and indicates one of the two objects are nullptr. Check on line above with e.g.

            qDebug() << myApp << this;
            

            ?

            If myApp == nullptr, you did remember to create an integer_interface, not just a QApplication now, didn't you...??

            Everywhere you go integer_interface *myApp = qobject_cast<integer_interface *>(qApp); you might follow that with Q_ASSERT(myApp); to verify all is well at that instant.

            P.S.

            integer_interface(int &argc, char *argv[]) {}; // empty constructor

            Do you have integer_interface(int &argc, char *argv[]) : QApplication(argc, argv) somewhere?

            G JonBJ 2 Replies Last reply
            0
            • JonBJ JonB

              @Gaetano03
              So I assume that occurs at runtime when it hits

              connect(myApp,&integer_interface::libraryLogged,this,&logger_widget::onLibraryLogged);
              

              and indicates one of the two objects are nullptr. Check on line above with e.g.

              qDebug() << myApp << this;
              

              ?

              If myApp == nullptr, you did remember to create an integer_interface, not just a QApplication now, didn't you...??

              Everywhere you go integer_interface *myApp = qobject_cast<integer_interface *>(qApp); you might follow that with Q_ASSERT(myApp); to verify all is well at that instant.

              P.S.

              integer_interface(int &argc, char *argv[]) {}; // empty constructor

              Do you have integer_interface(int &argc, char *argv[]) : QApplication(argc, argv) somewhere?

              G Offline
              G Offline
              Gaetano03
              wrote on last edited by
              #18

              @JonB
              I think the problem might be where I define myApp is not correct? Which is at the moment into the logger_widget class

              when I qDebug as you said I get indeed the following value for myApp

              QObject(0x0) logger_widget(0x1e04f63bcd0, name="logger_widget")
              

              and the app crashes few seconds after it starts

              Should I declare the myApp out of any member class as I did for the log function?

              1 Reply Last reply
              0
              • JonBJ JonB

                @Gaetano03
                So I assume that occurs at runtime when it hits

                connect(myApp,&integer_interface::libraryLogged,this,&logger_widget::onLibraryLogged);
                

                and indicates one of the two objects are nullptr. Check on line above with e.g.

                qDebug() << myApp << this;
                

                ?

                If myApp == nullptr, you did remember to create an integer_interface, not just a QApplication now, didn't you...??

                Everywhere you go integer_interface *myApp = qobject_cast<integer_interface *>(qApp); you might follow that with Q_ASSERT(myApp); to verify all is well at that instant.

                P.S.

                integer_interface(int &argc, char *argv[]) {}; // empty constructor

                Do you have integer_interface(int &argc, char *argv[]) : QApplication(argc, argv) somewhere?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #19

                @Gaetano03

                @JonB said in Add to Qt GUI logger functionality:

                If myApp == nullptr, you did remember to create an integer_interface, not just a QApplication now, didn't you...??

                ??

                G 1 Reply Last reply
                0
                • JonBJ JonB

                  @Gaetano03

                  @JonB said in Add to Qt GUI logger functionality:

                  If myApp == nullptr, you did remember to create an integer_interface, not just a QApplication now, didn't you...??

                  ??

                  G Offline
                  G Offline
                  Gaetano03
                  wrote on last edited by
                  #20

                  @JonB

                  Do you have integer_interface(int &argc, char *argv[]) : QApplication(argc, argv) somewhere?

                  ye I have it in an integer_interface.cpp, looks like this

                  #include "integer_interface.h"
                  
                  integer_interface::integer_interface(int &argc, char *argv[])
                      : QApplication(argc, argv)
                  {
                  
                  }
                  

                  ??

                  I have it declared in my logger_widget.h class file (if you also see my previous reply), but was thinking maybe should be declared outside of the class?

                  Or maybe you meant something else from this

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • G Gaetano03

                    @JonB

                    Do you have integer_interface(int &argc, char *argv[]) : QApplication(argc, argv) somewhere?

                    ye I have it in an integer_interface.cpp, looks like this

                    #include "integer_interface.h"
                    
                    integer_interface::integer_interface(int &argc, char *argv[])
                        : QApplication(argc, argv)
                    {
                    
                    }
                    

                    ??

                    I have it declared in my logger_widget.h class file (if you also see my previous reply), but was thinking maybe should be declared outside of the class?

                    Or maybe you meant something else from this

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    @Gaetano03 said in Add to Qt GUI logger functionality:

                    I have it declared in my logger_widget.h class file (if you also see my previous reply), but was thinking maybe should be declared outside of the class?

                    You should also instantiate it somewhere.

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

                    G 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Gaetano03 said in Add to Qt GUI logger functionality:

                      I have it declared in my logger_widget.h class file (if you also see my previous reply), but was thinking maybe should be declared outside of the class?

                      You should also instantiate it somewhere.

                      G Offline
                      G Offline
                      Gaetano03
                      wrote on last edited by
                      #22

                      @Christian-Ehrlicher

                      I have it declared and instantiated both in the logger_widget.h class among the private members

                      private:
                          Ui::logger_widget *ui;
                      
                          integer::logger::errors err_init;
                          integer::logger::integer_logger& logger = integer::logger::integer_logger::get_instance();
                          integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                      

                      and in the static log function

                      void log(std::string message)
                      {
                          integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                          emit myApp->libraryLogged(message);
                      }
                      

                      as suggested by @JonB

                      PS
                      @JonB doing as you suggested as sanity check with the following lines

                          integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                          Q_ASSERT(myApp);
                      

                      gives a bunch of errors not sure I understand:
                      logger_widget.h:42:14: C++ requires a type specifier for all declarations

                      logger_widget.h:42:5: Expected ')'
                      0:0: :42:5: note: to match this '('
                      
                      logger_widget.h:42:14: Duplicate member 'myApp'
                      logger_widget.h:41:24: previous declaration is here
                      
                      JonBJ 2 Replies Last reply
                      0
                      • G Gaetano03

                        @Christian-Ehrlicher

                        I have it declared and instantiated both in the logger_widget.h class among the private members

                        private:
                            Ui::logger_widget *ui;
                        
                            integer::logger::errors err_init;
                            integer::logger::integer_logger& logger = integer::logger::integer_logger::get_instance();
                            integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                        

                        and in the static log function

                        void log(std::string message)
                        {
                            integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                            emit myApp->libraryLogged(message);
                        }
                        

                        as suggested by @JonB

                        PS
                        @JonB doing as you suggested as sanity check with the following lines

                            integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                            Q_ASSERT(myApp);
                        

                        gives a bunch of errors not sure I understand:
                        logger_widget.h:42:14: C++ requires a type specifier for all declarations

                        logger_widget.h:42:5: Expected ')'
                        0:0: :42:5: note: to match this '('
                        
                        logger_widget.h:42:14: Duplicate member 'myApp'
                        logger_widget.h:41:24: previous declaration is here
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #23

                        @Gaetano03
                        Look, can you please show your main() function now. I have a funny feeling your still have QApplication a(argc, argv); there, and what do you think you actually need instead of that?

                        G 1 Reply Last reply
                        1
                        • G Gaetano03

                          @Christian-Ehrlicher

                          I have it declared and instantiated both in the logger_widget.h class among the private members

                          private:
                              Ui::logger_widget *ui;
                          
                              integer::logger::errors err_init;
                              integer::logger::integer_logger& logger = integer::logger::integer_logger::get_instance();
                              integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                          

                          and in the static log function

                          void log(std::string message)
                          {
                              integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                              emit myApp->libraryLogged(message);
                          }
                          

                          as suggested by @JonB

                          PS
                          @JonB doing as you suggested as sanity check with the following lines

                              integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                              Q_ASSERT(myApp);
                          

                          gives a bunch of errors not sure I understand:
                          logger_widget.h:42:14: C++ requires a type specifier for all declarations

                          logger_widget.h:42:5: Expected ')'
                          0:0: :42:5: note: to match this '('
                          
                          logger_widget.h:42:14: Duplicate member 'myApp'
                          logger_widget.h:41:24: previous declaration is here
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #24

                          @Gaetano03 said in Add to Qt GUI logger functionality:

                          integer_interface *myApp = qobject_cast<integer_interface *>(qApp);
                          Q_ASSERT(myApp);
                          

                          If that is in a .h where you are declaring member variables in a class definition you cannot add arbitrary C++ statements to execute..... Put the qobject_cast<> in the constructor, where you can verify it.

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Gaetano03
                            Look, can you please show your main() function now. I have a funny feeling your still have QApplication a(argc, argv); there, and what do you think you actually need instead of that?

                            G Offline
                            G Offline
                            Gaetano03
                            wrote on last edited by
                            #25

                            @JonB
                            you have the right funny feeling, just realized how silly I was.

                            Everything works fine now and I get stuff printed on my QPlainText, at least for now XD.

                            Thanks a lot, really!

                            I really appreciate

                            JonBJ 1 Reply Last reply
                            0
                            • G Gaetano03

                              @JonB
                              you have the right funny feeling, just realized how silly I was.

                              Everything works fine now and I get stuff printed on my QPlainText, at least for now XD.

                              Thanks a lot, really!

                              I really appreciate

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #26

                              @Gaetano03
                              Yeah, do you get the point? You can subclass QApplication to add whatever you want, but then you must create a SubclassedApplication not the original base class QApplication to use it. Just like any other subclassing.

                              qApp() macro returns QApplication *-type pointer, for whatever (single) QApplication instance has been created. If you derived and created the derived instance, qApp won't return that derived type (though it will return the correct derived instance). So to call methods/access variables you defined in your subclass that's why we qobject_cast<SubclassedApplication *>(qApp).

                              G 1 Reply Last reply
                              3
                              • JonBJ JonB

                                @Gaetano03
                                Yeah, do you get the point? You can subclass QApplication to add whatever you want, but then you must create a SubclassedApplication not the original base class QApplication to use it. Just like any other subclassing.

                                qApp() macro returns QApplication *-type pointer, for whatever (single) QApplication instance has been created. If you derived and created the derived instance, qApp won't return that derived type (though it will return the correct derived instance). So to call methods/access variables you defined in your subclass that's why we qobject_cast<SubclassedApplication *>(qApp).

                                G Offline
                                G Offline
                                Gaetano03
                                wrote on last edited by
                                #27

                                @JonB
                                Yes I got the point, thanks also for this explanation!! That helps a lot understanding what I am doing behind the lines!!!

                                I have to admit that I worked so much on the library that I forgot for a while a main calling it and launching the app was even existing XD

                                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