September 8, 2011

jesuisbenjamin jesuisbenjam..
Lab Rat
79 posts

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?

Thanks.
B.

10 replies

September 8, 2011

Volker Volker
Robot Herder
5428 posts

Did you try QAbstractItemView::setIndexWidget() [doc.qt.nokia.com] already?

September 8, 2011

jesuisbenjamin jesuisbenjam..
Lab Rat
79 posts

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() [doc.qt.nokia.com] (?)

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.

September 8, 2011

jesuisbenjamin jesuisbenjam..
Lab Rat
79 posts

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

September 8, 2011

Volker Volker
Robot Herder
5428 posts

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:

  1. class AbstractBaseClass {
  2. public
  3.     AbstractBaseClass();
  4.     ~AbstractBaseClass();
  5.  
  6.     virtual void doSomething();
  7.     virutal void reimplementThis(int a) = 0;
  8. }

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: QTableView is rendered as a table, QTreeView displays a tree like in the different explorers etc., QListView provides a list or icon view on the data, 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 QListWidget seems to be a feasible solution.

To a achieve the scroll behavior you described call

  1. viewOrWidget->setVerticalScrollMode( QAbstractItemView:: ScrollPerItem );

September 9, 2011

jesuisbenjamin jesuisbenjam..
Lab Rat
79 posts

Thanks for clearing this up. In the meantime, I have discovered the model-view programming documentation [doc.qt.nokia.com]

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 [doc.qt.nokia.com], I cannot find any role that allows a QWidget, at best there will be the Qt::DecorationRole, but will that do?

September 9, 2011

Volker Volker
Robot Herder
5428 posts

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.

September 9, 2011

octal octal
Lab Rat
74 posts

What about creating your own QStyledItemDelegate [doc.qt.nokia.com]

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

September 9, 2011

jesuisbenjamin jesuisbenjam..
Lab Rat
79 posts

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?

September 9, 2011

octal octal
Lab Rat
74 posts

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 ?

September 9, 2011

jesuisbenjamin jesuisbenjam..
Lab Rat
79 posts

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.

 
  ‹‹ Span header      [Solved]QDateEdit - How to restrict user input ? ››

You must log in to post a reply. Not a member yet? Register here!