PDA

View Full Version : Subclassing QDirModel to Add Extra Columns Results in Empty Columns!



killerwookie99
13th August 2008, 15:10
Hello I am new to Qt and can't figure out why I can add a column header, but the data never populates...It always sends ::data indexes 0-3 as if it doesn't know there is an additional column, but I see 0-4 columns:
Name | Size | Type | Date | [My File Attribute]

What I am doing wrong? Here is my header and implementation file:

FileModel.h


#ifndef FILEMODEL_H
#define FILEMODEL_H

#include <QtGui>

class FileModel : public QDirModel
{
QOBJECT

public:

FileModel(QWidget *parent = 0)

~FileModel();

virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;

virtual QModelIndex index(const QString &path, int column = 0) const;

virtual QModelIndex parent(const QModelIndex &index) const;

virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;

virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;

virtual QVariant data(const QModelIndex &index, int role = Qt:DisplayRole) const;

virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
};
#endif // FILEMODEL_H


FileModel.cpp


#include "FileModel.h"

FileModel::FileModel(QWidget *parent) : QDirModel(parent)
{
}

FileModel::~FileModel()
{
}

QModelIndex FileModel::index(int row, int column, const QModelIndex &parent) const
{
return QDirModel::index(row, column, parent);
}

QModelIndex FileModel::index(const QString &path, int column) const
{
return QDirModel::index(path, column);
}

QModelIndex FileModel::parent(const QModelIndex &index) const
{
return QDirModel::parent(index);
}

int FileModel::rowCount(const QModelIndex &parent) const
{
return QDirModel::rowCount(parent);
}

int FileModel::columnCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
{
return 0; // This is a safe return??
}
return QDirModel::columnCount(parent) + 1; // Adding one column to QDirModel
}

QVariant FileModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
if (index.column() == QDirModel::columnCount()) // I never ever see 4 for index.column() count, it's ok for QDirModel::columnCount() to return 4, but this always fails.
return tr("File Attribute");
}
return QDirModel::data(index, role);
}


QVariant FileModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch (section)
{
case 4:
return tr("File Attribute Header");
default:
return QDirModel::headerData(section, orientation, role);
}
}
return QVariant();
}

jpn
13th August 2008, 15:21
Works for me after fixing all the compilation errors:

PS. What's the point overriding all those index, parent etc. methods? By the way, one of them is not even virtual.

killerwookie99
13th August 2008, 15:25
Sorry about the compilation errors, the code is on a machine that's hard to get things from. What version of Qt are you using?

jpn
13th August 2008, 15:26
I'm using Qt/X11 4.4.0.

killerwookie99
13th August 2008, 15:31
I am using 4.3.2 perhaps worth an upgrade?

I just remember reading to implement all those functions...for now I just put the calls to QDirModel...what are some things you would recommend as I am only just starting with Qt.

Appreciate your quick responses as well!

jpn
13th August 2008, 15:36
Hmm, does it change anything if you implement columnCount() like this:


int FileModel::columnCount(const QModelIndex &parent) const
{
int count = QDirModel::columnCount(parent);
if (count > 0)
{
count++; // Adding one column to QDirModel
}
return count;
}

?

killerwookie99
13th August 2008, 15:44
No that did not change it, the other curious thing is that it only hits that function maybe a total of 15 times for a listing of 1000 files...

wysota
13th August 2008, 21:49
How about simply returning QDirModel::columnCount()+1? Or even "5"?

killerwookie99
14th August 2008, 15:48
Ok, the problem was Qt 4.3.2 and maybe other versions. I am on solaris I upgraded yesterday (on a sparc! :eek:) Anyway, you are correct it works. Should've asked the night before I kept thinking it was something I was doing, but all the reading was probably a good idea anyway. Thanks y'all!