Custom QHeaderView doesn't adjust size
-
Hello
I'm subclassing QHeaderView based on this example and on @Oak77 's post here. The filters are working fine but I can't resize the headers or even click them for sorting. Method resizeColumnsToContents() also doesn't work. What might be wrong?
#include "filterheader.h" #include "copytable.h" #include <QHeaderView> #include <QTableView> #include <QLineEdit> #include <QScrollBar> FilterHeader::FilterHeader(QTableView *parent) : QHeaderView(Qt::Horizontal, parent) { _padding = 4; setStretchLastSection(true); setSectionResizeMode(QHeaderView::Stretch); setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); setSortIndicatorShown(false); connect(this, &QHeaderView::sectionResized, this, &FilterHeader::adjustPositions); connect(parent->horizontalScrollBar(), &QScrollBar::valueChanged, this, &FilterHeader::adjustPositions); } FilterHeader::FilterHeader(CopyTable *parent) : QHeaderView(Qt::Horizontal, parent) { _padding = 4; setStretchLastSection(true); setSectionResizeMode(QHeaderView::Stretch); setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); setSortIndicatorShown(false); connect(this, &QHeaderView::sectionResized, this, &FilterHeader::adjustPositions); connect(parent->horizontalScrollBar(), &QScrollBar::valueChanged, this, &FilterHeader::adjustPositions); show(); } void FilterHeader::setFilterBoxes(int count) { while (!_editors.empty()) { QLineEdit* editor = _editors.back(); _editors.pop_back(); editor->deleteLater(); } for (int i = 0; i < count; i++) { QLineEdit* editor = new QLineEdit(parentWidget()); editor->setPlaceholderText("Filter"); connect(editor, &QLineEdit::returnPressed, this, &FilterHeader::emitFilterActivated); _editors.push_back(editor); editor->show(); } adjustPositions(); } void FilterHeader::emitFilterActivated() { emit filterActivated(_editors.indexOf(sender())); } QSize FilterHeader::sizeHint() const { QSize size = QHeaderView::sizeHint(); if (!_editors.empty()) { int height = _editors[0]->sizeHint().height(); size.setHeight(size.height() + height + _padding); } return size; } void FilterHeader::updateGeometries() { if (!_editors.empty()) { int height = _editors[0]->sizeHint().height(); setViewportMargins(0, 0, 0, height + _padding); } else setViewportMargins(0, 0, 0, 0); QHeaderView::updateGeometries(); adjustPositions(); } void FilterHeader::adjustPositions() { for (int i = 0; i < _editors.size(); i++) { QLineEdit* editor = _editors[i]; int height = editor->sizeHint().height(); editor->move(sectionPosition(i) - offset() + qobject_cast<QTableView*>(parent())->verticalHeader()->width(), height + (_padding / 2)); editor->resize(sectionSize(i), height); } } QString FilterHeader::filterText(int index) const { if (index >= 0 && index < _editors.size()) return _editors[index]->text(); return QString(); } void FilterHeader::setFilterText(int index, const QString& text) { if (index >= 0 && index < _editors.size()) _editors[index]->setText(text); } void FilterHeader::clearFilters() { for (auto editor : _editors) editor->clear(); }
Code below at the end of the function that loads the tableView
FilterHeader* header = new FilterHeader(ui->tableView); ui->tableView->setHorizontalHeader(header); header->setFilterBoxes(model->columnCount()); connect(header, &FilterHeader::filterActivated, this, &Extratos::handleFilterActivated);
-
Hi,
setSectionResizeMode(QHeaderView::Stretch);
<- this is why you cannot manually resize.