Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QIODevice::read: device not open

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();
};
like image 879
user3878223 Avatar asked Jul 25 '14 19:07

user3878223


3 Answers

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.

like image 69
rela Avatar answered Sep 27 '22 19:09

rela


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.

like image 7
randag Avatar answered Oct 10 '22 05:10

randag


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.

like image 3
Kuba hasn't forgotten Monica Avatar answered Oct 10 '22 04:10

Kuba hasn't forgotten Monica