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. How to check if an item exist in a model?

How to check if an item exist in a model?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 6.4k Views
  • 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
    freecamellia
    wrote on last edited by
    #1

    Hi,
    How to do that for a QSqlTableModel and/or QSortFilterProxyModel.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      You can only iterate over the model. That means: a linear search, unless you know that the data will be sorted.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        broadpeak
        wrote on last edited by
        #3

        Maybe helps:
        use a temporary model, and set this filter exactly:
        @
        model->setFilter( "columnName = 'what_you_have_to_find_string'" );
        @
        And if your model has resultset, you'll find your actual item in your model.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

          [quote author="broadpeak" date="1323703488"]Maybe helps:
          use a temporary model, and set this filter exactly:
          [/quote]

          It depends on the number of items in the model (and on their type), having another clone model is not an efficient solution for huge amount of data. Another solution that seems more efficient to me is to keep an hash table with the column data as key and the model index as value. This could be simpler for huge amount of data and exact match searching for.
          But iterating would be my first choice.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            freecamellia
            wrote on last edited by
            #5

            Thanks, Trying now...

            1 Reply Last reply
            0
            • F Offline
              F Offline
              freecamellia
              wrote on last edited by
              #6

              [quote author="Andre" date="1323702362"]unless you know that the data will be sorted. [/quote]

              Yes, in my case I have a sorted QSqlSortFilterModel, so what I need to do?

              1 Reply Last reply
              0
              • B Offline
                B Offline
                broadpeak
                wrote on last edited by
                #7

                (Ther is no "QSqlSortFilterModel", but we have QSortFilterModel, because this can filter and sort every model, not only sql_model)

                So with FilterModel you can try it:
                @
                proxyModel->setFilterRegExp(QRegExp("*.txt", Qt::CaseInsensitive, QRegExp::FixedString));
                @

                (I don't know how to iterate through on a filtered model :S)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Well, if you know the model is sorted, you can use a binary search algorithm. You basically first try the item in the middle of the list, if that item is bigger than the one you were looking for, you can try the one half way the beginning and the item you just tried, otherwise you try the one between the middle and the end. That way, you can home in on the place in the list where your item is or should be very quickly. It is much faster than just iterating over all items (assuming random access costs are small, of course; if random access is much more expensive than sequential access, a linear search untill you are at an item bigger than what you were looking for might still be cheaper).

                  Edit
                  Slower, but less code, is to just create a new QSortFilterProxyModel, set the proxy model you already have a the source model, set the filter to whatever you are searching for. If the model contains more than 0 items, you have found your item. However, that is not as efficient as searching yourself, I think.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    Am I missing something, or would "QAbstractItemModel::match() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#match be of use here?

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="Volker" date="1323734916"]Am I missing something, or would "QAbstractItemModel::match() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#match be of use here?[/quote]

                      You are missing nothing, I am. This is of course much easier than anything I suggested before.

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        freecamellia
                        wrote on last edited by
                        #11

                        Thanks for you all. I try..

                        1 Reply Last reply
                        0

                        • Login

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