Search in QStandardItemModel



  • Hello everybody,

    I have read in and displayed a csv file in a QStandardItemModel (QTableView) , this works fine.
    Now I would like to search in this model (1 column, <200 lines) for a word, this can occur theoretically in every line.
    With this code works the reading in and attach the line, now unfortunately I do not know how I can now show the found words .. For the beginning, I would like to show them in the console, preferably with the line number.

    Does anyone have any idea how to do this?

    QStandardItemModel *csvModel; 
    
     QList<QStandardItem *> list;  
     for (QString item : line.split(";")) {      
                        list.append(new QStandardItem(item));   
                    }
    
                    csvModel->insertRow(csvModel->rowCount(), list);
                    csvModel->setHorizontalHeaderLabels(QStringList() << "Ausgangssprache");    
                    
                    
                         QList<QStandardItem *> items = csvModel->findItems("GESUCHTER STRING", Qt::MatchExactly);
         		    if (items.size() > 0) {
             		   qDebug () << "Mind. 1 gefunden";
            	 }
    

    Thank you in advance!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What kind of visualisation do you have in mind for the items found ? Another view ? A change of colour in this one ?



  • For the beginning, I would like to show them in the console, preferably with the line number.



  • @FloQt
    So in the list of QStandardItem *s returned by findItems(): I presume row() is the line number, and data(Qt::DisplayRole) is the string of the line. As for "show them in the console", presumably cout will do that to the terminal where you run the program from, so something like (I'm not a C++er):

    cout << item->row() << item->data(Qt::DisplayRole);
    

Log in to reply
 

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