Results 1 to 2 of 2

Thread: Effective way to parse through a model intended to store key and associated values.

  1. #1
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Effective way to parse through a model intended to store key and associated values.

    I have a model, deriving from QStandardItemModel, that is used to represent keys with associated values. The key is stored in the first column of each row, while the associated values are stored in the following columns from each row.

    I have quite often to verify if a certain key is already stored in the model, if this is the case I am leaving the model untouched otherwhise I am adding a new row.

    So each time I have to verify if a certain key is already stored in the model, I am parsing the entire model like this:

    Qt Code:
    1. // frame_id contains the key we may want to store in the model
    2.  
    3. // Get the amount of rows already stored in the model
    4. int rowCount = m_model->rowCount(QModelIndex());
    5. int row;
    6.  
    7. unsigned int stored_id;
    8. // Parse the model to look if this identifier is already stored
    9. for (row = 0; row < rowCount; row++) {
    10. stored_id = m_model->data(m_model->index(row,0,QModelIndex()),Qt::DisplayRole).toUInt();
    11. if (stored_id == frame_id) {
    12. // This identifier is already stored in the model, so we simply return it color
    13. return QColor(m_model->data(m_model->index(row,1,QModelIndex()),Qt::DisplayRole).toString());
    14. }
    15. }
    16.  
    17. // This identifier is not already stored in the model, so we will add it at the end.
    18. m_model->insertRows(row,1,QModelIndex());
    19. m_model->setData(m_model->index(row,0,QModelIndex()),frame.id);
    20. m_model->setData(m_model->index(row,1,QModelIndex()),QColor(Qt::white).name());
    21. m_model->setData(m_model->index(row,2,QModelIndex()),"");
    To copy to clipboard, switch view to plain text mode 

    I am wondering if parsing the entire model is the correct way to do or if there is an other type of model I could use in order to make this more efficient.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Effective way to parse through a model intended to store key and associated value

    Such model is very inefficient because you can only iterate it as a sequence. You could store the data in a QHash and reimplement QAbstractItemModel::match() or provide your own method for searching. Alternatively if keys are unique, store them in a private QSet object and provide a contains() method for your model.

    Also, if you can make sure your keys are always sorted, you can use binary search to find the key quickly in the standard model.

    So you see there are many ways to solve your problem - pick one that suits you best.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.