Searching a QString in QSqlRelationalTableModel witch match doesn't return a result



  • Hello,

    What am I doing wrong? I have been looking for a solution for quite some time now. The first part of the code works (search in a for loop for a QString in a model). The second part should do the same but doesn't return any valid index.

    ClubsModel is subclassed from QSqlRelationalTableModel. ClubsModelProxy from QSortFilterProxyModel.

    
    int ClubsModel::getRank(const int clubId)
    {
        int nrofrows = clubsModelProxy->rowCount();
        int rank=0;
    
        QString clubName=data(index(clubId, CLUBNAME, QModelIndex()), Qt::DisplayRole).toString();
    
        for (int row = 0; row < nrofrows; ++row) {
            QModelIndex idx=clubsModelProxy->index(row,CLUBNAME,QModelIndex());
            if (clubsModelProxy->data(idx).toString()==clubName){
                rank=idx.row()+1;
                break;
            }
        }
    
        QModelIndexList indexList=clubsModelProxy->match(index(0,0),Qt::DisplayRole, QVariant::fromValue(clubName), -1, Qt::MatchFixedString);
        qDebug()<<indexList.count();
        return rank;
    }
    

    What do I overlook?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Is CLUBNAME's value 0 ?



  • Thank you! CLUBNAME is not 0. CLUBNAME is defined as const int CLUBNAME=2. It's refering to the third row of the model. That row contains club names.


  • 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 :)


Log in to reply
 

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