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. QDebug output in a QTextEdit
QtWS25 Last Chance

QDebug output in a QTextEdit

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 8.7k 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.
  • F Offline
    F Offline
    fouffa89
    wrote on last edited by
    #1

    I'm new in Qt and I need to display the Qdebug output for the hole application into a QTextEdit , I found @customMessageHandler and @qInstallMessageHandler but the problem is that @customMessageHandler is static so I can't redirect my output to QTextEdit from this function, please help me if you have any idea.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to DevNet,

      You might implement a singleton class that contains a pointer to a QTextEdit and use it to write your output:

      @
      class LoggerSingleton {
      // Add all missing constructor etc... to have singleton

      private:
      QPointer<QTextEdit> _textEdit;
      };

      LoggerSingleton::setTextEdit(QTextEdit *textEdit)
      {
      _textEdit = textEdit;
      }
      LoggerSingleton::writeMessage(const QString& message)
      {
      if (_textEdit) {
      _textEdit->append(message);
      }
      }
      @

      This is just a brief "mockup" of the idea. I'll let you implement the singleton class properly

      Don't forget to put proper locking when access the QTextEdit.

      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
      • F Offline
        F Offline
        fouffa89
        wrote on last edited by
        #3

        Thanks for this reply , but how can I put locking? I never use locking before .
        Thanks again!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Have a look at the threading documentation. Basically you have to use a mutex each time you want to access the _textEdit since it's shared.

          If you only want to show the debug message, i'd suggest to also make it read only.

          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
          • F Offline
            F Offline
            fouffa89
            wrote on last edited by
            #5

            Thanks a lot , so helpful!

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fouffa89
              wrote on last edited by
              #6

              I just finished the implementation of the LoggerSingleton proposed by SGaist and it works fine but I have just one problem:
              for qDebug messages that I wrote my self they are not displayed in real time .I'm missing something??

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fouffa89
                wrote on last edited by
                #7

                this is the code for now:
                @#include<Presentation/LoggerSingleton.h>

                bool LoggerSingleton::instanceFlag = false;
                LoggerSingleton* LoggerSingleton::single = NULL;

                LoggerSingleton::LoggerSingleton()
                {
                }

                LoggerSingleton *LoggerSingleton::getInstance()
                {
                if(! instanceFlag)
                {
                single = new LoggerSingleton();
                instanceFlag = true;
                return single;
                }
                else
                {
                return single;
                }
                }

                LoggerSingleton::~LoggerSingleton()
                {
                instanceFlag = false;
                }

                void LoggerSingleton::setTextEdit(QTextEdit *textEdit)
                {
                _textEdit = textEdit;
                }

                void LoggerSingleton::writeMessage(const QString &message)
                {
                if (_textEdit) {
                _textEdit->append(message);
                }
                }
                @

                and
                @
                void HomePage::customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
                {
                QString txt;
                switch (type) {
                case QtDebugMsg:
                txt = QString("Debug: %1").arg(msg);
                break;

                case QtWarningMsg:
                    txt = QString("Warning: %1").arg(msg);;
                break;
                case QtCriticalMsg:
                    txt = QString("Critical: %1").arg(msg);
                break;
                case QtFatalMsg:
                    txt = QString("Fatal: %1").arg(msg);
                    abort();
                }
                
                logger=LoggerSingleton::getInstance();
                

                //Set my QTextEdit
                logger->setTextEdit(log);
                //Write the message to QTextEdit
                logger->writeMessage(txt);
                }
                @
                in the constructor of the HomePage
                @ qInstallMessageHandler(customMessageHandler); @

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Your implementation could be simpler:

                  @
                  LoggerSingleton *LoggerSingleton::getInstance()
                  {
                  if(!_single) {
                  _single = new LoggerSingleton();
                  }
                  return _single
                  }@

                  There's no need for a flag.

                  Don't forget to add locking when accessing the QTextEdit

                  Without knowing your program, it's difficult to say why they don't appear "real time". Messages comes from various sources (thinking threads), your main thread (AKA gui thread) might be busy doing something else etc...

                  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
                  • T Offline
                    T Offline
                    tchoninho
                    wrote on last edited by
                    #9

                    I recommend you change the global variables for static one.

                    [code]
                    LoggerSingleton *LoggerSingleton::getInstance()
                    {
                    static LoggerSingleton *_single = LoggerSingleton();
                    if(!_single) {
                    _single = new LoggerSingleton();
                    }
                    return _single
                    }

                    [/code]

                    Or my favorite, return a reference rather than a pointer.

                    [code]
                    LoggerSingleton & LoggerSingleton::getInstance()
                    {
                    static LoggerSingleton _single;
                    return _single
                    }
                    [/code]

                    Computer Scientist
                    Belo Horizonte - Brasil
                    C++ Development
                    Qt Development

                    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