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. I have a LogDisplay application. I want to reduce Time Complexity.

I have a LogDisplay application. I want to reduce Time Complexity.

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

    Hi,
    I have a Log Display application which displays recent 10, 20 , 100 logs and use For loop to read these recent 100 lines. I want to reduce time complexity. Any suggestions on what function should I use?

    void LogDisplay::updateLogDisplay()
    {
        logDisplay->clear();
        int count = 0;
    
        QTextCursor cursor(logDisplay->document());
        cursor.movePosition(QTextCursor::End);
    
        for(int i = logList.size() - 1; i >= 0 && count < maxLines; --i)
        {
            QString log = logList[i];
    
            if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                (infoCheckbox->isChecked() && log.contains("INFO"))) {
    
                QTextCharFormat format;
                if (log.contains("ERROR")) {
                    format.setForeground(Qt::red);
                } else if (log.contains("WARNING")) {
                    format.setForeground(Qt::darkYellow);
                }
    
                cursor.insertText("\n");
                cursor.movePosition(QTextCursor::Up);
                cursor.setCharFormat(format);
                cursor.insertText(log);
                ++count;
            }
        }
    }
    
    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:

      Error: No matching member function for call to 'filter'.

      I do not see any overload of QStringList QStringList::filter() which takes a function/lambda argument, so I don't know where you got the idea from that it does.

      Error: No member named 'toPlainTextFormat' in 'QTextOption'

      I do not see any method of QTextOption named toPlainTextFormat(), so I don't know where you got that from.

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

      @JonB Thankyou for reply!
      Well I solved it:

      void LogDisplay::updateLogDisplay()
      {
          // Filter logList using lambda function
          QStringList filteredLogList;
          std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList),
              [&](const QString& log) {
                  if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                      (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                      (infoCheckbox->isChecked() && log.contains("INFO"))) {
                      return true;
                  }
                  return false;
              });
      
          // Check if the filtered log list is longer than maxLines
              if (filteredLogList.size() > maxLines) {
                  // Remove oldest logs to limit the size to maxLines
                  filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines);
              }
      
          QString logHtml;
          QTextCharFormat errorFormat, warningFormat, defaultFormat;
      
          errorFormat.setForeground(Qt::red);
          warningFormat.setForeground(Qt::darkYellow);
      
          QTextDocument document;
          document.setDefaultFont(logDisplay->font());
          defaultFormat.setFont(document.defaultFont());
      
          for (const QString& log : filteredLogList) {
              QTextCharFormat format = defaultFormat;
              if (log.contains("ERROR")) {
                  format.merge(errorFormat);
              } else if (log.contains("WARNING")) {
                  format.merge(warningFormat);
              }
              logHtml += QString("<p style=\"color:%1;\">%2</p>")
                  .arg(format.foreground().color().name())
                  .arg(log.toHtmlEscaped());
          }
      
          logDisplay->setPlainText("");
          logDisplay->document()->setHtml(logHtml);
      }
      
      piervalliP JonBJ 2 Replies Last reply
      0
      • Aviral 0A Aviral 0

        Hi,
        I have a Log Display application which displays recent 10, 20 , 100 logs and use For loop to read these recent 100 lines. I want to reduce time complexity. Any suggestions on what function should I use?

        void LogDisplay::updateLogDisplay()
        {
            logDisplay->clear();
            int count = 0;
        
            QTextCursor cursor(logDisplay->document());
            cursor.movePosition(QTextCursor::End);
        
            for(int i = logList.size() - 1; i >= 0 && count < maxLines; --i)
            {
                QString log = logList[i];
        
                if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                    (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                    (infoCheckbox->isChecked() && log.contains("INFO"))) {
        
                    QTextCharFormat format;
                    if (log.contains("ERROR")) {
                        format.setForeground(Qt::red);
                    } else if (log.contains("WARNING")) {
                        format.setForeground(Qt::darkYellow);
                    }
        
                    cursor.insertText("\n");
                    cursor.movePosition(QTextCursor::Up);
                    cursor.setCharFormat(format);
                    cursor.insertText(log);
                    ++count;
                }
            }
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Aviral-0
        I guess you could build a single block of HTML, including the colors, for all lines and then just append the HTML in one go. Don't know if you'd notice the difference, and wouldn't expect what you already have to be a problem?

        Aviral 0A 1 Reply Last reply
        0
        • JonBJ JonB

          @Aviral-0
          I guess you could build a single block of HTML, including the colors, for all lines and then just append the HTML in one go. Don't know if you'd notice the difference, and wouldn't expect what you already have to be a problem?

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

          @JonB I have tried this code:

          void LogDisplay::updateLogDisplay()
          {
              QStringList filteredLogList = logList.filter([&](const QString& log) {
                  if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                      (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                      (infoCheckbox->isChecked() && log.contains("INFO"))) {
                      return true;
                  }
                  return false;
              });
          
              QString logHtml;
              QTextCharFormat errorFormat, warningFormat, defaultFormat;
          
              errorFormat.setForeground(Qt::red);
              warningFormat.setForeground(Qt::darkYellow);
          
              QTextDocument document;
              document.setDefaultFont(logDisplay->font());
              defaultFormat = document.defaultTextOption().toPlainTextFormat();
          
              for (const QString& log : filteredLogList) {
                  QTextCharFormat format = defaultFormat;
                  if (log.contains("ERROR")) {
                      format.merge(errorFormat);
                  } else if (log.contains("WARNING")) {
                      format.merge(warningFormat);
                  }
                  logHtml += QString("<p style=\"color:%1;\">%2</p>")
                      .arg(format.foreground().color().name())
                      .arg(log.toHtmlEscaped());
              }
          
              logDisplay->setPlainText("");
              logDisplay->document()->setHtml(logHtml);
          }
          

          But It throws ERROR:
          Error: No matching member function for call to 'filter'.
          Error: No member named 'toPlainTextFormat' in 'QTextOption'
          Please Tell me what to do.

          JonBJ 1 Reply Last reply
          0
          • Aviral 0A Aviral 0

            @JonB I have tried this code:

            void LogDisplay::updateLogDisplay()
            {
                QStringList filteredLogList = logList.filter([&](const QString& log) {
                    if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                        (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                        (infoCheckbox->isChecked() && log.contains("INFO"))) {
                        return true;
                    }
                    return false;
                });
            
                QString logHtml;
                QTextCharFormat errorFormat, warningFormat, defaultFormat;
            
                errorFormat.setForeground(Qt::red);
                warningFormat.setForeground(Qt::darkYellow);
            
                QTextDocument document;
                document.setDefaultFont(logDisplay->font());
                defaultFormat = document.defaultTextOption().toPlainTextFormat();
            
                for (const QString& log : filteredLogList) {
                    QTextCharFormat format = defaultFormat;
                    if (log.contains("ERROR")) {
                        format.merge(errorFormat);
                    } else if (log.contains("WARNING")) {
                        format.merge(warningFormat);
                    }
                    logHtml += QString("<p style=\"color:%1;\">%2</p>")
                        .arg(format.foreground().color().name())
                        .arg(log.toHtmlEscaped());
                }
            
                logDisplay->setPlainText("");
                logDisplay->document()->setHtml(logHtml);
            }
            

            But It throws ERROR:
            Error: No matching member function for call to 'filter'.
            Error: No member named 'toPlainTextFormat' in 'QTextOption'
            Please Tell me what to do.

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

            @Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:

            Error: No matching member function for call to 'filter'.

            I do not see any overload of QStringList QStringList::filter() which takes a function/lambda argument, so I don't know where you got the idea from that it does.

            Error: No member named 'toPlainTextFormat' in 'QTextOption'

            I do not see any method of QTextOption named toPlainTextFormat(), so I don't know where you got that from.

            Aviral 0A 1 Reply Last reply
            0
            • JonBJ JonB

              @Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:

              Error: No matching member function for call to 'filter'.

              I do not see any overload of QStringList QStringList::filter() which takes a function/lambda argument, so I don't know where you got the idea from that it does.

              Error: No member named 'toPlainTextFormat' in 'QTextOption'

              I do not see any method of QTextOption named toPlainTextFormat(), so I don't know where you got that from.

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

              @JonB Thankyou for reply!
              Well I solved it:

              void LogDisplay::updateLogDisplay()
              {
                  // Filter logList using lambda function
                  QStringList filteredLogList;
                  std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList),
                      [&](const QString& log) {
                          if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                              (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                              (infoCheckbox->isChecked() && log.contains("INFO"))) {
                              return true;
                          }
                          return false;
                      });
              
                  // Check if the filtered log list is longer than maxLines
                      if (filteredLogList.size() > maxLines) {
                          // Remove oldest logs to limit the size to maxLines
                          filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines);
                      }
              
                  QString logHtml;
                  QTextCharFormat errorFormat, warningFormat, defaultFormat;
              
                  errorFormat.setForeground(Qt::red);
                  warningFormat.setForeground(Qt::darkYellow);
              
                  QTextDocument document;
                  document.setDefaultFont(logDisplay->font());
                  defaultFormat.setFont(document.defaultFont());
              
                  for (const QString& log : filteredLogList) {
                      QTextCharFormat format = defaultFormat;
                      if (log.contains("ERROR")) {
                          format.merge(errorFormat);
                      } else if (log.contains("WARNING")) {
                          format.merge(warningFormat);
                      }
                      logHtml += QString("<p style=\"color:%1;\">%2</p>")
                          .arg(format.foreground().color().name())
                          .arg(log.toHtmlEscaped());
                  }
              
                  logDisplay->setPlainText("");
                  logDisplay->document()->setHtml(logHtml);
              }
              
              piervalliP JonBJ 2 Replies Last reply
              0
              • Aviral 0A Aviral 0 has marked this topic as solved on
              • Aviral 0A Aviral 0

                @JonB Thankyou for reply!
                Well I solved it:

                void LogDisplay::updateLogDisplay()
                {
                    // Filter logList using lambda function
                    QStringList filteredLogList;
                    std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList),
                        [&](const QString& log) {
                            if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                                (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                                (infoCheckbox->isChecked() && log.contains("INFO"))) {
                                return true;
                            }
                            return false;
                        });
                
                    // Check if the filtered log list is longer than maxLines
                        if (filteredLogList.size() > maxLines) {
                            // Remove oldest logs to limit the size to maxLines
                            filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines);
                        }
                
                    QString logHtml;
                    QTextCharFormat errorFormat, warningFormat, defaultFormat;
                
                    errorFormat.setForeground(Qt::red);
                    warningFormat.setForeground(Qt::darkYellow);
                
                    QTextDocument document;
                    document.setDefaultFont(logDisplay->font());
                    defaultFormat.setFont(document.defaultFont());
                
                    for (const QString& log : filteredLogList) {
                        QTextCharFormat format = defaultFormat;
                        if (log.contains("ERROR")) {
                            format.merge(errorFormat);
                        } else if (log.contains("WARNING")) {
                            format.merge(warningFormat);
                        }
                        logHtml += QString("<p style=\"color:%1;\">%2</p>")
                            .arg(format.foreground().color().name())
                            .arg(log.toHtmlEscaped());
                    }
                
                    logDisplay->setPlainText("");
                    logDisplay->document()->setHtml(logHtml);
                }
                
                piervalliP Offline
                piervalliP Offline
                piervalli
                wrote on last edited by
                #6

                @Aviral-0
                I like a foreach on loglist with break with maxsize, so you can look one shot.

                1 Reply Last reply
                0
                • Aviral 0A Aviral 0

                  @JonB Thankyou for reply!
                  Well I solved it:

                  void LogDisplay::updateLogDisplay()
                  {
                      // Filter logList using lambda function
                      QStringList filteredLogList;
                      std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList),
                          [&](const QString& log) {
                              if ((errorCheckbox->isChecked() && log.contains("ERROR")) ||
                                  (warningCheckbox->isChecked() && log.contains("WARNING")) ||
                                  (infoCheckbox->isChecked() && log.contains("INFO"))) {
                                  return true;
                              }
                              return false;
                          });
                  
                      // Check if the filtered log list is longer than maxLines
                          if (filteredLogList.size() > maxLines) {
                              // Remove oldest logs to limit the size to maxLines
                              filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines);
                          }
                  
                      QString logHtml;
                      QTextCharFormat errorFormat, warningFormat, defaultFormat;
                  
                      errorFormat.setForeground(Qt::red);
                      warningFormat.setForeground(Qt::darkYellow);
                  
                      QTextDocument document;
                      document.setDefaultFont(logDisplay->font());
                      defaultFormat.setFont(document.defaultFont());
                  
                      for (const QString& log : filteredLogList) {
                          QTextCharFormat format = defaultFormat;
                          if (log.contains("ERROR")) {
                              format.merge(errorFormat);
                          } else if (log.contains("WARNING")) {
                              format.merge(warningFormat);
                          }
                          logHtml += QString("<p style=\"color:%1;\">%2</p>")
                              .arg(format.foreground().color().name())
                              .arg(log.toHtmlEscaped());
                      }
                  
                      logDisplay->setPlainText("");
                      logDisplay->document()->setHtml(logHtml);
                  }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:

                  Well I solved it:

                  So did doing it this way instead of inserting text item-by-item make a big difference to speed?

                  piervalliP 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:

                    Well I solved it:

                    So did doing it this way instead of inserting text item-by-item make a big difference to speed?

                    piervalliP Offline
                    piervalliP Offline
                    piervalli
                    wrote on last edited by
                    #8

                    @JonB
                    only differen of thought In the QList there is method "reserve" which reserve the space. Max caposity is 100records.
                    If the loglist has many records in one single loop you can append records without realloc memory. Sorry this is only my thought which can be wrong.

                    JonBJ 1 Reply Last reply
                    0
                    • piervalliP piervalli

                      @JonB
                      only differen of thought In the QList there is method "reserve" which reserve the space. Max caposity is 100records.
                      If the loglist has many records in one single loop you can append records without realloc memory. Sorry this is only my thought which can be wrong.

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

                      @piervalli
                      Your thought is not wrong, but it will make 0.00001% difference here :)
                      The only thing which could take any time is updating of the UI.

                      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