【发布时间】:2015-10-09 21:36:42
【问题描述】:
基本上我做了一个简单的程序来搜索文本文件中的某个单词。但是我的文本文件没有显示在 textEdit 对象中,它只是显示为空白。我正确地将文本文件放入资源和所有内容中。我真的很感激一些意见。
#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
}
应用程序输出也有这个红色字母 QIODevice::read: device not open
【问题讨论】:
-
那
":/textfile.txt"不是缺少一个起始c什么的吗? -
文件路径以
:开头,表示文件在资源文件中。如果您没有.qrc文件,请从路径中删除:。 -
我确实有一个 qrc 文件虽然@koplersky
-
我不知道@rodrigo 是什么意思
-
textfile.txt是否在.qrc文件的文件夹中?