QFile not being recognized
-
Hey everybody!
I'm using _QFile_ to manipulate with files but somehow _QFile_ does not seem to be recognized by Qt. I can't figure out why. Please help me. My code is
@
QFile mFile(fileName);
if(!mFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "Could not open file for reading";
return;
}
else
{} mFile.close();
}
@and I have included
@
#include <QFile>
@and my .pro file has
@
CONFIG += qt
QT += core gui
@The QFile in my .cpp file is not showing up in the usual purple color in Qt. (w.r.t. qt-project.org, it is green color)
-
I think line 2 is wrong:
@if(!mFile.open(QFile::ReadOnly | QFile::Text))@
...and it should be:
@if(!mFile.open(QIODevice::ReadOnly | QIODevice::Text))@
-
[quote author="Andre" date="1363250152"]Nonsense. Using QFile:: instead of QIODevice:: works just fine. [/quote]
Sorry... I never used it like that before :)
-
What I'm trying to say is that my Qt Creator is not picking up the QFile keyword. QFile, IODevice etc are usually purple in color in Qt, right? In mine, it's just black so I'm guessing the creator is not able to detect that QFile is actually an inbuilt class or something. I'm sorry I'm unable to explain my problem clearly to you.
As for the code, all I'm trying to do is load a file and read it. That's all. -
Thanks Andre
Why I was hesitating it to post this question in Tools was because I have another program written in the same Qt Creator where the QFile is being recognized and the application runs smoothly. However this problem persists only in this application. I checked several times to see if I missed any header file or anything in the .pro but I just can't seem to figure out what's wrong. Maybe a fresh pair of eyes will help find the bug :)
-
Creator is only ever expected to do proper code highlighting for syntactically correct code. For everything else it will guess, but that guess may or may not be close to what you think your code means.
The code you give is obviously faulty as it is just a random section of your file pasted here. Can you at least provide the complete method and the complete compiler output on a clean rebuild? Most likely the build fails and you are running a old version of your code.
-
It works with QWidget as well, how about you upload your project somewhere and provide the link here, so that we can reproduce the same behaviour.
For me most of the times if the class is not recognised by intellisense, i close the respective file and re-open it again in the editor. Also sometimes(not sure) having multiple projects in the same session creates ambiguity.
-
Tobias,
The code may not be faulty because when it gives me the correct output when I use it in a _QMainWindow_ and no results when used with _QWidget_. Yes, I did paste only a part of the code here because my project is too huge and I didn't want to include a lot of code.
What I've pasted is a cleaned and rebuilt version :)
-
Did you forget the #include <QFile> in your widget-based version?
Anyway, QFile works regardless in which other class you use it. If it doesn't, your code is faulty. The code you pasted it not "cleaned", it is stripped of all useful information. If you can't paste your full code, then please create a small, self-contained example showing your problem.