PDA

View Full Version : headerView of the QTableView not shown



momesana
20th September 2009, 17:18
Hi,
I have a QTableModel with 5 columns, but when I return labels for more than 4 columns the horizontal headerview isn't shown at all. Here is a simple application to highlight the problem. Save it as main.cpp and compile it using qmake -project && qmake && make



#include <QtGui>

class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
TableModel(QObject* parent = 0) : QAbstractTableModel(parent)
{
}
protected:
protected:
int rowCount(const QModelIndex & parent = QModelIndex()) const
{
return 20;
}

int columnCount(const QModelIndex & parent = QModelIndex()) const
{
return 5;
}

QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
{
if (index.isValid() && role == Qt::DisplayRole) {
return QString("index: %1,%2").arg(index.row()).arg(index.column());
}
return QVariant();
}

QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
{
if (orientation == Qt::Horizontal) {
switch(section) {
case 0: return QString("one");
case 1: return QString("two");
case 2: return QString("three");
case 3: return QString("four");
case 4: return QString("five");
}
}
return QVariant();
}
};

int main(int argc, char** argv)
{
QApplication app(argc, argv);
TableModel* model = new TableModel();
QTableView* view = new QTableView;
view->setModel(model);
view->show();
return app.exec();
}
#include "main.moc"

Any Ideas?

Thanx in advance
momesana

tituslup
21st September 2009, 09:04
The problem is, you have to emit the signal headerDataChanged(...) if you re implement this function. Just before returning the QVariant() will do just fine.

http://doc.trolltech.com/4.1/qabstractitemmodel.html#headerDataChanged

momesana
26th September 2009, 19:12
Thanks but that's not the solution. The reason for the issue was that I didn't check for the correct role (Qt::DisplayRole). Checking for the role solves the issue.

Cheers
momesana