Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Search in QStandardItemModel
Qt 6.11 is out! See what's new in the release blog

Search in QStandardItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FloQt
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • F Offline
        F Offline
        FloQt
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • F FloQt

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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);
          
          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved