Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile not being recognized
Forum Updated to NodeBB v4.3 + New Features

QFile not being recognized

Scheduled Pinned Locked Moved General and Desktop
19 Posts 5 Posters 6.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      Just close your editor and reopen your cpp file again.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        The main question is: does it compile ok?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          I think line 2 is wrong:

          @if(!mFile.open(QFile::ReadOnly | QFile::Text))@

          ...and it should be:

          @if(!mFile.open(QIODevice::ReadOnly | QIODevice::Text))@

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #6

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

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

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                holygirl
                wrote on last edited by
                #7

                Sam

                I tried that. Still not recognizing

                Andre

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    holygirl
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        holygirl
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            holygirl
                            wrote on last edited by
                            #13

                            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 :(

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              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?

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                tobias.hunger
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • H Offline
                                  H Offline
                                  holygirl
                                  wrote on last edited by
                                  #16

                                  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? 
                                  
                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    Sam
                                    wrote on last edited by
                                    #17

                                    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.

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      holygirl
                                      wrote on last edited by
                                      #18

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

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        andre
                                        wrote on last edited by
                                        #19

                                        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.

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved