Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt TextFinder Tutorial Problem



  • Hello,

    I found the tutorial for Qt and just tried to copy that and understand it.

    I thought I made everything right, but obviously not, because when I run the code the Application starts with these lines:

    bq. In German:
    Starte C:<PathToProjectFolder>\Qt\build-TextFinder-Desktop_Qt_5_2_0_MSVC2012_OpenGL_64bit-Debug\debug\TextFinder.exe...
    QIODevice::read: device not open
    C:<PathToProjectFolder>\Qt\build-TextFinder-Desktop_Qt_5_2_0_MSVC2012_OpenGL_64bit-Debug\debug\TextFinder.exe beendet, Rückgabewert 0

    i think in english it should look like:

    bq. Run C:<PathToProjectFolder>\Qt\build-TextFinder-Desktop_Qt_5_2_0_MSVC2012_OpenGL_64bit-Debug\debug\TextFinder.exe...
    QIODevice::read: device not open
    C:<PathToProjectFolder>\Qt\build-TextFinder-Desktop_Qt_5_2_0_MSVC2012_OpenGL_64bit-Debug\debug\TextFinder.exe terminate/exit, return 0

    My Files:

    textfinder.h

    @#ifndef TEXTFINDER_H
    #define TEXTFINDER_H

    #include <QWidget>

    namespace Ui {
    class TextFinder;
    }

    class TextFinder : public QWidget
    {
    Q_OBJECT

    public:
    explicit TextFinder(QWidget *parent = 0);
    ~TextFinder();

    private slots:
    void on_findButton_clicked();

    private:
    Ui::TextFinder *ui;
    void loadTextFile();

    };

    #endif // TEXTFINDER_H
    @

    textfinder.cpp
    @#include "textfinder.h"
    #include "ui_textfinder.h"
    #include <QtCore/QFile>
    #include <QtCore/QTextStream>

    TextFinder::TextFinder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TextFinder)
    {
    ui->setupUi(this);
    loadTextFile();
    QMetaObject::connectSlotsByName(this);
    }

    TextFinder::~TextFinder()
    {
    delete ui;
    }

    void TextFinder::on_findButton_clicked()
    {
    QString searchString = ui->lineEdit->text();
    ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
    }

    void TextFinder::loadTextFile()
    {
    QFile inputFile(":/input.txt");
    inputFile.open(QIODevice::ReadOnly);

    QTextStream in(&inputFile);
    QString line = in.readAll();
    inputFile.close();
    
    ui->textEdit->setPlainText(line);
    QTextCursor cursor = ui->textEdit->textCursor();
    cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
    

    }
    @

    so how do i fix it?



  • You're reading from an io device that isn't opened. Did you make sure the open call on inputeFile isn't failing?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you have input.txt in your sources and do you have a qrc file referencing it in your project ?



  • bq. You’re reading from an io device that isn’t opened. Did you make sure the open call on inputeFile isn’t failing?

    How can I make it sure, that the open call isn't failing?

    bq. Hi and welcome to devnet,
    Do you have input.txt in your sources and do you have a qrc file referencing it in your project ?

    I have a resource file (name: textfinder.qrc) with the prefix / and a file in it with the name input.txt.

    When i double click the resource file I see on top the / and below the input.txt

    I even found the file on my drive


  • Lifetime Qt Champion

    By solving what error QFile returns if open fails



  • @Hailo90 You need to firt "Run qmake" before running the project.


Log in to reply