Results 1 to 14 of 14

Thread: Creating a QAbstractItemModel for QTreeView

  1. #1
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Creating a QAbstractItemModel for QTreeView

    hi

    - I want know how subclassin a QAbstractItemModel to create a model for QTreeView

    - how can i create my model like QDirModel using QAbstractItemModel ? i want display more informations of files and directories in a QTreeView (owner .....)

    thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating a QAbstractItemModel for QTreeView

    Quote Originally Posted by hbill View Post
    - I want know how subclassin a QAbstractItemModel to create a model for QTreeView
    There are many resources available on that topic. Take a look at the [wiki=QAbstractItemModel]wiki[/wiki].

    - how can i create my model like QDirModel using QAbstractItemModel ? i want display more informations of files and directories in a QTreeView (owner .....)
    Subclass QDirModel and reimplement appropriate methods calling base class implementations where needed.

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a QAbstractItemModel for QTreeView

    Quote Originally Posted by hbill View Post
    hi

    - I want know how subclassin a QAbstractItemModel to create a model for QTreeView
    model like QDirModel
    thank you
    you can build tree from QStandardItemModel begin to read xml tree or sql tree demo
    http://www.klempert.de/nested_sets/demo/

    run on mysql or on sqlite3
    each level become a QList<QStandardItem *>

    model = new QStandardItemModel();
    model->invisibleRootItem()->appendRow(QList<QStandardItem *>);
    sublevel the first QStandardItem appendRow(QList<QStandardItem *>)

    code on http://www.qtforum.de/forum/viewtopic.php?t=5845

    To swap from one model to other you can use:
    http://fop-miniscribus.googlecode.co....0/ModelSwap.h
    http://fop-miniscribus.googlecode.co.../ModelSwap.cpp
    I use this to print on tree or tableview to QTextDocument

  4. #4
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QAbstractItemModel for QTreeView

    thank you
    can you help me to subclassing QDirModel,

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Creating a QAbstractItemModel for QTreeView

    Quote Originally Posted by hbill View Post
    can you help me to subclassing QDirModel,
    What's causing problems? What did you try so far? Let's say you wanted to add one column:
    Qt Code:
    1. class MyDirModel : public QDirModel
    2. {
    3. public:
    4. ...
    5. // reimplement columnCount()
    6. virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
    7. ...
    8. };
    9.  
    10. int MyDirModel::columnCount(const QModelIndex& parent) const
    11. {
    12. // call base class implementation and add 1 to whatever it returns
    13. return QDirModel::columnCount(parent) + 1;
    14. }
    To copy to clipboard, switch view to plain text mode 
    This is just an example to get you going. Most likely you don't want to return 1 as column count when the base class returns 0. Next thing to do is to reimplement data() and make it handle this additional column.
    J-P Nurmi

  6. #6
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QAbstractItemModel for QTreeView

    hi
    Qt Code:
    1. MyDirModel:: MyDirModel(QObject *parent)
    2. : QDirModel(parent)
    3. {
    4. removeColumn(QDirModel::columnCount() - 1);
    5. }
    6.  
    7.  
    8. MyDirModel::~MyDirModel()
    9. {
    10. }
    11.  
    12.  
    13. int MyDirModel::columnCount(const QModelIndex& parent) const
    14. {
    15. return QDirModel::columnCount(parent) + 2 ;
    16. }
    17.  
    18. QVariant MyDirModel::data(const QModelIndex & index, int role) const
    19. {
    20. if( role==Qt::DisplayRole )
    21. {
    22. if( index.column()==QDirModel::columnCount() )
    23. return fileInfo( index ).owner();
    24. else
    25. if( index.column()==QDirModel::columnCount() +1 )
    26. return fileInfo( index ).group();
    27. }
    28. return QDirModel::data(index, role);
    29. }
    To copy to clipboard, switch view to plain text mode 
    the column "last modified" was not removed and the new columns are empty
    Last edited by hbill; 30th March 2008 at 14:41.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Creating a QAbstractItemModel for QTreeView

    QDirModel does not implement QAbstractItemModel::removeColums() so you can't use it. Either hide it from the view or make the subclass return alternative data for that particular column. Start with checking out if anything shows up in those columns when you return a fixed test string. After that, continue with investigating whether the QFileInfo returned by fileInfo() is ok.
    J-P Nurmi

  8. #8
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QAbstractItemModel for QTreeView

    hi

    Qt Code:
    1. QVariant MyDirModel::data(const QModelIndex & index, int role) const
    2. {
    3. QMessageBox msgBox;
    4. //msgBox.exec();//ok
    5. if( role==Qt::DisplayRole )
    6. {
    7. //msgBox.exec();//ok
    8. if( index.column()==QDirModel::columnCount() )//problem
    9. {
    10. //msgBox.exec();//not ok
    11. return fileInfo( index ).owner();
    12. }
    13. else
    14. if( index.column()==QDirModel::columnCount() +1 )//problem
    15. return fileInfo( index ).group();
    16. else
    17. if( index.column()==QDirModel::columnCount() -1 )
    18. {
    19. //msgBox.exec();//ok
    20. return fileInfo( index ).owner();//ok
    21. }
    22. }
    23. return QDirModel::data(index, role);
    24. }
    To copy to clipboard, switch view to plain text mode 
    i have a problem with the new columns !!!

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Creating a QAbstractItemModel for QTreeView

    Quote Originally Posted by hbill View Post
    i have a problem with the new columns !!!
    Could you be a bit more verbose about the problem, please?
    J-P Nurmi

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating a QAbstractItemModel for QTreeView

    Quote Originally Posted by jpn View Post
    Could you be a bit more verbose about the problem, please?
    Yeah... a more verbose version is "they are not working!"

    Sorry, couldn't resist myself...

  11. #11
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QAbstractItemModel for QTreeView

    the test
    Qt Code:
    1. if( index.column()==id_column )
    To copy to clipboard, switch view to plain text mode 
    is virified if id_column <QDirModel::columnCount()
    but no if id_column >=QDirModel::columnCount()

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating a QAbstractItemModel for QTreeView

    Could you check if your columnCount() method is called at all?

  13. #13

    Default Re: Creating a QAbstractItemModel for QTreeView

    Did you ever find a solution to the column data not being added?

  14. #14
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QAbstractItemModel for QTreeView

    Your problem may have been similar to mine. What version of Qt are you using. I upgraded from 4.3.2 to 4.4.1 on solaris, and my problems went away.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.