Important: Please read the Qt Code of Conduct -

[SOLVED] Passing QFile as an argument

  • 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
    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 (
       QDataStream stream(&file);
        qint32 rows, columns;
        stream >> rows >> columns;
        for (int r = 0; r< rows ; ++r) {
               for (int c = 0; c < columns; c++) {
                   QStandardItem *newItems = new QStandardItem;
                   model->setItem(r, c, newItems);


    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

  • Moderators

    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.

  • 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;

Log in to reply