Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
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
Code:
#ifndef FILEMODEL_H
#define FILEMODEL_H
#include <QtGui>
{
QOBJECT
public:
~FileModel();
virtual QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const;
};
#endif // FILEMODEL_H
FileModel.cpp
Code:
#include "FileModel.h"
{
}
FileModel::~FileModel()
{
}
{
return QDirModel::index(row, column, parent
);
}
{
}
{
}
int FileModel
::rowCount(const QModelIndex &parent
) const {
}
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 }
{
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");
}
}
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
);
}
}
}
1 Attachment(s)
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
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.
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
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?
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
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!
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
Hmm, does it change anything if you implement columnCount() like this:
Code:
int FileModel
::columnCount(const QModelIndex &parent
) const {
if (count > 0)
{
count++; // Adding one column to QDirModel
}
return count;
}
?
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
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...
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
How about simply returning QDirModel::columnCount()+1? Or even "5"?
Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!
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!