[solved] Are models (which inherited from QAbstractItemModel) only working on 'Qt GUI Application'?



  • Hi all,

    I would like to learn about Model/View programming. So I'm trying some examples.

    I created a 'Qt Console Application' and edited the code as below.

    @#include <QtCore/QCoreApplication>
    #include <QFileSystemModel>
    #include <QDebug>
    #include <QDir>
    #include <QModelIndex>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QFileSystemModel *model = new QFileSystemModel;
    QModelIndex *parent_index = model->index(QDir::currentPath());
    int num_rows = model->rowCount(parent_index);
    
    for (int row = 0; row < num_rows; ++row) {
       QModelIndex *index = model->index(row,0,parent_index);
       qDebug() << model->data(index,Qt::DisplayRole);
    }
    
    return a.exec&#40;&#41;;
    

    }@

    But unfortunately, I'm getting following errors.

    @.\ModelExample\main.cpp:2:28: error: QFileSystemModel: No such file or directory
    ..\ModelExample\main.cpp: In function 'int main(int, char**)':
    ..\ModelExample\main.cpp:13: error: 'QFileSystemModel' was not declared in this scope
    ..\ModelExample\main.cpp:13: error: 'model' was not declared in this scope
    ..\ModelExample\main.cpp:13: error: expected type-specifier before 'QFileSystemModel'
    ..\ModelExample\main.cpp:13: error: expected ';' before 'QFileSystemModel'@

    It appear as my app not recognized the 'QFileSystemModel' class or Models cannot use here.

    Can anyone help me?

    Thanks for reading.


  • Moderators

    QFileSystemModel is part of QtWidgets module, so you need to add this to your .pro file:
    @
    QT += widgets
    @

    However, MVC is all about GUIs and displaying the information provided by the modeal and controller classes. So using this in a console application seems a bit pointless.



  • @ sierdzio,

    Thank you for your answer. It is really helpful.


Log in to reply
 

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