×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    How to fiter a list

    thanks ... doing the same.

    I have one query about porfermance issue.
    First let me tell you what I am doing.
    I am making a contact search page. All contacts will be read from phone's addressbook. User can search those contacts by names. I have bind textChanged SINGAL of LineEdit control with my search logic. I have loaded all contacts with unchecked checkboxes in QListWidgets. I have also loaded each item with some data using setData() method. Data includes contact's localId, Contact Display name, phone number.

    My Question is that suppose I have 500 contacts. I loaded them in QListWidget then if I search for a contact name and suppose only 50 comes in that search then should I clear the whole list and insert only those 50 items or should I hide 450 contacts. It's all about performance. I think clearing all and adding again will be a costly process so hiding should be fast. But when user will search again in those 50 contacts then searching will be slow as there are still hidden contacts in list but in other case as there is only 50 so it will be fast. So what is the best way to do this ? any other idea do you have ?
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Can we get Signals when an item changes it's state in QListWidget

    The best solution in such use case is to use QSortFilterProxyModel
    http://doc.qt.nokia.com/4.7/qsortfil...l.html#details

  3. #3
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    Re: Can we get Signals when an item changes it's state in QListWidget

    Quote Originally Posted by divanov View Post
    The best solution in such use case is to use QSortFilterProxyModel
    http://doc.qt.nokia.com/4.7/qsortfil...l.html#details
    After reading that I implemented it like the below, but it is not filtering. What is the problem

    All variables are declared in header file. list variable is of type QListWidget
    Code:
    void MainScreen::setFilterOnList()
    {
        proxyModel = new QSortFilterProxyModel(this);
        proxyModel->setSourceModel(list->model());
        proxyModel->setFilterKeyColumn(0);
       //QMessageBox::information(NULL, "Count", QString("").setNum(list->model()->columnCount()), QMessageBox::Ok);
    
    }
    Here is the slot which should filter the result.
    Code:
    void MainScreen::OnSearchName(QString nameToSearch)
    {
        nameToSearch = QString("*") + nameToSearch + QString("*");
        QRegExp regExp(nameToSearch, Qt::CaseInsensitive, QRegExp::Wildcard);
        proxyModel->setFilterRegExp(regExp);
        QMessageBox::information(NULL, "Count", QString("").setNum(list->model()->rowCount()), QMessageBox::Ok);
    
    }
    Last edited by rahul.kulshreshtha; 2010-10-05 at 11:46. Reason: added information about list variable
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

  4. #4
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    Re: Can we get Signals when an item changes it's state in QListWidget

    Quote Originally Posted by divanov View Post
    The best solution in such use case is to use QSortFilterProxyModel
    http://doc.qt.nokia.com/4.7/qsortfil...l.html#details
    I found the exact problem. It's filtering as I expected. I printed out number of rows in proxyModel before and after the searching and rowCount was right.
    The problem is that list is not using proxyModel so after applying filter to proxyModel, list does not update it's view because it is using it's own model not proxyModel.
    I tried to set proxyModel as list's model but setModel() method is private so it's not accessible. List's model should automatically update if proxyModel changes but it's not getting update.

    What am I missing in code?
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Can we get Signals when an item changes it's state in QListWidget

    I don't know why you say setModel is private.

    Code:
    /* Creating models */
    MyItemModel *sourceModel = new MyItemModel(this);
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(sourceModel);
    
    /* Creating list widget */
    QListWidget *list = new QListWidget;
    list->setModel(proxyModel);
    
    /* Filtering */
    proxyModel->setFilterRegExp(QRegExp("*test*", Qt::CaseInsensitive,
                                                 QRegExp::FixedString));

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    39

    Re: Can we get Signals when an item changes it's state in QListWidget

    I think there's a confusion with the classes that are being used.
    It's nos posible to set a model on a QListWidget because it already has a predefined model attached.
    The class that allows to set custom models is the QListView, but use that class would mean that rahul should change his code to adapt it to the MVC pattern, and only he knows if it's the best approach for his problem.

    rahul:
    Maybe you should read the Model/View Programming section.

  7. #7
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    Re: Can we get Signals when an item changes it's state in QListWidget

    Yes mkfnx, I am asking for QListWidget which uses it's own model so we can not change it's model. So is there any way to filter QListWidget ? I am reading that article which you suggested but I guess there is only findItems() methods which can filter out data but it does not update view so I have to update it manually. I don't see any way which will not duplicate the data in memory.

    Quote Originally Posted by divanov View Post
    I don't know why you say setModel is private.

    Code:
    /* Creating list widget */
    QListWidget *list = new QListWidget;
    list->setModel(proxyModel);
    @divanov I am not able to compile list->setModel(proxyModel);
    setModel is private in QListWidget class but virtual public in QAbstractItemView

    Let me know if you require a prototype project to solve this problem.
    I had created one already.
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

  8. #8
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to fiter a list

    Use QListView instead of QListWidget.

  9. #9
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    Re: How to fiter a list

    Quote Originally Posted by divanov View Post
    Use QListView instead of QListWidget.
    Does QListView suport checkbox??
    I want to give multiselct option to user using checkbox
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

  10. #10
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to fiter a list

    Quote Originally Posted by rahul.kulshreshtha View Post
    Does QListView suport checkbox??
    Yes, it does.

  11. #11
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    Re: How to fiter a list

    Quote Originally Posted by divanov View Post
    Yes, it does.
    Thanks Divanov & mkfnx, now every thing is working fine as I wanted except it shows checkboxes "Right Side". How can I move them left side ?
    I have also written article on it. http://wiki.forum.nokia.com/index.ph...lter_QListView
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

Similar Threads

  1. Images as list items & list as a component of a form
    By thijsman86 in forum Symbian User Interface
    Replies: 1
    Last Post: 2010-07-23, 20:00
  2. Replies: 11
    Last Post: 2010-06-28, 23:22
  3. Replies: 15
    Last Post: 2009-01-19, 17:32
  4. Replies: 1
    Last Post: 2008-12-22, 06:38
  5. Replies: 3
    Last Post: 2008-08-11, 13:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×