PDA

View Full Version : Implement custom tree/table header view to support filtering on columns



prasad_N
23rd March 2016, 10:39
Hi all,
I want custom header view for my tree/table view like in the attachment, I want to show a line edit below the each column header so that use can filter on each column by entering text into the line edit.
I tried with delegate & came to know that delegate can't work with QHeaderView, so tried other ways also they really didn't work.

what is the best way to achieve this, If I need to customize the QHeaderView, what are the functions I need to re-implement ?

11808


Thanks in the advance.

prasad_N
31st March 2016, 11:51
hide existing view header.
Have a custom header, It is nothing but QTableView with 1 row & no.of columns your original view has. for this table view set Lined edits as cell widgets so that we will have line edits below headers where we can type & use as filter.
We need to connect few sig/slots in order to sync b/w this header & our main view.



class CustomHeader : public QHeaderView
{
Q_OBJECT
public:
CustomHeader(int columnCount = 0, QWidget *parent = 0)
{
QHBoxLayout *l = new QHBoxLayout(this);
l->setContentsMargins(0,0,0,0);
l->setSpacing(0);

table = new QTableWidget(1, columnCount, this);
l->addWidget(table);
table->setFixedHeight(55);

for(int i=0; i<columnCount; i++)
{
DelayedLineEdit* lineEdit = new DelayedLineEdit(this);
lineEdit->setDelay(700);
lineEdit->setIndex(i);
table->setCellWidget(0, i, lineEdit);
connect(lineEdit, SIGNAL(delayTextChanged(QString)), this, SLOT(applyColumnFilter(QString)));
}

table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
table->verticalHeader()->hide();

setLayout(l);
setFixedHeight(50);
}

private:
QTableWidget* table;
};



class treeView : public QTreeView
{
Q_OBJECT
public:
treeView(QWidget *parent = 0)
{
// Horizontal header treatement:
header()->hide();
m_header = new CustomHeader(m_treeModel->columnCount(), this);
}
~treeView();

protected:
void resizeEvent(QResizeEvent *event) {
setViewportMargins(0,60, 0, 0);
m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
}
void showEvent(QShowEvent *) {
setViewportMargins(0, m_header->sizeHint().height(), 0, 0);
m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
}

private:
CustomHeader *m_header;
};