QIODevice::read: device not open

26,861

Solution 1

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.

Solution 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.

Solution 3

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.

Share:
26,861
user3878223
Author by

user3878223

Updated on July 19, 2022

Comments

  • user3878223
    user3878223 almost 2 years

    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();
    };
    
  • user3878223
    user3878223 over 9 years
    On the tuttorial I watched it worked perfectly fine. I fixed it. Thanks for your input.
  • Kuba hasn't forgotten Monica
    Kuba hasn't forgotten Monica over 9 years
    @user3878223 It didn't "work perfectly fine". It worked because you invoked the tutorial in a particular way that made it appear to work.
  • user3878223
    user3878223 over 9 years
    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.