Extending the addressbook example by a find method



  • 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



  • 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);
    

    @



  • 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


  • 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



  • 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.


Log in to reply
 

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