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 Window Not Showing!
Qt 6.11 is out! See what's new in the release blog

Log Window Not Showing!

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 454 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.
  • Aviral 0A Offline
    Aviral 0A Offline
    Aviral 0
    wrote on last edited by
    #1

    Hi I am trying to create an application which displays recent 10 Logs from a file, and have checkboxes to filter the logs based on types,
    here is my code:

    logdisplay.cpp

    #include "logdisplay.h"
    #include <QFile>
    #include <QHBoxLayout>
    #include <QLabel>
    #include <QTextStream>
    #include <QTimer>
    #include <QVBoxLayout>
    
    LogDisplay::LogDisplay(QWidget *parent)
        : QWidget(parent)
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
        QHBoxLayout *checkBoxLayout = new QHBoxLayout;
        infoCheckBox = new QCheckBox("INFO");
        infoCheckBox->setChecked(true);
        checkBoxLayout->addWidget(infoCheckBox);
        warnCheckBox = new QCheckBox("WARN");
        warnCheckBox->setChecked(true);
        checkBoxLayout->addWidget(warnCheckBox);
        debugCheckBox = new QCheckBox("DEBUG");
        debugCheckBox->setChecked(true);
        checkBoxLayout->addWidget(debugCheckBox);
        mainLayout->addLayout(checkBoxLayout);
    
        readLogs();
    
        QTimer *timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &LogDisplay::refresh);
        timer->start(5000);
    }
    
    
    
    void LogDisplay::readLogs()
    {
        QFile file("home/arpitk/QT Projects/logtail1/sample.log");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
    
        QTextStream in(&file);
        QStringList logLines;
        while (!in.atEnd()) {
            logLines.append(in.readLine());
        }
    
        int logLinesCount = logLines.count();
        int startIndex = logLinesCount > 10 ? logLinesCount - 10 : 0;
    
        for (int i = startIndex; i < logLinesCount; i++) {
            QString message = logLines.at(i);
            QString level = message.left(5);
            if ((level == "INFO " && infoCheckBox->isChecked()) ||
                (level == "WARN " && warnCheckBox->isChecked()) ||
                (level == "DEBUG" && debugCheckBox->isChecked())) {
                mainLayout->addWidget(new QLabel(message));
            }
        }
    
        file.close();
    }
    
        void LogDisplay::refresh()
                {
                    QLayoutItem *child;
                    while ((child = mainLayout->takeAt(1)) != 0) {
                        delete child->widget();
                        delete child;
                    }
                    readLogs();
                }
        LogDisplay::~LogDisplay()
        {
        }
    
    

    main.cpp

    #include "logdisplay.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        LogDisplay w;
        w.show();
        return a.exec();
    }
    
    

    The code is executing and checkboxes are displaying but Log Window is not showing?
    Screenshot 2023-02-09 182317.png
    The log window is not showing, Please help!

    jsulmJ JonBJ 3 Replies Last reply
    0
    • Aviral 0A Aviral 0

      Hi I am trying to create an application which displays recent 10 Logs from a file, and have checkboxes to filter the logs based on types,
      here is my code:

      logdisplay.cpp

      #include "logdisplay.h"
      #include <QFile>
      #include <QHBoxLayout>
      #include <QLabel>
      #include <QTextStream>
      #include <QTimer>
      #include <QVBoxLayout>
      
      LogDisplay::LogDisplay(QWidget *parent)
          : QWidget(parent)
      {
          QVBoxLayout *mainLayout = new QVBoxLayout(this);
      
          QHBoxLayout *checkBoxLayout = new QHBoxLayout;
          infoCheckBox = new QCheckBox("INFO");
          infoCheckBox->setChecked(true);
          checkBoxLayout->addWidget(infoCheckBox);
          warnCheckBox = new QCheckBox("WARN");
          warnCheckBox->setChecked(true);
          checkBoxLayout->addWidget(warnCheckBox);
          debugCheckBox = new QCheckBox("DEBUG");
          debugCheckBox->setChecked(true);
          checkBoxLayout->addWidget(debugCheckBox);
          mainLayout->addLayout(checkBoxLayout);
      
          readLogs();
      
          QTimer *timer = new QTimer(this);
          connect(timer, &QTimer::timeout, this, &LogDisplay::refresh);
          timer->start(5000);
      }
      
      
      
      void LogDisplay::readLogs()
      {
          QFile file("home/arpitk/QT Projects/logtail1/sample.log");
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
              return;
      
          QTextStream in(&file);
          QStringList logLines;
          while (!in.atEnd()) {
              logLines.append(in.readLine());
          }
      
          int logLinesCount = logLines.count();
          int startIndex = logLinesCount > 10 ? logLinesCount - 10 : 0;
      
          for (int i = startIndex; i < logLinesCount; i++) {
              QString message = logLines.at(i);
              QString level = message.left(5);
              if ((level == "INFO " && infoCheckBox->isChecked()) ||
                  (level == "WARN " && warnCheckBox->isChecked()) ||
                  (level == "DEBUG" && debugCheckBox->isChecked())) {
                  mainLayout->addWidget(new QLabel(message));
              }
          }
      
          file.close();
      }
      
          void LogDisplay::refresh()
                  {
                      QLayoutItem *child;
                      while ((child = mainLayout->takeAt(1)) != 0) {
                          delete child->widget();
                          delete child;
                      }
                      readLogs();
                  }
          LogDisplay::~LogDisplay()
          {
          }
      
      

      main.cpp

      #include "logdisplay.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          LogDisplay w;
          w.show();
          return a.exec();
      }
      
      

      The code is executing and checkboxes are displaying but Log Window is not showing?
      Screenshot 2023-02-09 182317.png
      The log window is not showing, Please help!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • Aviral 0A Aviral 0

        Hi I am trying to create an application which displays recent 10 Logs from a file, and have checkboxes to filter the logs based on types,
        here is my code:

        logdisplay.cpp

        #include "logdisplay.h"
        #include <QFile>
        #include <QHBoxLayout>
        #include <QLabel>
        #include <QTextStream>
        #include <QTimer>
        #include <QVBoxLayout>
        
        LogDisplay::LogDisplay(QWidget *parent)
            : QWidget(parent)
        {
            QVBoxLayout *mainLayout = new QVBoxLayout(this);
        
            QHBoxLayout *checkBoxLayout = new QHBoxLayout;
            infoCheckBox = new QCheckBox("INFO");
            infoCheckBox->setChecked(true);
            checkBoxLayout->addWidget(infoCheckBox);
            warnCheckBox = new QCheckBox("WARN");
            warnCheckBox->setChecked(true);
            checkBoxLayout->addWidget(warnCheckBox);
            debugCheckBox = new QCheckBox("DEBUG");
            debugCheckBox->setChecked(true);
            checkBoxLayout->addWidget(debugCheckBox);
            mainLayout->addLayout(checkBoxLayout);
        
            readLogs();
        
            QTimer *timer = new QTimer(this);
            connect(timer, &QTimer::timeout, this, &LogDisplay::refresh);
            timer->start(5000);
        }
        
        
        
        void LogDisplay::readLogs()
        {
            QFile file("home/arpitk/QT Projects/logtail1/sample.log");
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                return;
        
            QTextStream in(&file);
            QStringList logLines;
            while (!in.atEnd()) {
                logLines.append(in.readLine());
            }
        
            int logLinesCount = logLines.count();
            int startIndex = logLinesCount > 10 ? logLinesCount - 10 : 0;
        
            for (int i = startIndex; i < logLinesCount; i++) {
                QString message = logLines.at(i);
                QString level = message.left(5);
                if ((level == "INFO " && infoCheckBox->isChecked()) ||
                    (level == "WARN " && warnCheckBox->isChecked()) ||
                    (level == "DEBUG" && debugCheckBox->isChecked())) {
                    mainLayout->addWidget(new QLabel(message));
                }
            }
        
            file.close();
        }
        
            void LogDisplay::refresh()
                    {
                        QLayoutItem *child;
                        while ((child = mainLayout->takeAt(1)) != 0) {
                            delete child->widget();
                            delete child;
                        }
                        readLogs();
                    }
            LogDisplay::~LogDisplay()
            {
            }
        
        

        main.cpp

        #include "logdisplay.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            LogDisplay w;
            w.show();
            return a.exec();
        }
        
        

        The code is executing and checkboxes are displaying but Log Window is not showing?
        Screenshot 2023-02-09 182317.png
        The log window is not showing, Please help!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Aviral-0 Did you do any debugging?
        You call readLogs() only once in the constructor.
        Does "home/arpitk/QT Projects/logtail1/sample.log" contain anything?
        You should really first do at least some debugging...

        1 Reply Last reply
        1
        • Aviral 0A Aviral 0

          Hi I am trying to create an application which displays recent 10 Logs from a file, and have checkboxes to filter the logs based on types,
          here is my code:

          logdisplay.cpp

          #include "logdisplay.h"
          #include <QFile>
          #include <QHBoxLayout>
          #include <QLabel>
          #include <QTextStream>
          #include <QTimer>
          #include <QVBoxLayout>
          
          LogDisplay::LogDisplay(QWidget *parent)
              : QWidget(parent)
          {
              QVBoxLayout *mainLayout = new QVBoxLayout(this);
          
              QHBoxLayout *checkBoxLayout = new QHBoxLayout;
              infoCheckBox = new QCheckBox("INFO");
              infoCheckBox->setChecked(true);
              checkBoxLayout->addWidget(infoCheckBox);
              warnCheckBox = new QCheckBox("WARN");
              warnCheckBox->setChecked(true);
              checkBoxLayout->addWidget(warnCheckBox);
              debugCheckBox = new QCheckBox("DEBUG");
              debugCheckBox->setChecked(true);
              checkBoxLayout->addWidget(debugCheckBox);
              mainLayout->addLayout(checkBoxLayout);
          
              readLogs();
          
              QTimer *timer = new QTimer(this);
              connect(timer, &QTimer::timeout, this, &LogDisplay::refresh);
              timer->start(5000);
          }
          
          
          
          void LogDisplay::readLogs()
          {
              QFile file("home/arpitk/QT Projects/logtail1/sample.log");
              if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                  return;
          
              QTextStream in(&file);
              QStringList logLines;
              while (!in.atEnd()) {
                  logLines.append(in.readLine());
              }
          
              int logLinesCount = logLines.count();
              int startIndex = logLinesCount > 10 ? logLinesCount - 10 : 0;
          
              for (int i = startIndex; i < logLinesCount; i++) {
                  QString message = logLines.at(i);
                  QString level = message.left(5);
                  if ((level == "INFO " && infoCheckBox->isChecked()) ||
                      (level == "WARN " && warnCheckBox->isChecked()) ||
                      (level == "DEBUG" && debugCheckBox->isChecked())) {
                      mainLayout->addWidget(new QLabel(message));
                  }
              }
          
              file.close();
          }
          
              void LogDisplay::refresh()
                      {
                          QLayoutItem *child;
                          while ((child = mainLayout->takeAt(1)) != 0) {
                              delete child->widget();
                              delete child;
                          }
                          readLogs();
                      }
              LogDisplay::~LogDisplay()
              {
              }
          
          

          main.cpp

          #include "logdisplay.h"
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              LogDisplay w;
              w.show();
              return a.exec();
          }
          
          

          The code is executing and checkboxes are displaying but Log Window is not showing?
          Screenshot 2023-02-09 182317.png
          The log window is not showing, Please help!

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

          @Aviral-0
          I would start by changing the way you do stuff. All this creating individual QLabels for each message and deleting widgets/layout items is not a good way, for many reasons. For displaying log lines just append to a Q[Plain]TextEdit, or possibly use a QListView or QListWidget. You might also benefit from separating responsibilities, so readLogs() might just read the lines with no UI stuff and something else be responsible for displaying them.

          Aviral 0A 1 Reply Last reply
          2
          • JonBJ JonB

            @Aviral-0
            I would start by changing the way you do stuff. All this creating individual QLabels for each message and deleting widgets/layout items is not a good way, for many reasons. For displaying log lines just append to a Q[Plain]TextEdit, or possibly use a QListView or QListWidget. You might also benefit from separating responsibilities, so readLogs() might just read the lines with no UI stuff and something else be responsible for displaying them.

            Aviral 0A Offline
            Aviral 0A Offline
            Aviral 0
            wrote on last edited by
            #5

            @JonB @jsulm I am new to all this, appreciate your patience!
            This is my updated code, I have tried to keep all of your suggestions in mind. and this is I come up with.
            The application is building but still no logs are shown :'(
            And yes the file contains log information for which I am putting absolute path.

            #include "logdisplay.h"
            #include <QFileSystemWatcher>
            #include <QFile>
            #include <QTextStream>
            #include <QVBoxLayout>
            #include <QHBoxLayout>
            #include <QCheckBox>
            #include <QPlainTextEdit>
            #include <QTextCursor>
            #include <QToolBar>
            
            LogDisplay::LogDisplay(QWidget *parent)
                : QMainWindow(parent)
            {
                // Create the log display widget
                logDisplay = new QPlainTextEdit(this);
                logDisplay->setReadOnly(true);
            
                // Create the error checkbox
                errorCheckbox = new QCheckBox("Error", this);
                errorCheckbox->setChecked(true);
            
                // Create the warning checkbox
                warningCheckbox = new QCheckBox("Warning", this);
                warningCheckbox->setChecked(true);
            
                // Create the info checkbox
                infoCheckbox = new QCheckBox("Info", this);
                infoCheckbox->setChecked(true);
            
                // Create a horizontal layout for the checkboxes
                QHBoxLayout *checkboxLayout = new QHBoxLayout;
                checkboxLayout->addWidget(errorCheckbox);
                checkboxLayout->addWidget(warningCheckbox);
                checkboxLayout->addWidget(infoCheckbox);
            
                // Create a vertical layout for the log display and checkboxes
                QVBoxLayout *mainLayout = new QVBoxLayout;
                mainLayout->addWidget(logDisplay);
                mainLayout->addLayout(checkboxLayout);
            
                // Create a central widget and set its layout
                QWidget *centralWidget = new QWidget(this);
                centralWidget->setLayout(mainLayout);
                setCentralWidget(centralWidget);
            
                // Create a toolbar for the checkboxes
                QToolBar *filterToolBar = addToolBar("Filter");
                filterToolBar->addWidget(errorCheckbox);
                filterToolBar->addWidget(warningCheckbox);
                filterToolBar->addWidget(infoCheckbox);
            
                // Connect the checkbox state changed signals to the updateLogDisplay slot
                connect(errorCheckbox, &QCheckBox::stateChanged, this, &LogDisplay::updateLogDisplay);
                connect(warningCheckbox, &QCheckBox::stateChanged, this, &LogDisplay::updateLogDisplay);
                connect(infoCheckbox, &QCheckBox::stateChanged, this, &LogDisplay::updateLogDisplay);
            
                // Create a QFileSystemWatcher to monitor the log file
                QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
                connect(watcher, &QFileSystemWatcher::fileChanged, this, &LogDisplay::updateLogList);
                watcher->addPath("log.txt");
            
                // Initialize the log display with the first set of logs
                updateLogList();
            }
            
            LogDisplay::~LogDisplay()
            {
            }
            
            bool LogDisplay::updateLogList()
            {
                // Open the log file
                QFile logFile("/home/arpitk/QT Projects/logtail1/sample.log");
                if (!logFile.open(QFile::ReadOnly | QFile::Text))
                    return false;
            
                // Read the logs from the log file into the log line
                logList.clear();
                QTextStream in(&logFile);
                while (!in.atEnd()) {
                logList.prepend(in.readLine());
                }
                // Close the log file
                logFile.close();
            
                // Update the log display with the new logs
                updateLogDisplay();
            
                return true;
                }
            void LogDisplay::updateLogDisplay()
            {
            // Clear the current log display
            logDisplay->clear();
            // Iterate over the logs in the log list
            QTextCursor cursor(logDisplay->document());
            for (int i = 0; i < logList.size(); ++i) {
                // Check if the log should be displayed based on the filter
                QString log = logList[i];
                if ((log.contains("Error") && errorCheckbox->isChecked()) ||
                    (log.contains("Warning") && warningCheckbox->isChecked()) ||
                    (log.contains("Info") && infoCheckbox->isChecked())) {
                    // Insert the log into the log display
                    cursor.insertText(log + "\n");
                }
            }
            }
            
            
            JonBJ 1 Reply Last reply
            0
            • Aviral 0A Aviral 0

              @JonB @jsulm I am new to all this, appreciate your patience!
              This is my updated code, I have tried to keep all of your suggestions in mind. and this is I come up with.
              The application is building but still no logs are shown :'(
              And yes the file contains log information for which I am putting absolute path.

              #include "logdisplay.h"
              #include <QFileSystemWatcher>
              #include <QFile>
              #include <QTextStream>
              #include <QVBoxLayout>
              #include <QHBoxLayout>
              #include <QCheckBox>
              #include <QPlainTextEdit>
              #include <QTextCursor>
              #include <QToolBar>
              
              LogDisplay::LogDisplay(QWidget *parent)
                  : QMainWindow(parent)
              {
                  // Create the log display widget
                  logDisplay = new QPlainTextEdit(this);
                  logDisplay->setReadOnly(true);
              
                  // Create the error checkbox
                  errorCheckbox = new QCheckBox("Error", this);
                  errorCheckbox->setChecked(true);
              
                  // Create the warning checkbox
                  warningCheckbox = new QCheckBox("Warning", this);
                  warningCheckbox->setChecked(true);
              
                  // Create the info checkbox
                  infoCheckbox = new QCheckBox("Info", this);
                  infoCheckbox->setChecked(true);
              
                  // Create a horizontal layout for the checkboxes
                  QHBoxLayout *checkboxLayout = new QHBoxLayout;
                  checkboxLayout->addWidget(errorCheckbox);
                  checkboxLayout->addWidget(warningCheckbox);
                  checkboxLayout->addWidget(infoCheckbox);
              
                  // Create a vertical layout for the log display and checkboxes
                  QVBoxLayout *mainLayout = new QVBoxLayout;
                  mainLayout->addWidget(logDisplay);
                  mainLayout->addLayout(checkboxLayout);
              
                  // Create a central widget and set its layout
                  QWidget *centralWidget = new QWidget(this);
                  centralWidget->setLayout(mainLayout);
                  setCentralWidget(centralWidget);
              
                  // Create a toolbar for the checkboxes
                  QToolBar *filterToolBar = addToolBar("Filter");
                  filterToolBar->addWidget(errorCheckbox);
                  filterToolBar->addWidget(warningCheckbox);
                  filterToolBar->addWidget(infoCheckbox);
              
                  // Connect the checkbox state changed signals to the updateLogDisplay slot
                  connect(errorCheckbox, &QCheckBox::stateChanged, this, &LogDisplay::updateLogDisplay);
                  connect(warningCheckbox, &QCheckBox::stateChanged, this, &LogDisplay::updateLogDisplay);
                  connect(infoCheckbox, &QCheckBox::stateChanged, this, &LogDisplay::updateLogDisplay);
              
                  // Create a QFileSystemWatcher to monitor the log file
                  QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
                  connect(watcher, &QFileSystemWatcher::fileChanged, this, &LogDisplay::updateLogList);
                  watcher->addPath("log.txt");
              
                  // Initialize the log display with the first set of logs
                  updateLogList();
              }
              
              LogDisplay::~LogDisplay()
              {
              }
              
              bool LogDisplay::updateLogList()
              {
                  // Open the log file
                  QFile logFile("/home/arpitk/QT Projects/logtail1/sample.log");
                  if (!logFile.open(QFile::ReadOnly | QFile::Text))
                      return false;
              
                  // Read the logs from the log file into the log line
                  logList.clear();
                  QTextStream in(&logFile);
                  while (!in.atEnd()) {
                  logList.prepend(in.readLine());
                  }
                  // Close the log file
                  logFile.close();
              
                  // Update the log display with the new logs
                  updateLogDisplay();
              
                  return true;
                  }
              void LogDisplay::updateLogDisplay()
              {
              // Clear the current log display
              logDisplay->clear();
              // Iterate over the logs in the log list
              QTextCursor cursor(logDisplay->document());
              for (int i = 0; i < logList.size(); ++i) {
                  // Check if the log should be displayed based on the filter
                  QString log = logList[i];
                  if ((log.contains("Error") && errorCheckbox->isChecked()) ||
                      (log.contains("Warning") && warningCheckbox->isChecked()) ||
                      (log.contains("Info") && infoCheckbox->isChecked())) {
                      // Insert the log into the log display
                      cursor.insertText(log + "\n");
                  }
              }
              }
              
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Aviral-0
              You must either step through in a debugger or put in qDebug() messages. We (i.e. you!) need to know whether/when lines are being read from the log file and whether/when they are (attempted to be) displayed.

              Aviral 0A 1 Reply Last reply
              0
              • JonBJ JonB

                @Aviral-0
                You must either step through in a debugger or put in qDebug() messages. We (i.e. you!) need to know whether/when lines are being read from the log file and whether/when they are (attempted to be) displayed.

                Aviral 0A Offline
                Aviral 0A Offline
                Aviral 0
                wrote on last edited by
                #7

                @JonB @jsulm after debugging i got to know that QFile was not reading file, got it solved and not its working perfectly. Thankyou for all you help and kind support.

                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