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. [SOLVED] Load HTML file to search for a string
QtWS25 Last Chance

[SOLVED] Load HTML file to search for a string

Scheduled Pinned Locked Moved General and Desktop
c++qt applicationqstringqtextstream
7 Posts 4 Posters 5.0k 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.
  • A Offline
    A Offline
    ASSeeger
    wrote on last edited by ASSeeger
    #1

    Hi there.

    My task is to write an application that loads a locally stored HTML file and search for a string in there.
    So I took a Push Button and connected with the slot, put the content into an QTextStream to display the file's content in a Text Browser – which is working fine so far.

    Now, if I get it correctly in order to search for a substring I need to have a QString object – and that's where I end up with an empty string.

    Question is: why is

    ui->textBrowserHTML->setText(in.readAll());
    

    working (showing content in the textBrowser) whereas

    QString myString;
    myString = in.readAll();
    

    results in an empty string? What am I – Newby that I am – missing here?

    So here's the whole code:

    void MainWindow::on_getHTMLButton_clicked()
    {
        QString filename = QFileDialog::getOpenFileName
                (
                    this,
                    tr("Open HTML-File:"),
                    "/Downloads/"
                    "HTML–Datei (*.htm*)"
                    );
        
        if (filename.isEmpty())
            QMessageBox::information(this, tr("File Name"), "Please choose valid file.");
        else
        {
            // Create file
            QFile file(filename);
            if (!file.open(QIODevice::ReadOnly))
            {
                QMessageBox::information(0, "Info", file.errorString());
            } 
            else
            {            
                // Create Stream
                QTextStream in(&file);
                
                ui->textBrowserHTML->setText(in.readAll());
                
                // Create string from stream
                QString myString;
                myString = in.readLine();
                
                if (myString.isEmpty())
                    qDebug() << "String is empty…";
                else
                    qDebug() << "String:" << myString;
            }
        }
    }
    
    p3c0P 1 Reply Last reply
    1
    • A ASSeeger

      Hi there.

      My task is to write an application that loads a locally stored HTML file and search for a string in there.
      So I took a Push Button and connected with the slot, put the content into an QTextStream to display the file's content in a Text Browser – which is working fine so far.

      Now, if I get it correctly in order to search for a substring I need to have a QString object – and that's where I end up with an empty string.

      Question is: why is

      ui->textBrowserHTML->setText(in.readAll());
      

      working (showing content in the textBrowser) whereas

      QString myString;
      myString = in.readAll();
      

      results in an empty string? What am I – Newby that I am – missing here?

      So here's the whole code:

      void MainWindow::on_getHTMLButton_clicked()
      {
          QString filename = QFileDialog::getOpenFileName
                  (
                      this,
                      tr("Open HTML-File:"),
                      "/Downloads/"
                      "HTML–Datei (*.htm*)"
                      );
          
          if (filename.isEmpty())
              QMessageBox::information(this, tr("File Name"), "Please choose valid file.");
          else
          {
              // Create file
              QFile file(filename);
              if (!file.open(QIODevice::ReadOnly))
              {
                  QMessageBox::information(0, "Info", file.errorString());
              } 
              else
              {            
                  // Create Stream
                  QTextStream in(&file);
                  
                  ui->textBrowserHTML->setText(in.readAll());
                  
                  // Create string from stream
                  QString myString;
                  myString = in.readLine();
                  
                  if (myString.isEmpty())
                      qDebug() << "String is empty…";
                  else
                      qDebug() << "String:" << myString;
              }
          }
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @ASSeeger,
      Because once after readAll there is nothing left to read again from that stream and thus it returns empty.

      157

      A 1 Reply Last reply
      1
      • S Offline
        S Offline
        stereomatchingkiss
        wrote on last edited by
        #3

        Do not know what is the type of "ui->textBrowserHTML", I guess it is QTextEdit
        Like @p3c0 said, after readAll there is nothing left to read again
        you can get the string as following if it is QTextEdit

        auto const WebContent = ui->textBrowserHTML->toPlainText();
        
        A 1 Reply Last reply
        1
        • p3c0P p3c0

          @ASSeeger,
          Because once after readAll there is nothing left to read again from that stream and thus it returns empty.

          A Offline
          A Offline
          ASSeeger
          wrote on last edited by
          #4

          @p3c0 Thank you, I now did the creation of the string before displaying it in the textBrowser – and now it works.

          p3c0P 1 Reply Last reply
          0
          • A ASSeeger

            @p3c0 Thank you, I now did the creation of the string before displaying it in the textBrowser – and now it works.

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @ASSeeger That's great :)
            You can also mark the post as solved if done. Just edit the post title and prepend [Solved].

            157

            1 Reply Last reply
            0
            • S stereomatchingkiss

              Do not know what is the type of "ui->textBrowserHTML", I guess it is QTextEdit
              Like @p3c0 said, after readAll there is nothing left to read again
              you can get the string as following if it is QTextEdit

              auto const WebContent = ui->textBrowserHTML->toPlainText();
              
              A Offline
              A Offline
              ASSeeger
              wrote on last edited by
              #6

              @stereomatchingkiss Also thank you, as stated above I just changed the workflow and now it works. It's been a QTextBrowser by the way.
              Thanks for your efforts!

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7
                This post is deleted!
                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