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] Are models (which inherited from QAbstractItemModel) only working on 'Qt GUI Application'?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k 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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hareen Laks
        wrote on last edited by
        #3

        @ sierdzio,

        Thank you for your answer. It is really helpful.

        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