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. [SOLVED] How to reuse QFile in multiple functions/methods
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to reuse QFile in multiple functions/methods

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.9k 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.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    How can I make a QFile visible in multiple functions/methods? In other words I would like to create a QFile and be able to use the same name in multiple places, for instance the save and load functions.

    I have tried...

    .h
    @private:
    QFile myFile(QString);@

    .cpp

    @//constructor
    myFile("/Users/userName/someFile.txt");@

    later in the program

    @if (myFile.open(QIODevice::WriteOnly))
    ...
    @
    Which of course doesn't work, I know this has to do with my poor understanding of the language C++.

    Thanks a lot

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      did you check what error you have ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fs_tigre
        wrote on last edited by
        #3

        Error!
        undefined reference to 'MainWindow::myFile(QString)

        Is it even possible to have a QFile globally visible? Sorry if my original question doesn't make any sense I just don't like to be repeating the same code over an over.

        I know I could use a string variable to hold the path and then just reuse the string but still doesn't feel right.

        .h File
        @#include <QFile>
        QString fileName;@

        .cpp File
        @#include <QFile>
        // cosntructor
        fileName = "/Users/userName/someFile.txt";@

        @// later in the program
        QFile myFile(fileName);@

        How do you guys usually do this?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I just re-read your code

          @
          private:
          QFile myFile(QString);
          @

          is not valid.

          @
          private:
          QFile myFile;
          @

          is

          and in your constructor:

          @
          myFile.setFileName("path_to_file");
          @

          EDIT: corrected sleepy head error

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fs_tigre
            wrote on last edited by
            #5

            First of all thank you for your reply.

            When I do what you suggested I get the following message

            'operator=' is a private member of 'QFile'

            Any Idea what could be wrong?

            @private:
            QFile mainDataFile;@

            @// constructor
            mainDataFile = QFile("/Users/UserName/someFile.txt");@

            @// later in the program
            if (mainDataFile.open(QIODevice::WriteOnly))@

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #6

              @
              MainWindow::MainWindow(...): mainDataFile (QFile("/Users/UserName/someFile.txt"))
              {
              }
              @

              or

              @
              mainDataFile.setFileName("/Users/UserName/someFile.txt");
              @

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                [quote author="1+1=2" date="1377656296"]@
                MainWindow::MainWindow(...): mainDataFile (QFile("/Users/UserName/someFile.txt"))
                {
                }
                @
                [/quote]

                won't work either, the copy constructor as well as the assignment operator are private (QFile's a QObject)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fs_tigre
                  wrote on last edited by
                  #8

                  @private:
                  QFile myFile;@
                  @// Constructor
                  myFile.setFileName("path_to_file");@

                  This works, thanks a lot for your help.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    You're welcome !

                    Please update the thread's title prepending solved so other forum users may know an answer has been found :)

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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