Important: Please read the Qt Code of Conduct -

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(! | QFile::Text))
    qDebug() << "Could not open file for reading";



    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., it is green color)

  • Just close your editor and reopen your cpp file again.

  • The main question is: does it compile ok?

  • I think line 2 is wrong:

    @if(! | QFile::Text))@

    ...and it should be:

    @if(! | QIODevice::Text))@

  • Nonsense. Using QFile:: instead of QIODevice:: works just fine.

  • [quote author="Andre" date="1363250152"]Nonsense. Using QFile:: instead of QIODevice:: works just fine. [/quote]

    Sorry... I never used it like that before :)

  • Sam

    I tried that. Still not recognizing


    Yes it not only compiles but also runs! But the QFile part of the code does not give any output.

  • From the miniscule code section you posted, I can't figure out what output you're expecting exactly...

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

  • Ah, so the issue is about QtCreator, not about QFile as such and using that. Let me move the topic to Tools for clarity...

  • 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 :)

  • Honestly, while I get that it annoying, I would not spend so much time an effort on syntax highlighting issues in your tool. Your time would be better spend actually learning Qt :-)

  • But I need to get my app working and this highlighting problem is getting in the way. I wouldn't worry so much if my code worked sans the highlighting :(

  • Ok, so moving back to General & Desktop then. Your primary problem is your code not working, not the IDE.

    Could you show us more of your code, and explain to us what you expect your code to do?

  • Moderators

    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.

  • Hi again!

     I didn't exactly solve this problem but I noticed this when I was trying to solve. _QFile_ works well when used in _QMainWindow_ but now in _QWidget_. Is this the same for everyone? 

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

Log in to reply