user3183586
user3183586

Reputation: 167

C++ GUI Text Not Displaying

Basically I made a simple program to search a text file for a certain word. However my text file does not display in the textEdit Object it just displays it blank. I properly placed the text file into the resources and everything. I would really appreciate some input.

#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include "find.h"
#include "ui_find.h"

Find::Find(QWidget *parent) : QWidget(parent), ui(new Ui::Find)
{
    ui->setupUi(this); //sets up user interface
    getTextFile();
}

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

void Find::on_goButton_clicked()
{
    QString word = ui->lineEdit->text(); //gets text and stores in word
    ui->textEdit->find(word, QTextDocument::FindWholeWords);
}

void Find::getTextFile()
{
    QFile myFile(":/textfile.txt"); //what file
    myFile.open(QIODevice::ReadOnly); //opens file

    QTextStream textStream(&myFile); //convert to stream
    QString line = textStream.readAll(); //store into variable
    myFile.close(); //close file

    ui->textEdit->setPlainText(line); //display text
    QTextCursor textCursor = ui->textEdit->textCursor(); //moves cursor into position
    textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1); //moves cursor into position

}

The application output also has this in red letters QIODevice::read: device not open

Upvotes: 2

Views: 1501

Answers (4)

rela
rela

Reputation: 33

I have Qt 5.4 and my code gave the same error, when it was working with MSVC2010 OpenGL as a compiler. I manually added MinGW 32bit to use it as compiler and it worked. P.S. I have not installed MSVC2013 for my Qt 5.4., and it works sometimes with MSVC2010 OpenGL without error, but not in this case.

Upvotes: 1

Chawathe Vipul S
Chawathe Vipul S

Reputation: 1696

Qt Creator's clean rebuild are broken. Just permanently delete the kit specific folder where qmake has kept the project specific makefiles.

Attempt to do build again. rcc will be invoked specific to .qrc files added, that were skipped by older makefile files, as qmake has to give new makefile files now.

Upvotes: 1

Rachael
Rachael

Reputation: 1995

I just solved this with same error/symptoms. The issue was that I did not have a working compiler for my build kit. I ended up having to manually select my Microsoft Visual C++ Compiler 11.0 (x86). I guess it wasn't able to auto-detect it. This seems to be a common error message for new windows users beginning Qt5.

Upvotes: 0

Igor
Igor

Reputation: 1059

Make sure that you added qrc file in project. If you use qrc file as external source, you should register it by call QResource::registerResource("path to file") in main function.

Upvotes: 0

Related Questions