Results 1 to 8 of 8

Thread: How to set filter option in QTableWidget

  1. #1
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to set filter option in QTableWidget

    Hi,
    In my application I have QtableWidget displaying multiple rows , line edit to enter string and push button, Requirement says upon clicking on push button Same Qtable Widget should show only those rows which are having string entered into line edit.

    I thought of using QSortFilterProxy Model but QTableWidget is having SetModel method private so i am unable to use QSortFilterProxy Model in this case .Please let me know how to implement Filter option in QTable Widget

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set filter option in QTableWidget

    You can either migrate to QTableView, and user QSortFilterProxy Model (or)

    write a filter function,
    1. use QList<QTableWidgetItem *> QTableWidget::findItems() to get all the matching items in the table.
    2. iterate through the list and hide all the corresponding row numbers

    Qt Code:
    1. //example
    2. void sort(QString filter)
    3. {
    4. QList<QTableWidgetItem *> items = tableWidget->findItems(filter, Qt::MatchContains);
    5.  
    6. for(int i = 0; i < items.count(); i++)
    7. tableWidget->hideRow(items.at(i).row());
    8. }
    To copy to clipboard, switch view to plain text mode 

    Note: you need to un-hide the rows at some point, then what?... then write un-hide code some where...

  3. The following user says thank you to Santosh Reddy for this useful post:

    johnMick (22nd July 2011)

  4. #3
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set filter option in QTableWidget

    Quote Originally Posted by Santosh Reddy View Post
    You can either migrate to QTableView, and user QSortFilterProxy Model (or)

    write a filter function,
    1. use QList<QTableWidgetItem *> QTableWidget::findItems() to get all the matching items in the table.
    2. iterate through the list and hide all the corresponding row numbers

    Qt Code:
    1. //example
    2. void sort(QString filter)
    3. {
    4. QList<QTableWidgetItem *> items = tableWidget->findItems(filter, Qt::MatchContains);
    5.  
    6. for(int i = 0; i < items.count(); i++)
    7. tableWidget->hideRow(items.at(i).row());
    8. }
    To copy to clipboard, switch view to plain text mode 

    Note: you need to un-hide the rows at some point, then what?... then write un-hide code some where...
    when the code is compiled the following error is shown:

    error: request for member 'row' in 'items.QList<T>::at [with T = QTableWidgetItem*](i)', which is of non-class type 'QTableWidgetItem* const' mingw32-make[1]: *** [release/detonsis.o] Error 1

    It seems that the function "row()" does not return the int value.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set filter option in QTableWidget

    error: request for member 'row' in 'items.QList<T>::at [with T = QTableWidgetItem*](i)', which is of non-class type 'QTableWidgetItem* const' mingw32-make[1]: *** [release/detonsis.o] Error 1

    It seems that the function "row()" does not return the int value.
    Which part of the error message gave you that impression?

    In this case at() returns a pointer to QTableWidgetItem, not a QTableWidgetItem, so you need to dereference the pointer in line 7.

  6. #5
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set filter option in QTableWidget

    Quote Originally Posted by ChrisW67 View Post
    Which part of the error message gave you that impression?

    In this case at() returns a pointer to QTableWidgetItem, not a QTableWidgetItem, so you need to dereference the pointer in line 7.
    The error message that prevents compile the code and because of the line 7. The
    Qt Code:
    1. items.at(i).row()
    To copy to clipboard, switch view to plain text mode 
    does not return an
    Qt Code:
    1. int
    To copy to clipboard, switch view to plain text mode 
    .

    The solution would be?

    Qt Code:
    1. QTableWidgetItem* p = items.at(i);
    2. tableWidget->hideRow(p->row());
    To copy to clipboard, switch view to plain text mode 

    I m using Qt 4.7.0 on my WIN 7.
    Last edited by jaca; 18th October 2011 at 03:30.

  7. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to set filter option in QTableWidget

    ChrisW67 has given you the answer.

    Let me put it another way: If item.at(i) returns a pointer to a QTableWidgetItem, how would you access the member function row()?

  8. #7
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set filter option in QTableWidget

    The following code was compiled and gave me the result I expected.

    Qt Code:
    1. void DetonSis::sort(QString filter)
    2. {
    3. QList<QTableWidgetItem *> items = deslocadosTable->findItems(filter, Qt::MatchExactly);
    4.  
    5. for(int i = 0; i < items.count(); i++)
    6. p = items.at(i);
    7. deslocadosTable->hideRow(p->row());
    8. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to set filter option in QTableWidget

    The original code had the wrong operator:
    Qt Code:
    1. void DetonSis::sort(QString filter)
    2. {
    3. // QTableWidgetItem *p = new QTableWidgetItem();
    4. QList<QTableWidgetItem *> items = deslocadosTable->findItems(filter, Qt::MatchExactly);
    5.  
    6. for(int i = 0; i < items.count(); i++)
    7. // p = items.at(i);
    8. deslocadosTable->hideRow(items.at(i)->row());
    9. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Remove Help option
    By bismitapadhy in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2009, 13:23
  2. Is QTimer is the only option ?
    By nrabara in forum Newbie
    Replies: 6
    Last Post: 6th May 2009, 06:59
  3. using the lib template dll option on *nix
    By jamadagni in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2007, 13:14
  4. rtti option
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 5th January 2007, 15:10

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.