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. How to declare a QFile as a member class
Forum Updated to NodeBB v4.3 + New Features

How to declare a QFile as a member class

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 2.3k 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.
  • A Offline
    A Offline
    aurquiel
    wrote on last edited by A Former User
    #1

    In the header of the class i did

    header

    QFile file;                                                   //File to write 
    QDataStream out;                                 //To write in the file
    
    void create_file();                                      //Function  to create in a file
    void write_file(unsigned char *p);        //Function  to write in a file receive a pointer to the array buffer void delete_file();                                       //Function  to delete in a file
    

    cpp

    //Function to create a File
    void backend::create_file()
    {
        //Name of the file
        file("text.txt");
    
        //Creation of the file, mode writeonly
        file.open(QIODevice::WriteOnly);
    
    }
    
    //Function to write in a File
    void backend::write_file(unsigned char *p)
    {
        out(&file);
    }
    
    //Function to delete a File
    void backend::delete_file()
    {
    
    }
    

    In the header i can't declare mi fle like this QFile file("text.txt");
    And in the cpp file("text.txt"); gives me error

    /home/pasante/Escritorio/PIZZA/backend.cpp:1521: error: no match for call to ‘(QFile) (const char [9])’
    file("text.txt");
    ^

    out(&file); this gives me error too, but i can't put it in the headrer

    /home/pasante/Escritorio/PIZZA/backend.cpp:1531: error: no match for call to ‘(QDataStream) (QFile*)’
    out(&file);
    ^

    mrjjM 1 Reply Last reply
    0
    • A aurquiel

      In the header of the class i did

      header

      QFile file;                                                   //File to write 
      QDataStream out;                                 //To write in the file
      
      void create_file();                                      //Function  to create in a file
      void write_file(unsigned char *p);        //Function  to write in a file receive a pointer to the array buffer void delete_file();                                       //Function  to delete in a file
      

      cpp

      //Function to create a File
      void backend::create_file()
      {
          //Name of the file
          file("text.txt");
      
          //Creation of the file, mode writeonly
          file.open(QIODevice::WriteOnly);
      
      }
      
      //Function to write in a File
      void backend::write_file(unsigned char *p)
      {
          out(&file);
      }
      
      //Function to delete a File
      void backend::delete_file()
      {
      
      }
      

      In the header i can't declare mi fle like this QFile file("text.txt");
      And in the cpp file("text.txt"); gives me error

      /home/pasante/Escritorio/PIZZA/backend.cpp:1521: error: no match for call to ‘(QFile) (const char [9])’
      file("text.txt");
      ^

      out(&file); this gives me error too, but i can't put it in the headrer

      /home/pasante/Escritorio/PIZZA/backend.cpp:1531: error: no match for call to ‘(QDataStream) (QFile*)’
      out(&file);
      ^

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @aurquiel
      Hi
      QFile can also set the info via functions
      http://doc.qt.io/qt-5/qfile.html#setFileName

      and for out(&file);
      http://doc.qt.io/qt-5/qdatastream.html#setDevice

      The reason it dont work is that the first syntax is the constructor and that cant work the same when in .h

      but both classes have function to set it.

      Alternativ u can new them but its better with the setX functions. ( IMHO)

      1 Reply Last reply
      2

      • Login

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