Results 1 to 12 of 12

Thread: Searching a QListWidget

  1. #1
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Searching a QListWidget

    I am trying to use a textbox that searches as you type. I have written some basic code that works perfect for smaller list, but not for big ones. I just connect this to the test edits changed signal. Is there anything I can do to speed this up?

    Qt Code:
    1. QString searchText = ui->lineEdit->text();
    2. int listWidgetSize = ui->listWidget->count();
    3.  
    4. for (int k1 = 0; k1 < listWidgetSize; k1++)
    5. {
    6. if (ui->listWidget->item(k1)->text().startsWith(searchText))
    7. {
    8. ui->listWidget->item(k1)->setHidden(false)
    9. }
    10. else
    11. {
    12. ui->listWidget->item(k1)->setHidden(true);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Thanks guys!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching a QListWidget

    use
    Qt Code:
    1. ++k1
    To copy to clipboard, switch view to plain text mode 
    in your for scope. But I guess you wont realize the speed improvement... Na you only can speed up, if you cache the widgets text in a QStringList or similar. But the best would be to use the model view frameworks. Look ups in models are really fast.

  3. The following user says thank you to Lykurg for this useful post:

    bl1nk (9th August 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Searching a QListWidget

    I would have to use a QListView to use the model frameworks right?

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching a QListWidget

    Quote Originally Posted by bl1nk View Post
    I would have to use a QListView to use the model frameworks right?
    Yes, what data you are displaying right now? And how does it look like?

    For searching see QSortFilterProxyModel.

  6. The following user says thank you to Lykurg for this useful post:

    bl1nk (9th August 2010)

  7. #5
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Searching a QListWidget

    Its basically just an image browser right now. In the list widget (icon view) i just have the image icon and the image name as the text. I have never use the model based framework. How do I go about adding data and accessing individual items?

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching a QListWidget

    For a beginning you can use QStandardItemModel with your text as display role and your image as decoration role. but the performance will be also not very well. If you want better performance you have to write your own model.

  9. The following user says thank you to Lykurg for this useful post:

    bl1nk (9th August 2010)

  10. #7
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Searching a QListWidget

    Alright, so for my list widget I was loading up all the icons with just a standard icon from a resource file. Then I start run a thread to go though and load the icons. That way it takes no time to load up the base icons, then just starts loading the actual image icons. I am having some trouble finding out how to access and individual item so that i can change its icon. How can I access and individual icon from the list view?

  11. #8
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Searching a QListWidget

    I'm new but I want to know if when you set hidden an element, Qt redraw the QListWidget. If yes, that can explain the problem and using a copy of the widget can resolve the problem.

    Just a note : ui->listWidget->item(k1)->setHidden(!ui->listWidget->item(k1)->text().startsWith(searchText))

  12. #9
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Searching a QListWidget

    Using the model view framework is a lot faster! I am using the proxy to search, but I am having issues filtering things that only start with the text. Should i use a regular expression to only take the first portion? What is the best way to go about this?

    Also, with a list view how do you detect when the selection changes? (not clicking on anything) Like the QListWidget::currentItemChanged signal

  13. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching a QListWidget

    Each view has a selection model with signals when the selection changes.
    You can't access an icon of an item in a view. You have to set up your model. So if your thread have load an image set it to your model. This will emit a dataChanged signal and normally the view gets updated so that the new icon will be shown.
    A regular expression is to much. I would go for startWith().

  14. #11
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Searching a QListWidget

    How do I use startWith() with my proxy? Thats what i cant figure out.

    I have gotten the loading of the icons and everything working fine and have my model constructed. I just dont know how to detect when a selection change is made

  15. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching a QListWidget

    Ok, first see this code from the docs:
    Qt Code:
    1. bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
    2. const QModelIndex &sourceParent) const
    3. {
    4. QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    5. QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
    6. QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
    7.  
    8. return (sourceModel()->data(index0).toString().contains(filterRegExp())
    9. || sourceModel()->data(index1).toString().contains(filterRegExp()))
    10. && dateInRange(sourceModel()->data(index2).toDate());
    11. }
    To copy to clipboard, switch view to plain text mode 
    I guess your filter looks similar. so simple use something like:
    Qt Code:
    1. sourceModel()->data(index0).toString().startsWith("foo")
    To copy to clipboard, switch view to plain text mode 

    For the selection change:
    Qt Code:
    1. connect(pointerToYourView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(foo()));
    To copy to clipboard, switch view to plain text mode 
    or use the selectionChanged signal etc.

Similar Threads

  1. QAbstractListModel searching.
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 19:41
  2. Searching in a list.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 09:00
  3. searching in a TableWidget
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2008, 17:17
  4. Searching a QTable
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 23:15
  5. Qt IDE Windows - still searching...
    By magland in forum General Discussion
    Replies: 8
    Last Post: 25th June 2007, 01:12

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.