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 4 Jun 2015, 14:14 last edited by ASSeeger 6 May 2015, 09:24
    #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;
            }
        }
    }
    
    P 1 Reply Last reply 4 Jun 2015, 17:05
    1
    • A ASSeeger
      4 Jun 2015, 14:14

      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;
              }
          }
      }
      
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 4 Jun 2015, 17:05 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 5 Jun 2015, 09:17
      1
      • S Offline
        S Offline
        stereomatchingkiss
        wrote on 5 Jun 2015, 00:32 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 5 Jun 2015, 09:23
        1
        • P p3c0
          4 Jun 2015, 17:05

          @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 5 Jun 2015, 09:17 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.

          P 1 Reply Last reply 5 Jun 2015, 09:21
          0
          • A ASSeeger
            5 Jun 2015, 09:17

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

            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 5 Jun 2015, 09:21 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
              5 Jun 2015, 00:32

              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 5 Jun 2015, 09:23 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
              • C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 5 Jun 2015, 10:23 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                2/7

                4 Jun 2015, 17:05

                topic:navigator.unread, 5
                • Login

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