PDA

View Full Version : Creating a QAbstractItemModel for QTreeView



hbill
27th March 2008, 22:50
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

wysota
27th March 2008, 22:59
- 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.


- 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.

patrik08
27th March 2008, 23:24
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.com/svn/trunk/http_debugger.1.0/ModelSwap.h
http://fop-miniscribus.googlecode.com/svn/trunk/http_debugger.1.0/ModelSwap.cpp
I use this to print on tree or tableview to QTextDocument

hbill
29th March 2008, 00:24
thank you
can you help me to subclassing QDirModel, :(

jpn
29th March 2008, 08:45
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:


class MyDirModel : public QDirModel
{
public:
...
// reimplement columnCount()
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
...
};

int MyDirModel::columnCount(const QModelIndex& parent) const
{
// call base class implementation and add 1 to whatever it returns
return QDirModel::columnCount(parent) + 1;
}

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.

hbill
30th March 2008, 13:35
hi


MyDirModel:: MyDirModel(QObject *parent)
: QDirModel(parent)
{
removeColumn(QDirModel::columnCount() - 1);
}


MyDirModel::~MyDirModel()
{
}


int MyDirModel::columnCount(const QModelIndex& parent) const
{
return QDirModel::columnCount(parent) + 2 ;
}

QVariant MyDirModel::data(const QModelIndex & index, int role) const
{
if( role==Qt::DisplayRole )
{
if( index.column()==QDirModel::columnCount() )
return fileInfo( index ).owner();
else
if( index.column()==QDirModel::columnCount() +1 )
return fileInfo( index ).group();
}
return QDirModel::data(index, role);
}
the column "last modified" was not removed and the new columns are empty :o

jpn
30th March 2008, 15:58
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.

hbill
3rd April 2008, 15:49
hi



QVariant MyDirModel::data(const QModelIndex & index, int role) const
{
QMessageBox msgBox;
//msgBox.exec();//ok
if( role==Qt::DisplayRole )
{
//msgBox.exec();//ok
if( index.column()==QDirModel::columnCount() )//problem
{
//msgBox.exec();//not ok
return fileInfo( index ).owner();
}
else
if( index.column()==QDirModel::columnCount() +1 )//problem
return fileInfo( index ).group();
else
if( index.column()==QDirModel::columnCount() -1 )
{
//msgBox.exec();//ok
return fileInfo( index ).owner();//ok
}
}
return QDirModel::data(index, role);
}

i have a problem with the new columns !!! :confused:

jpn
3rd April 2008, 21:04
i have a problem with the new columns !!! :confused:
Could you be a bit more verbose about the problem, please?

wysota
3rd April 2008, 22:03
Could you be a bit more verbose about the problem, please?

Yeah... a more verbose version is "they are not working!" :D

Sorry, couldn't resist myself...

hbill
4th April 2008, 00:10
the test

if( index.column()==id_column )
is virified if id_column <QDirModel::columnCount()
but no if id_column >=QDirModel::columnCount()

wysota
4th April 2008, 00:15
Could you check if your columnCount() method is called at all?

crimsonphire
13th August 2008, 06:16
Did you ever find a solution to the column data not being added?

killerwookie99
14th August 2008, 16:01
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.