Important: Please read the Qt Code of Conduct -

[SOLVED] Load HTML file to search for a string

  • 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


    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
                    tr("Open HTML-File:"),
                    "HTML–Datei (*.htm*)"
        if (filename.isEmpty())
            QMessageBox::information(this, tr("File Name"), "Please choose valid file.");
            // Create file
            QFile file(filename);
            if (!
                QMessageBox::information(0, "Info", file.errorString());
                // Create Stream
                QTextStream in(&file);
                // Create string from stream
                QString myString;
                myString = in.readLine();
                if (myString.isEmpty())
                    qDebug() << "String is empty…";
                    qDebug() << "String:" << myString;

  • Moderators

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

  • 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();

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

  • Moderators

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

  • @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!

  • Moderators

    This post is deleted!