Is it possible to filter model by value in Primary Key column?



  • Hi everybody!
    Could somebody help me to find out the way of filter model.
    I have the following model:

    id (PK) / column2 / column3 ....

    i know (id) value and i need to find a row with this id in model.
    It's possible to sort by QString value, but there is no standart way to filter by int.
    How could this problem be done?

    I'm trying to use QSortProxyModel with

    proxy->setFilterRegExp(QRegExp(QString::number(k)));
    proxy->setFilterKeyColumn(DataBase::U_ID);


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I don't think it is out of the box however you can take inspiration on the Custom Filter Model example and re-implement the filterAcceptsRow method.

    Hope it helps.



  • @SGaist Hi. Thanks for your reply. I saw this example several times today and still can't understand how to deal with
    filterAcceptsColumn/ filterAcceptsRow function. There is two parameters,

    int source, const QModelIndex &source_parent

    and what does they mean? For example source - is my serching id integer, but what is the second one? When i call this reimplemented method from MySortProxyModel i have to give this parameter into function.

    And as i know this far, methods i calling from proxy model must change the instance of proxy model.
    Should i,

    for(int i=0; i<sourceModel()->rowCount(); i++)
    {
    int id_tmp = sourceModel()->data(sourceModel()->index(i,0)).toInt();
    if(source == id_tmp)
    {
    //do something to change model instance
    }
    }



  • By reading documentation more and more, i mention that this methods

    filterAcceptsColumn()
    filterAcceptsRow()

    used for filter current row or column.

    But, why there is no method like setFilterRegExp for other standart classes (int, bool for example)
    I just need to get a single row in my proxy model which contain in its Primary Key id column the value i specifyed.
    (It's easy with QString but difficult with int WTF)

    I't so dissapointing to reimplement everything in Qt for every simple task. And, yeah, i don't anderstand the way it working.


  • Lifetime Qt Champion

    The first parameter is the source model row you are going to inspect. The second is the parent item, usually only avoid when implementing a tree model.

    No, you shouldn't modify anything in the source model from that function. Just return whether the row passed as parameter should be returned for the filtering you want to do.



  • @SGaist
    But how should i specify id i searching for?
    What if i didn't try to implement tree model. I only need to obtain a QSortProxyModel with a singl row (in source model more than 100 rows), which i put in QTableView. (with transpose row and columns)


  • Lifetime Qt Champion

    Like in the example, you add your own setter function for that.



  • @SGaist
    I got it. I dont need to call this method by myself . QTableView will call it.
    And i only need to add new private varible with its setter wich i (variable) allow to use in filterAcceptsColumn().
    And when this method return true, QTableView will show me the instance ?
    And thats mean i shoul go along this row or column, and compare cell value with my specifyed id.
    ANd more, i dont eed to do any cicle by myself because QTableView will call this method in a cycle and give me const QModelIndex &source_parent which value i could compare.


  • Lifetime Qt Champion

    Yes, that's that.



  • @SGaist
    It is worked well. Finally, after a day, it makes sense.

    Thank you very much! I do not know that I have done without your help!


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

    Note, you may have to first click on "Ask as question" before you can mark it solved.



  • @SGaist
    Done


  • Lifetime Qt Champion

    Thanks !

    Happy coding :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.