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. when is a QString not really as QString - argument rejected by QFile
Qt 6.11 is out! See what's new in the release blog

when is a QString not really as QString - argument rejected by QFile

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.2k Views 2 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.
  • B Offline
    B Offline
    bitbasher
    wrote on last edited by
    #1

    hi folks

    this code (Qt V6, Windows 11)

    bool Game::saveGame(const QString filename, const FileFormat format) const {
        QFile fn(filename);
    

    is driving me nuts. As you can see i want to save data into a json file using the name in argument "filename"

    Visual Studio Code is reporting that QFile does not have a constructor that matches this argument list and that the argument is of type QString .. so .. the error might even be in VSC ??

    i have also tried filename.ToStdString() to see if it would use the QFile constructor that will take the file name that way .. same problem

    I tried making a file info QFileInfor infoname(filename) .. that works .. but then
    QFile nf( infoname.FilePath() ) still has no constructor for a QString

    help?

    Christian EhrlicherC 1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      And I'm guessing

      QFile fn;
      fn.setFileName(filename);
      

      gives you the same error?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bitbasher
        wrote on last edited by
        #3

        yes .. except the error flag is on the name of the QFile, "fn" .. but .. GitHUb CoPilot found a solution .. if i can screen shot the answer

        i changed the code to

        // Save Game object to file
        bool Game::saveGame( const QString filename, const FileFormat format) const {
            QFile fn();
            fn.setFileName( filename );
            if( fn.exists() ) {
                std::cerr << std::string("Error: File already exists: ") << fn.fileName().toStdString() << std::endl;
                return false;
            }
        

        and that changed the message to this

        expression must have class type but it has type "QFile (*)()"

        and now all the reference to "fn" are flagged as errors

        it took a beat but the AI Proposed this as the root of the problem

        The problem is that QFile fn() is being interpreted as a function declaration rather than object construction. Remove the parentheses to properly declare a QFile object.

        and it changed the code to this

        Screenshot 2025-11-04 162544.png

        so you see any difference? cause i do not .. but it is fixed so ..

        Christian EhrlicherC 1 Reply Last reply
        0
        • B bitbasher

          yes .. except the error flag is on the name of the QFile, "fn" .. but .. GitHUb CoPilot found a solution .. if i can screen shot the answer

          i changed the code to

          // Save Game object to file
          bool Game::saveGame( const QString filename, const FileFormat format) const {
              QFile fn();
              fn.setFileName( filename );
              if( fn.exists() ) {
                  std::cerr << std::string("Error: File already exists: ") << fn.fileName().toStdString() << std::endl;
                  return false;
              }
          

          and that changed the message to this

          expression must have class type but it has type "QFile (*)()"

          and now all the reference to "fn" are flagged as errors

          it took a beat but the AI Proposed this as the root of the problem

          The problem is that QFile fn() is being interpreted as a function declaration rather than object construction. Remove the parentheses to properly declare a QFile object.

          and it changed the code to this

          Screenshot 2025-11-04 162544.png

          so you see any difference? cause i do not .. but it is fixed so ..

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @bitbasher said in when is a QString not really as QString - argument rejected by QFile:

          so you see any difference? cause i do not .. but it is fixed so ..

          QFile fn();

          --> function call

          QFile fn;

          --> ctor

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • B bitbasher

            hi folks

            this code (Qt V6, Windows 11)

            bool Game::saveGame(const QString filename, const FileFormat format) const {
                QFile fn(filename);
            

            is driving me nuts. As you can see i want to save data into a json file using the name in argument "filename"

            Visual Studio Code is reporting that QFile does not have a constructor that matches this argument list and that the argument is of type QString .. so .. the error might even be in VSC ??

            i have also tried filename.ToStdString() to see if it would use the QFile constructor that will take the file name that way .. same problem

            I tried making a file info QFileInfor infoname(filename) .. that works .. but then
            QFile nf( infoname.FilePath() ) still has no constructor for a QString

            help?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @bitbasher said in when is a QString not really as QString - argument rejected by QFile:

            the error might even be in VSC ??

            Does the compiler complain or Visual studio code?

            bool saveGame(const QString filename) {
                QFile fn(filename);
                return fn.exists();
            }
            

            This compiles fine.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              For completeness, here's a copy of some examples from https://en.cppreference.com/w/cpp/language/initialization.html:

              #include <string>
               
              std::string s1;           // default-initialization
              std::string s2();         // NOT an initialization!
                                        // actually declares a function “s2”
                                        // with no parameter and returns std::string
              std::string s3 = "hello"; // copy-initialization
              std::string s4("hello");  // direct-initialization
              std::string s5{'a'};      // list-initialization (since C++11)
               
              char a[3] = {'a', 'b'}; // aggregate initialization
                                      // (part of list initialization since C++11)
              char& c = a[0];         // reference initialization
              

              So indeed there's a big difference between fn; and fn(); :-)

              1 Reply Last reply
              3
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #7

                In the C++ world don't pass mutable objects by const value. Pass them by const reference. ie

                const QString& filename
                

                QFile follows this paradigm...

                The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                1 Reply Last reply
                1

                • Login

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