invalid use of incomplete type 'class QFileSystemModel' when using a class for a model



  • Hey all, brand new to both QT as well as C++ and was looking for some assistance with an issue I'm having. Sorry in advance if this is the wrong section of the forum, please let me know where I should direct my post to in that case.

    I'm having an issue with using the QFileSystemModel with the QTreeView widget I have set up in UI. The exact error I get is "invalid use of incomplete type 'class QFileSystemModel'
    ui->tvListBox->setModel((m_model->getModel()));
    ^
    "

    When I compile, I get the following error and am feeling really lost as I don't understand why it won't recognize the QFileSystemModel I set up. It works fine when I use the QFileSystemModel from within the "configTab" class directly but, I'm guessing I goofed at some point when I pushed the actual model into another class named "configModel".

    Here's the full project, in case my code isn't clear https://www.dropbox.com/s/4cm144nkuf1vw4w/Editor.zip?dl=0

    Sorry in advance for the code dump.

    Model/configmodel.h

    #ifndef CONFIGMODEL_H
    #define CONFIGMODEL_H
    #include <QWidget>
    
    class QFileSystemModel;
    class QAbstractItemModel;
    class QDir;
    
    class configModel
    {
    public:
        configModel();
        QFileSystemModel getModel();
    
    private:
        QFileSystemModel *m_fileModel;
    };
    #endif // CONFIGMODEL_H
    

    Model/configmodel.cpp

    #include "configmodel.h"
    #include "QFileSystemModel"
    #include "QDir"
    
    configModel::configModel() :
        m_fileModel(new QFileSystemModel) //create the file model
    {
        //set test model
        m_fileModel->setRootPath(QDir::currentPath());
    }
    
    QFileSystemModel configModel::getModel()
    {
        return m_fileModel;
    }
    

    View/configtab.h

    #ifndef CONFIGTAB_H
    #define CONFIGTAB_H
    
    #include <QWidget>
    
    namespace Ui {
        class ConfigTab;
    }
    
    class configModel;
    
    class ConfigTab : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit ConfigTab(QWidget *parent = 0);
        ~ConfigTab();
    
    private:
        Ui::ConfigTab *ui;
        configModel *m_model;
    };
    
    #endif // CONFIGTAB_H
    

    View/configtab.cpp

    #include "configtab.h"
    #include "ui_configtab.h"
    #include "Model/configmodel.h"
    
    
    ConfigTab::ConfigTab(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ConfigTab),
        m_model(new configModel) //create test model
    {
        ui->setupUi(this);
    
        //load model into tree
        ui->tvListBox->setModel((m_model->getModel()));
    }
    
    ConfigTab::~ConfigTab()
    {
        delete ui;
    }
    

    View/mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
        class MainWindow;
    }
    
    class ConfigTab;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent=0);
        ~MainWindow();
    
    private:   
        Ui::MainWindow *ui;
        ConfigTab* m_config;
    };
    
    #endif // MAINWINDOW_H
    

    View/mainwindow.cpp

    #include "View\mainwindow.h"
    #include "View\configtab.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        m_config(new ConfigTab(this))
    {
        ui->setupUi(this);
        ui->loConfig->addWidget(m_config);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    main.cpp

    #include "View\mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    Forms/View/configtab.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>ConfigTab</class>
     <widget class="QWidget" name="ConfigTab">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QTreeView" name="tvListBox"/>
       </item>
       <item>
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item alignment="Qt::AlignTop">
        <widget class="QPushButton" name="btnAdd">
         <property name="text">
          <string>Add</string>
         </property>
        </widget>
       </item>
       <item alignment="Qt::AlignTop">
        <widget class="QPushButton" name="btnDel">
         <property name="text">
          <string>Remove</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    Forms/View/mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="wdMain">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QTabWidget" name="wdDisplay">
          <property name="currentIndex">
           <number>0</number>
          </property>
          <widget class="QWidget" name="tbConfig">
           <attribute name="title">
            <string>Configure</string>
           </attribute>
           <widget class="QWidget" name="verticalLayoutWidget">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>10</y>
              <width>371</width>
              <height>191</height>
             </rect>
            </property>
            <layout class="QVBoxLayout" name="loConfig"/>
           </widget>
          </widget>
          <widget class="QWidget" name="tbSettings">
           <attribute name="title">
            <string>Settings</string>
           </attribute>
           <widget class="QWidget" name="verticalLayoutWidget_2">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>10</y>
              <width>371</width>
              <height>191</height>
             </rect>
            </property>
            <layout class="QVBoxLayout" name="loSettings"/>
           </widget>
          </widget>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>400</width>
         <height>21</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    


  • @NoticeMeSenpai

    like this:

    in configtab.cpp
    Add it #include <QFileSystemModel>

    in configmodel.h
    Modify QFileSystemModel *getModel();

    in configmodel.cpp
    Modify QFileSystemModel *configModel::getModel()



  • @Devopia53

    That worked, thanks so much!

    Let me know if I understand it correctly with the beneath or if I'm wrong somewhere please correct me.

    We include QFileSystemModel into configtab.cpp so that it knows how to handle the dependency of the setModel method.

    We change the getModel method to a pointer of (I actually did not realize you could make a method a pointer lmao) QFileSystemModel because the setModel method requires a pointer and not a direct reference?


  • Lifetime Qt Champion

    Hi,

    You include a header where you actually use a class.

    In your header, as along as you are declaring pointers to the classes you use, you can use forward declaration as you did.

    As for getModel, your original declaration would return an object of type QFileSystemModel. The error you got came from the fact that you were returning an object, therefore, the compiler has to know it and you would have to include the header for that class there. However, even if you added the include, it would not have worked because it is not possible to copy QObject based classes.

    On a side note, you can also make that method const as you are not modifying your class when calling it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.