How to set filter option in QTableWidget

后端 未结 2 1534
陌清茗
陌清茗 2020-12-31 09:19

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 <

2条回答
  •  耶瑟儿~
    2020-12-31 09:51

    Using a sort/filter proxy is probably overkill for this anyway.

    It's a matter of iterating through all of your QTableWidgetItem objects, determining if their text matches the filter and calling QTableView::setRowHidden() as needed.

    For example:

    QString filter = textEdit->text();
    for( int i = 0; i < table->rowCount(); ++i )
    {
        bool match = false;
        for( int j = 0; j < table->columnCount(); ++j )
        {
            QTableWidgetItem *item = table->item( i, j );
            if( item->text().contains(filter) )
            {
                match = true;
                break;
            }
        }
        table->setRowHidden( i, !match );
    }
    

提交回复
热议问题