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. Extending the addressbook example by a find method

Extending the addressbook example by a find method

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.9k 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.
  • M Offline
    M Offline
    micha_52
    wrote on last edited by
    #1

    trying to get an understanding how the MVC works I'm looking to the "addressbook example":http://doc.qt.nokia.com/4.7-snapshot/itemviews-addressbook.html

    In order to become familiar with Qt I'd like to add a find method to the addresswidget class. That method shall get a QString parameter and I want to highlight the name/address in the appropriate tab when found.

    Unfortunately I can't find out how to connect a QSortFilterProxyModel to the view and the table so that this can be achieved. I would be very happy if somebody could give me a hint.

    Thanks,
    Michael

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi Michael,

      first of all, why do you need a QSortFilterProxyModel (QSFPM) to achieve a find?

      If you really need a QSFPM, in general, it works like this:

      @
      QTableView* pView = ...;
      MyModel* pModel = ...;

      QSortFilterProxyModel* pProxy = new QSortFilterProxyModel(this);
      pProxy->setSourceModel(pModel);
      pView->setModel(pProxy);
      

      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

        The example already uses a set of QSortFilterProxyModels to filter out the correct addresses for each page, and each page has its own view. That makes your idea bit more tricky.

        What you could do, is this:

        • Add a new page "All contacts", that not only has a view like the other pages, but also a search bar
        • Filter that view using a QSortFilterProxyModel that is updated by your search bar

        Hints:

        • your QSortFilterProxyModel needs to have dynamic filtering enabled
        • Use a QLineEdit for the search bar, and use the textChanged(QString) signal to update the search expression.
        • Wildcard filtering may work best, but you would need to add the actual wildcard characters
        1 Reply Last reply
        0
        • M Offline
          M Offline
          micha_52
          wrote on last edited by
          #4

          Hi Gerolf,

          I understood that I can connect the view with the table by using a QSortFilterProxyModel (QSFPM). What I have not understood is how I can highlight an item in the view once I have found the appropriate index in the table for a name.

          Hi Andre,

          thanks for your hint. It sounds simpler. But also here I do not know how to establish the connection between the table and the view by using QSFPM. Some lines of code would be a great help.

          Thanks,
          Michael

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

            Well, the table is a model (a QAbstractItemModel subclass). You set a model on a view by using setModel() on the view, the table in this case.

            To put a QSortFilterProxy model into the picture, you have to recognize that it too is a model just like the table model you already have. Only, instead of giving access to actual data, it gives access to another model that it provides another view on: it may be sorted, filtered, or even otherwise modified. So, you set the proxy model as the model for the view, and set the original model as the source model for the proxy model. There is actually a code example inside the example you are modifying. Take a look at the AddressWidget::setupTabs() method in the example code you referenced. You will see a QSortFilterProxy model being set up there.

            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