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] Passing QFile as an argument
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Passing QFile as an argument

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 7.0k 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,

    I have been working on an application that uses a few lists of items(QTableViews/QStandardItemModel) which are saved and load from a .txt file and everything is working fine, I basically created the first one and then I just copied and pasted the code, changed the model and file names for the rest of them and everything is working fine. What I'm trying to do now to practice and to make my code more modular I basically created a class to try to reuse common functions such as loadDataToTableView, saveDataFromTableView etc., the problem I ran into is when I tried to pass QFile as an argument in one of these functions, when I try to use that method in other classes I get an error that says "calling a private constructor of class 'QFile' " I believe this has to do with the way QFile is declared in the Qt Libraries but my question is, is there a way to pass QFile as an argument?

    .h ItemListUtility Class
    @public:
    explicit ItemListUtility(QObject *parent = 0);
    void loadDataToTableView(QStandardItemModel *customModel, QFile customFile);@

    .cpp ItemListUtility Class
    @void ItemListUtility::loadDataToTableView(QStandardItemModel *customModel, QFile customFile)
    {
    QStandardItemModel *model = customModel;
    QFile file = customFile;

    // load data to tableView
    if (file.open(QIODevice::ReadOnly))
    {
       QDataStream stream(&file);
        qint32 rows, columns;
        stream >> rows >> columns;
        model->setRowCount(rows);
        model->setColumnCount(columns);
    
        for (int r = 0; r< rows ; ++r) {
               for (int c = 0; c < columns; c++) {
                   QStandardItem *newItems = new QStandardItem;
                   newItems->read(stream);
                   model->setItem(r, c, newItems);
               }
           }
    
     file.close();
    }
    

    }@

    Using ItemListUtility class later in a different class

    @ItemListUtility myUtility;
    myUtility.loadDataToTableView(myModel, myFile);@

    Again the ERROR reads as follow calling a private constructor of class 'QFile'

    Any Idea how to pass QFile as a parameter or what I'm trying to do is simply crazy and doesn't make sense?

    Thanks a lot

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      as you already noticed the copy constructor of QFile is not accessible for you. Thats the reason why passing a QFile as copy-parameter fails.

      Thus you either you have to pass the QFile as reference or as pointer to the method. Or simply just pass the path to the file as string.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        Awesome! Passing it by reference did the trick, Thanks a lot

        @void loadDataToTableView(QStandardItemModel *customModel, QFile &customFile); @

        @void ItemListUtility::loadDataToTableView(QStandardItemModel *customModel, QFile &customFile)
        {
        QStandardItemModel *model = customModel;
        QFile &file = customFile;
        ...
        }@

        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