PDA

View Full Version : QTableView Horizontal Header's Width



araglin
19th December 2008, 14:32
Users of my application are able to resize header's columns as they want.
And resized widths of columns are to save in QSettings to restore user's favorite size next time. But something wrong with next code, please help:

First of all, here is a part of constructor of MainWindow:


eveModel = new EveModel(this);
header = tableView->horizontalHeader();
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(eveModel);
proxyModel->setDynamicSortFilter(true);
header->setResizeMode(QHeaderView::Interactive);
header->setStretchLastSection(true);
tableView->setModel(proxyModel);


There is no effect from any methods like QHeaderView::resizeSection() or ...setSectionHidden(). But ...setStretchLastSection() is work. I don't understand why!

Then headerData(), a part of EveModel:


QVariant EveModel::headerData(int section, Qt::Orientation orientation, int role /* =Qt::DisplayRole */ ) const {

if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole)
return m_headers->count() > section ? m_headers->at(section) : QAbstractTableModel::headerData(section, orientation, role);
if (role == Qt::SizeHintRole) {
return QSize(1, 30);
}
}
else if (orientation == Qt::Vertical) {
if (role == Qt::DisplayRole)
return QVariant();
}
return QAbstractTableModel::headerData(section, orientation, role);
}


In the code above I added "return QSize(1,30);" string, but it actually has influence only at header's height , not width (regardless of params).

I think that I misunderstand the whole concept of such kind of operation in Qt. Would you please explain me?

wysota
20th December 2008, 23:16
SizeHintRole is only a hint, it might not be followed. In general Qt will try to treat it as minimum width/height when arranging items. If you want to change the behaviour of headers, I suggest doing that after you apply the model, not before.

araglin
20th December 2008, 23:58
Thank you!

I've added

header->setModel(eveModel);
and now it's going better!

wysota
21st December 2008, 08:54
Simply add the model to the view (not to the header) before you modify the header. setModel() on the view sets the model on the header as well. You shouldn't do it manually.