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. Log issues
Qt 6.11 is out! See what's new in the release blog

Log issues

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 4 Posters 7.0k 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.
  • Chris HennesC Offline
    Chris HennesC Offline
    Chris Hennes
    wrote on last edited by
    #10

    Your static variable is probably uninitialized. It looks to me like you are trying to create something like a Singleton -- are you familiar with that design pattern? My guess is that you are trying to call writeToLog without first initializing the QTextBrowser. Remember that by using a static member function you do not have an object to work with, and therefore it's very possible that your class's constructor is never called.

    Chris Hennes, Pioneer Library System

    1 Reply Last reply
    0
    • Chris HennesC Offline
      Chris HennesC Offline
      Chris Hennes
      wrote on last edited by
      #11

      In fact, nowhere in any of the code you've entered here do you ever actually instantiate a QTextBrowser. Where is your allocation statement (e.g. textBrowser = new QTextBrowser;)?

      Chris Hennes, Pioneer Library System

      Cobra91151C 1 Reply Last reply
      0
      • Chris HennesC Chris Hennes

        In fact, nowhere in any of the code you've entered here do you ever actually instantiate a QTextBrowser. Where is your allocation statement (e.g. textBrowser = new QTextBrowser;)?

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #12

        @Chris-Hennes

        Thanks. Now it works but the same as it was without static, when I use Log::writeToLog("Some data"); to write to QTextBrowser in for example Test window and close it, then open Log window to check data, QTextBrowser is empty.

        To open Log I use:

        Log *appLog = new Log(this);
        appLog->show();
        
        1 Reply Last reply
        0
        • Chris HennesC Offline
          Chris HennesC Offline
          Chris Hennes
          wrote on last edited by
          #13

          How/where are you allocating the QTextBrowser?

          Chris Hennes, Pioneer Library System

          Cobra91151C 1 Reply Last reply
          0
          • Chris HennesC Chris Hennes

            How/where are you allocating the QTextBrowser?

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #14

            @Chris-Hennes

            I have initialized textBrowser = new QTextBrowser(); in constructor and in function below because constructor is not called when I call Log::writeToLog("Some data") in Test.cpp and app crashes.

            void Log::writeToLog(QString logMessage)
            {
                textBrowser = new QTextBrowser();
                textBrowser->append(logMessage);
            }
            

            But the result is the same (QTextBrowser is empty), I think because of two initialization.

            1 Reply Last reply
            0
            • Chris HennesC Offline
              Chris HennesC Offline
              Chris Hennes
              wrote on last edited by
              #15

              Definitely do not make a new QTextBrowser every time you call writeToLog, that defeats the purpose of making it static. You need to instantiate it one time in any given program run. Start by initializing it to null (or Q_NULLPTR), then in writeToLog check to see if it exists already. If it does not, then allocate it and set its parameters as appropriate, so something like:

              QTextBrowser *Log::textBrowser = null;
              
              ...
              
              void Log::writeToLog(QString logMessage)
              {
                  if (!textBrowser) {
                      textBrowser = new QTextBrowser;
                      textBrowser->installEventFilter(this);
                      connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);
                  }
                  textBrowser->append(logMessage);
              }

              Chris Hennes, Pioneer Library System

              Cobra91151C 1 Reply Last reply
              1
              • Chris HennesC Offline
                Chris HennesC Offline
                Chris Hennes
                wrote on last edited by
                #16

                (Note that my example code won't actually work as written because writeToLog is static, so you don't have a "this" pointer... those two lines will need to be in your constructor, along with appropriate checks to create textBrowser there if needed.)

                Chris Hennes, Pioneer Library System

                1 Reply Last reply
                0
                • Chris HennesC Chris Hennes

                  Definitely do not make a new QTextBrowser every time you call writeToLog, that defeats the purpose of making it static. You need to instantiate it one time in any given program run. Start by initializing it to null (or Q_NULLPTR), then in writeToLog check to see if it exists already. If it does not, then allocate it and set its parameters as appropriate, so something like:

                  QTextBrowser *Log::textBrowser = null;
                  
                  ...
                  
                  void Log::writeToLog(QString logMessage)
                  {
                      if (!textBrowser) {
                          textBrowser = new QTextBrowser;
                          textBrowser->installEventFilter(this);
                          connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);
                      }
                      textBrowser->append(logMessage);
                  }
                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by
                  #17

                  @Chris-Hennes

                  Thanks for code example. It now works. One more question, since Log::writeToLog(QString logMessage) is a static function I can't use this, I get error: log.cpp:116: error: C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers. So I need to get the instance of the Log class. Any ideas?

                  1 Reply Last reply
                  0
                  • Chris HennesC Offline
                    Chris HennesC Offline
                    Chris Hennes
                    wrote on last edited by
                    #18

                    Yes, remove those two lines from the if statement in writeToLog, but keep them in an identical if statement in your constructor.

                    Chris Hennes, Pioneer Library System

                    Cobra91151C 1 Reply Last reply
                    0
                    • Chris HennesC Chris Hennes

                      Yes, remove those two lines from the if statement in writeToLog, but keep them in an identical if statement in your constructor.

                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by Cobra91151
                      #19

                      @Chris-Hennes

                      I have added also check in constructor, because it crashes when open Log without adding anything to it.

                         if (!textBrowser) {
                              textBrowser = new QTextBrowser();
                              textBrowser->installEventFilter(this);
                              connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);
                          } else {
                              textBrowser->installEventFilter(this);
                              connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);
                          }
                      

                      Also I have notices that connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser); is not working. I have some buttons which is disabled and when new log data is added, those buttons should be enabled, but it still disabled.

                      void Log::checkLogTextBrowser()
                      {
                           if (textBrowser->document()->isEmpty()) {
                               saveLog->setEnabled(false);
                               saveLogAs->setEnabled(false);
                               openLog->setEnabled(false);
                               clearLog->setEnabled(false);
                               deleteLog->setEnabled(false);
                               sendLog->setEnabled(false);
                           } else {
                               saveLog->setEnabled(true);
                               saveLogAs->setEnabled(true);
                               openLog->setEnabled(false);
                               clearLog->setEnabled(true);
                               deleteLog->setEnabled(false);
                               sendLog->setEnabled(false);
                           }
                      }
                      

                      I can use some trigger as bool checkButtonsState to check when log is added and then set it true to enable buttons? But why connect is not working?

                      1 Reply Last reply
                      0
                      • Chris HennesC Offline
                        Chris HennesC Offline
                        Chris Hennes
                        wrote on last edited by
                        #20

                        Make sure to call checkLogTextBrowser() directly in your constructor as well, to handle anything that was logged before you opened it.

                        Chris Hennes, Pioneer Library System

                        Cobra91151C 1 Reply Last reply
                        1
                        • Chris HennesC Chris Hennes

                          Make sure to call checkLogTextBrowser() directly in your constructor as well, to handle anything that was logged before you opened it.

                          Cobra91151C Offline
                          Cobra91151C Offline
                          Cobra91151
                          wrote on last edited by
                          #21

                          @Chris-Hennes

                          Now everything works. Thank you.

                          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