C++ QIODevice::read: 设备未打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24963081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QIODevice::read: device not open
提问by user3878223
Im trying to read from a file and put into to the text edit and it keeps saying QIODevice::read:device not open. The .txt file is in the same location as my .qrc and .cpp file. I was following a step by step guide from online. From my understanding, they changed something when they went from Q4 to Q5. Does anyone have any hint on how I can fix this. thanks
我试图从文件中读取并放入文本编辑中,它一直说 QIODevice::read:device 未打开。.txt 文件与我的 .qrc 和 .cpp 文件位于同一位置。我正在按照网上的分步指南进行操作。据我了解,从 Q4 到 Q5,他们改变了一些东西。有没有人对我如何解决这个问题有任何提示。谢谢
//My findstuff.h
#ifndef FINDSTUFF_H
#define FINDSTUFF_H
#include <QWidget>
namespace Ui {class FindStuff;}
class FindStuff : public QWidget{
Q_OBJECT
public:
explicit FindStuff(QWidget *parent = 0);
~FindStuff();
private slots:
void on_goButton_clicked();
private:
Ui::FindStuff *ui;
void getTextFile();
};
回答by randag
If you're reading from a .qrc resource file you have to run qmake ("Build->Run qmake" in Qt Creator) before it will be available.
如果您正在读取 .qrc 资源文件,则必须在它可用之前运行 qmake(Qt Creator 中的“Build->Run qmake”)。
回答by Reinstate Monica
You're not passing the absolutepath of the file to QFile::open()
, and you're not checking the result of opening the file. In your case, it's a failure and open()
returns false, but you're ignoring it, instead of fixing the problem (the wrong path) that caused it.
您没有将文件的绝对路径传递给QFile::open()
,也没有检查打开文件的结果。在您的情况下,这是一个失败并open()
返回 false,但您忽略了它,而不是修复导致它的问题(错误的路径)。
This has zilch to do with Qt 4 -> Qt 5 upgrade, and everything to do with you assuming the wrong thing about the current directory your application happens to find itself with. Generally speaking, the current directory (or working directory) is arbitrary, and platform- and circumstance-specific, and wholly out of your control. Unless the user gives you a filename that's implicitly referenced to the current working directory (e.g. as a relative path given a commandline argument), you must use absolute file paths or things simply won't work.
这与 Qt 4 -> Qt 5 升级有关,并且与您假设您的应用程序碰巧发现自己所在的当前目录错误有关。一般而言,当前目录(或工作目录)是任意的,特定于平台和环境,完全不受您的控制。除非用户给您一个隐式引用到当前工作目录的文件名(例如,作为给定命令行参数的相对路径),否则您必须使用绝对文件路径,否则事情将无法正常工作。
回答by rela
It can be related to the version of Qt, since Qt5 sometimes doesn't work with MSVC2010. 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 Qt 5.4., and it works sometimes with MSVC2010 OpenGL without error, but not in this case.
它可能与 Qt 的版本有关,因为 Qt5 有时不适用于 MSVC2010。我有 Qt 5.4 并且我的代码在使用 MSVC2010 OpenGL 作为编译器时出现了同样的错误。我手动添加了 MinGW 32bit 以将其用作编译器并且它工作正常。PS 我还没有为 Qt 5.4 安装 MSVC2013,它有时与 MSVC2010 OpenGL 一起工作没有错误,但在这种情况下不是。
回答by Niall
I had this problem and it turned out Qt Creator hadn't actually added the .qrc file to my project. I'm using Qt Creator 4.1.0 on a Mac and the projects view doesn't always get populated after creating a new project, first requiring a restart of Creator. My version of this problem may relate to that.
我遇到了这个问题,结果发现 Qt Creator 实际上并没有将 .qrc 文件添加到我的项目中。我在 Mac 上使用 Qt Creator 4.1.0,在创建新项目后,项目视图并不总是填充,首先需要重新启动 Creator。我对这个问题的版本可能与此有关。
回答by user11014544
It does not have anything to do with Qt version. Even though your .txt file is in the same directory as your .cpp file, you still need to add the directory. I had the same problem and that simple solution worked well. Arman Arefi
它与 Qt 版本没有任何关系。即使您的 .txt 文件与 .cpp 文件位于同一目录中,您仍然需要添加该目录。我遇到了同样的问题,这个简单的解决方案效果很好。阿曼·阿雷菲