Qt6 topics merged with General

Can QWidgets be placed into a list view?

  • Hi,

    I would like to present a scrollable list of widgets (quite large, almost screen-wide in fact). I haven't worked with Qt lists before and I'd like some info before I dig in, so I know what to look for. Here are some questions:

    • can widgets be appended to a list?
    • can list items be indexed and moved remotely to that index (say, by clicking on a button)?
    • can lists scroll horizontally rather than vertically?
    • am I not better of making an elongated widget that moves on mouse wheel event?


  • Did you try "QAbstractItemView::setIndexWidget() ":http://doc.qt.nokia.com/4.7/qabstractitemview.html#setIndexWidget already?

  • Haa I think that's what I need --- but I am not sure.

    What I want is to make something like a picture-slide, only not with pictures, but widgets functioning as pages. I want them to slide horizontally, per item and not per pixel (so when I scroll an item, it moves to the next and does not stop in the middle of the two).
    Also I want to be able to move from a page to another by index.

    From what I see the QAbstractItemView can do that with "setCurrentIndex()":http://doc.qt.nokia.com/4.7/qabstractitemview.html#setCurrentIndex (?)

    But doesn't abstract mean it is not graphically rendered? I read:
    bq. QAbstractItemView is an abstract class and cannot itself be instantiated.

    Can you explain? Thanks.

  • OK i am beginning to make sense of it: QListView inherits the QAbstractItemView and therefore it can use the setCurrentIndex() method.

  • Abstract in the context of C++ means "not fully implemented". That is: The abstract class defines some methods, but provides no implementation for it. In a header file this is defined by assigning 0 to the method:

    class AbstractBaseClass {

    virtual void doSomething();
    virutal void reimplementThis(int a) = 0;


    In this example, the AbstractBaseClass provides a (default) implementation of method doSomething, but none for reimplementThis.

    An abstract class cannot be instantiated directly, but only subclasses that implement the missing methods. It can of course be the type of variable to wich an instace of the subclass can be assigned.

    An abstract base class is a good means to define a common interface that all subclasses must implement.

    We mostly refer to the method description of a base class if it is not clear which subclass is in use, or if the subclass does not reimplement that method.

    Regarding your actual problem:
    Qt provides some implementations of the view for different purpoeses: [[Doc:QTableView]] is rendered as a table, [[Doc:QTreeView]] displays a tree like in the different explorers etc., [[Doc:QListView]] provides a list or icon view on the data, [[Doc:QColumnView]] displays hierarchical data on side by side columns. You can choose the on that fits your needs best

    Also, be aware that most of the view have a corresponding xxx widget variant that works without a model.

    For your case, a [[Doc:QListWidget]] seems to be a feasible solution.

    To a achieve the scroll behavior you described call

    viewOrWidget->setVerticalScrollMode( QAbstractItemView:: ScrollPerItem );

  • Thanks for clearing this up. In the meantime, I have discovered the "model-view programming documentation":http://doc.qt.nokia.com/4.6/model-view-programming.html

    QListView indeed seems to be the best, I understand I need to pass a reimplemented QAbstractListModel.

    What I am concerned about however, is how to pass QWidgets into this model. I need to specify the role of the data I pass into the model, but if I look at the "existing roles":http://doc.qt.nokia.com/4.7/qt.html#ItemDataRole-enum, I cannot find any role that allows a QWidget, at best there will be the Qt::DecorationRole, but will that do?

  • There is no easy way to put widgets into a model. This is the reason why I would go with a QList widget in this case.

  • What about creating your own "QStyledItemDelegate":http://doc.qt.nokia.com/latest/qstyleditemdelegate.html

    To my mind, QListWidget and all other QXXXWidget should be reserved for static content.

  • Thanks Octal. That seems OK, but it doesn't tell me whether I can pass in a widget into the model.

    It seems using a QScrollArea and scroll programmatically using the inherited scrollContentsBy() would do the trick. The viewport then would be an elongated widget, sliding left or right so one section could be seen at a time.

    What do you think?

  • Because you don't pass a widget into the model, instead you create your own delegate.

    Which sort of widget would you want to place into your listview ?

  • I want to pass a QFrame holding many children (lists, buttons, labels, icons). What I want is similar to a StackedLayout in function, but with a slide view in appearance, switching from one page to another.

Log in to reply