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 3.6k 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
    22 Apr 2022, 12:01

    @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

    C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 22 Apr 2022, 12:03 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 22 Apr 2022, 12:15
    0
    • C Christian Ehrlicher
      22 Apr 2022, 12:03

      @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 22 Apr 2022, 12:15 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
      
      J 2 Replies Last reply 22 Apr 2022, 12:21
      0
      • G Gaetano03
        22 Apr 2022, 12:15

        @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
        
        J Offline
        J Offline
        JonB
        wrote on 22 Apr 2022, 12:21 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 22 Apr 2022, 12:29
        1
        • G Gaetano03
          22 Apr 2022, 12:15

          @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
          
          J Offline
          J Offline
          JonB
          wrote on 22 Apr 2022, 12:25 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
          • J JonB
            22 Apr 2022, 12:21

            @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 22 Apr 2022, 12:29 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

            J 1 Reply Last reply 22 Apr 2022, 12:34
            0
            • G Gaetano03
              22 Apr 2022, 12:29

              @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

              J Offline
              J Offline
              JonB
              wrote on 22 Apr 2022, 12:34 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 22 Apr 2022, 13:29
              3
              • J JonB
                22 Apr 2022, 12:34

                @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 22 Apr 2022, 13:29 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

                21/27

                22 Apr 2022, 12:03

                • Login

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