【问题标题】:C++ GUI Text Not DisplayingC++ GUI 文本不显示
【发布时间】: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 文件的文件夹中?

标签: c++ qt


【解决方案1】:

Qt Creator 的干净重建被破坏了。只需永久删除 qmake 保存项目特定 makefile 的套件特定文件夹。

尝试再次构建。 rcc 将被特定于添加的 .qrc 文件调用,这些文件被旧的 makefile 文件跳过,因为 qmake 现在必须提供新的 makefile 文件。

【讨论】:

  • 谢谢,我花了好几个小时试图找出问题所在。
【解决方案2】:

我有 Qt 5.4,当它使用 MSVC2010 OpenGL 作为编译器时,我的代码给出了同样的错误。我手动添加了 MinGW 32bit 以将其用作编译器并且它可以工作。 附言我没有为我的 Qt 5.4 安装 MSVC2013。它有时可以与 MSVC2010 OpenGL 一起使用而不会出错,但在这种情况下不是。

【讨论】:

    【解决方案3】:

    确保您在项目中添加了 qrc 文件。如果你使用 qrc 文件作为外部源,你应该通过在 main 函数中调用 QResource::registerResource("path to file") 来注册它。

    【讨论】:

      【解决方案4】:

      我刚刚用同样的错误/症状解决了这个问题。问题是我的构建工具包没有可用的编译器。我最终不得不手动选择我的 Microsoft Visual C++ Compiler 11.0 (x86)。我猜它无法自动检测到它。对于从 Qt5 开始的新 Windows 用户来说,这似乎是一个常见的错误消息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        • 1970-01-01
        • 2013-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多