10
votes

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();
};
6
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}Lighthat
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();};user3878223
The code you gave us does not help, we need to see how you're using open()Lighthat

6 Answers

7
votes

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.

3
votes

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.

1
votes

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.

0
votes

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.

0
votes

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

0
votes

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 ;)