Results 1 to 2 of 2

Thread: Implement custom tree/table header view to support filtering on columns

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Implement custom tree/table header view to support filtering on columns

    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 ?

    filter.PNG


    Thanks in the advance.
    Attached Images Attached Images
    Last edited by prasad_N; 23rd March 2016 at 11:08. Reason: updated contents
    Thanks :-)

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Implement custom tree/table header view to support filtering on columns

    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.

    Qt Code:
    1. class CustomHeader : public QHeaderView
    2. {
    3. Q_OBJECT
    4. public:
    5. CustomHeader(int columnCount = 0, QWidget *parent = 0)
    6. {
    7. QHBoxLayout *l = new QHBoxLayout(this);
    8. l->setContentsMargins(0,0,0,0);
    9. l->setSpacing(0);
    10.  
    11. table = new QTableWidget(1, columnCount, this);
    12. l->addWidget(table);
    13. table->setFixedHeight(55);
    14.  
    15. for(int i=0; i<columnCount; i++)
    16. {
    17. DelayedLineEdit* lineEdit = new DelayedLineEdit(this);
    18. lineEdit->setDelay(700);
    19. lineEdit->setIndex(i);
    20. table->setCellWidget(0, i, lineEdit);
    21. connect(lineEdit, SIGNAL(delayTextChanged(QString)), this, SLOT(applyColumnFilter(QString)));
    22. }
    23.  
    24. table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    25. table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    26. table->verticalHeader()->hide();
    27.  
    28. setLayout(l);
    29. setFixedHeight(50);
    30. }
    31.  
    32. private:
    33. QTableWidget* table;
    34. };
    35.  
    36.  
    37.  
    38. class treeView : public QTreeView
    39. {
    40. Q_OBJECT
    41. public:
    42. treeView(QWidget *parent = 0)
    43. {
    44. // Horizontal header treatement:
    45. header()->hide();
    46. m_header = new CustomHeader(m_treeModel->columnCount(), this);
    47. }
    48. ~treeView();
    49.  
    50. protected:
    51. void resizeEvent(QResizeEvent *event) {
    52. setViewportMargins(0,60, 0, 0);
    53. m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
    54. }
    55. void showEvent(QShowEvent *) {
    56. setViewportMargins(0, m_header->sizeHint().height(), 0, 0);
    57. m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
    58. }
    59.  
    60. private:
    61. CustomHeader *m_header;
    62. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by prasad_N; 31st March 2016 at 11:52. Reason: missing [code] tags
    Thanks :-)

Similar Threads

  1. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 1
    Last Post: 6th June 2015, 17:26
  2. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 6
    Last Post: 18th May 2015, 08:18
  3. Replies: 1
    Last Post: 10th May 2011, 19:58
  4. Replies: 0
    Last Post: 4th May 2011, 13:33
  5. how to move between columns in tree view?
    By Scott Shiff in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2007, 09:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.