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
Forum Updated to NodeBB v4.3 + New Features

Log issues

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 4 Posters 5.4k 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I have created the log program and want to collect some logs and display it in QTextBrowser without saving to the file. The problem is I get error:

    log.obj:-1: error: LNK2001: unresolved external symbol "private: static class QTextBrowser * Log::textBrowser" (?textBrowser@Log@@0PAVQTextBrowser@@A)
    
    static QTextBrowser *textBrowser;
    static void writeToLog(QString log)  {
           textBrowser->append(log);
    }
    

    How to fix this issue? Thanks.

    K 1 Reply Last reply
    0
    • Cobra91151C Cobra91151

      Hi! I have created the log program and want to collect some logs and display it in QTextBrowser without saving to the file. The problem is I get error:

      log.obj:-1: error: LNK2001: unresolved external symbol "private: static class QTextBrowser * Log::textBrowser" (?textBrowser@Log@@0PAVQTextBrowser@@A)
      
      static QTextBrowser *textBrowser;
      static void writeToLog(QString log)  {
             textBrowser->append(log);
      }
      

      How to fix this issue? Thanks.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Cobra91151

      Did you somewhere assign something to textBrowser?

      The initialization is missing.

      Vote the answer(s) that helped you to solve your issue(s)

      Cobra91151C 1 Reply Last reply
      0
      • M Offline
        M Offline
        mawh1960
        wrote on last edited by
        #3

        Have you included widgets in your .pro file?

        QT += widgets
        
        Cobra91151C 1 Reply Last reply
        0
        • M mawh1960

          Have you included widgets in your .pro file?

          QT += widgets
          
          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #4

          @mawh1960

          Yes. I have it included in .pro file.

          1 Reply Last reply
          0
          • K koahnig

            @Cobra91151

            Did you somewhere assign something to textBrowser?

            The initialization is missing.

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

            @koahnig

            In the Log class constructor I have initialized - textBrowser = new QTextBrowser(this);

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

              The most common time I run into this error is when I declare a static variable in my header file, but forget to create it in my code. For example, in your code above you have:
              static QTextBrowser *textBrowser;
              You've probably got that as a member of your Log class defined in your .h file, so in your .cpp file, you need:
              QTextBrowser *Log::textBrowser;

              Chris Hennes, Pioneer Library System

              Cobra91151C 1 Reply Last reply
              0
              • Chris HennesC Chris Hennes

                The most common time I run into this error is when I declare a static variable in my header file, but forget to create it in my code. For example, in your code above you have:
                static QTextBrowser *textBrowser;
                You've probably got that as a member of your Log class defined in your .h file, so in your .cpp file, you need:
                QTextBrowser *Log::textBrowser;

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

                @Chris-Hennes

                I have changed code in .cpp file (in constructor):

                QTextBrowser *Log::textBrowser;
                textBrowser->installEventFilter(this);
                connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);
                

                but I get error: C2655: 'Log::textBrowser': definition or redeclaration illegal in current scope?

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

                  The creation of the QTextBrowser does not belong in your constructor, it must be a standalone line in your .cpp file.

                  Chris Hennes, Pioneer Library System

                  Cobra91151C 1 Reply Last reply
                  1
                  • Chris HennesC Chris Hennes

                    The creation of the QTextBrowser does not belong in your constructor, it must be a standalone line in your .cpp file.

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

                    @Chris-Hennes

                    Thanks. I have fixed it but when I use Log::writeToLog("Some data"); in for example Test.cpp, the app crashes.

                    Code:
                    Log.h
                    static void writeToLog(QString logMessage);

                    Log.cpp

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

                    The same crash when I open Log also exists. Any suggestions? Thanks.

                    Update:
                    textBrowser->installEventFilter(this);

                    It crashes on this line:

                    connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);

                    1 Reply Last reply
                    0
                    • 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

                                          • Login

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