Results 1 to 2 of 2

Thread: How to make a filterable and sortable list of Widgets

  1. #1
    Join Date
    Nov 2018
    Qt products
    Qt3 Qt4 Qt5

    Default How to make a filterable and sortable list of Widgets

    I have a Qt C++ application to which I'd like to add a list of "Card" objects. Each Card can contain a few widgets, including a text area and two spinboxes. Here's the hard part: I want to be able to sort and filter this list dynamically. I tried making a QListView with a StandardItemModel, and set a SortFilterProxyModel on the StandardItemModel. This works fine when the items in the model (which must be QStandardItem) only contain text. The problem is that I can't add a Widget, such as a SpinBox, to a QStandardItem.

    I tried subclassing QStandardItem, but I could not figure out how to display Widgets in my subclass. I tried the QStyledItemDelegate example of a custom spinbox (, but this only works for rendering a single spinbox: it doesn't let me add any Widget to my QStandardItem.

    Is there a way to display a list of Widgets or Widget containers, such that I can filter and sort the list? My goal here is to make a list of card-like items (as described here:, and to show, hide, and sort the cards.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 728 Times in 718 Posts
    Qt products
    Windows Android

    Default Re: How to make a filterable and sortable list of Widgets

    Seems to me that you have the problem that you are "confusing the map for the territory", that is, you are not distinguishing between the model for your cards and their contents with the way you display the model on the UI.

    Your map is the model that describes the cards and the widgets each one contains. It is a hierarchy of widgets and layouts, with the card widget at the top and any layouts, spin boxes, line edits, etc. under that in a parent-child relationship.

    Your territory is the way you display that information on screen. If you want to display your model as a hierarchy, then you would use QTreeWidget. If you want a list, you use QListWidget. For each QWidget in your model, you can retrieve its name (QObject::objectName()) and through the QMetaObject (via QObject::metaObject()) you can retrieve the class name (widget type) through QMetaObject::className(). What you display is the text that describes each of the components in your model.

    This is exactly what Qt's "ui" files are all about. The ui file is a description of the widget and layout hierarchy for a dialog or other widget. At runtime, Qt uses the information compiled form that file to create the actual widgets on screen.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 3rd April 2017, 21:35
  2. How to make and use custom UI widgets for a list
    By Estefunny in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2014, 00:25
  3. Replies: 2
    Last Post: 20th August 2010, 13:20
  4. List of Widgets
    By linuxdev in forum Qt Tools
    Replies: 3
    Last Post: 30th April 2009, 14:24
  5. How to make a list of QGraphicsItems on the scene
    By Holy in forum Qt Programming
    Replies: 8
    Last Post: 10th June 2008, 13:43

Tags for this Thread


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.