QIODevice::read: device not open
Asked Answered
P

6

12

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

//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();
};
Pinchcock answered 25/7, 2014 at 19:22 Comment(3)
Can we see some code? If I had to guess, you're not opening the file correctly. If you're just reading from a file, why don't you use the QFile class, or just use fstream.h? Additionally if you're using Qt the .txt file needs to be in the build directory, not in the source directory (assuming you don't change the working directory). If you use the default settings, it will be in a folder named build-%{CurrentProject:Name}-%{CurrentKit:FileSystemName}-%{CurrentBuild:Name}Revolution
It wont let me put in the proper syntax //My findstuff.h #ifndef FINDSTUFF_H #define FINDSTUFF_H #include <QWidget> namespace Ui {class FindStuff;} class FindStuff : public QWidget{ Q_OBJECTpublic: explicit FindStuff(QWidget *parent = 0); ~FindStuff(); private slots: void on_goButton_clicked();private: Ui::FindStuff *ui; void getTextFile();};Pinchcock
The code you gave us does not help, we need to see how you're using open()Revolution
H
7

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.

Hieroglyphic answered 30/1, 2015 at 22:35 Comment(0)
R
2

You're not passing the absolute path 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.

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.

Redraft answered 26/7, 2014 at 0:5 Comment(3)
On the tuttorial I watched it worked perfectly fine. I fixed it. Thanks for your input.Pinchcock
@Pinchcock It didn't "work perfectly fine". It worked because you invoked the tutorial in a particular way that made it appear to work.Dilatory
I downloaded Q4 to see if it would work. It did. So there is something that changed from Q4 to Q5. You were wrong. But, thank you.Pinchcock
H
1

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.

Hooknosed answered 9/10, 2015 at 13:43 Comment(0)
F
1

this is mostly the case if you close a file which is not opened - so just remove the close statement for example:

file->close();

just remove it ;)

Fang answered 16/12, 2020 at 20:37 Comment(0)
J
0

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.

Jesselyn answered 25/1, 2017 at 17:20 Comment(0)
L
0

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

Lehet answered 4/2, 2019 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.