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>
{
QOBJECT
public:
~FileModel();
virtual QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
FileModel.cpp
#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
);
}
}
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks