Expanding QListView within QScrollArea
-
Did you considder that QItemView is itself a QScrollArea? You could perhaps put your additional widgets inside the item view itself. You might try setIndexWidget(). Not sure if it will work for your case, but it might be worth a try.
Edit: note that QML's ListView element has nice header and footer delegates that would be perfect for this...
-
bq. And I’d like to use a QListView if possible, as I use its other functionality (selecting items, drawing using a delegate, expanding them in case of a QTreeView).bq.
ok
bq. Did you considder that QItemView is itself a QScrollArea? You could perhaps put your additional widgets inside the item view itself. You might try setIndexWidget(). Not sure if it will work for your case, but it might be worth a try.bq.
A good idea indeed.
However it would be easier to give more relevant idea, if you uploaded a screenshot with that widgets.
(Maybe QTreeView or QTableView could be of use, this might depend on other widgets that must be around) -
I was actually trying to get something similar to Maemo's "Conversations" application, a window which contains two buttons with a messages listview below, which scrolls as I described before.
I'm now playing with Andre's idea of adding the widgets to the listview itself, which seems to work out fine (apart from some size issues which I think are solvable)!
-
As in Maemo Conversations: basically, make a widget that contains your other widgets and put the widget into 1st item
-
Works like a charm!
One additional question though. When there are no items to be displayed in the listview, I insert a dummy widget in the same manner as the top buttons -- setIndexWidget --. However, I'd like that widget to be centered vertically. Normally I accomplish that by using a VCenter alignment and Expanding vertical size policy, but this does not seem to apply within a QListView.
Is there a way to get a single item within the listview to consume all extra available space? This seems like the definition of a MinimumExpanding sizepolicy, strange it doesn't work... -
give QWidget::setMinimumHeight()/setMinimumSize() a try
-
Forcing the minimum height/size obviously works, I'd just like to do it using Qt's size policy features :)
EDIT: this because I'd like to reuse the code for other devices.
EDIT 2: also, when forcing the minimum height/size, the line which separates the different items in a QListView keeps on getting drawn where it would be drawn in case of a regular-sized item, completely independent of the actual size of the containing widget. Something seems fishy... -
maybe sizeHint()/sizePolicy() of the QListView widget will be also helpful
-
I tried that, but alas. Funny thing is, the QListView is already fully expanded: if I add a border-drawing stylesheet, it is clear the listview fills the entire space already. Not so with the containing widget though.
!http://imgur.com/kDG8P()! -
Are you sure that your model doesn't return 6 for rowCount? ;)
A workaround: place "No history or favourites" label into the middle (3rd or 4th) row
-
I'm sure! Just tested it once more, it contains only 2 items, just as it should. Would there have been more, it would have been made visible by the style sheet as well.
And adding more items does once more restrict the portability by being based on the amount of items it takes to fill the screen... I still hope to stumble upon a "proper" solution :)
Either way, thanks for the help.
-
You have a QScrollArea that contains a QListView and other widgets?
Why not have a QListView that contains the other widgets plus the items you already have in it.
You can use a QListView header for this?Then you can scroll the QListView and everything scrolls up.
Edit: This is what Andre said before.
-
That's what I'm doing now indeed. It works, apart from (1) the items not expanding properly despite the QSizePolicy, and (2) the item separating line being drawn where a regular-sized item would have ended instead of where the item actually ends (enforced by setting the widget's minimum size).
-
Sorry for the multitude of posts, but I've created a minimal testcase, and I don't think I'm doing anything wrong. Should I file a bug?
This is the test window, which clearly displays the issues I'm talking about:
@class Testcase : public QMainWindow
{
Q_OBJECT
public:
Testcase()
{
// View
QListView* tView = new QListView();
setCentralWidget(tView);
tView->setEditTriggers(QAbstractItemView::NoEditTriggers);
tView->setSelectionBehavior(QAbstractItemView::SelectRows);
tView->setSelectionMode(QAbstractItemView::SingleSelection);// Model QStandardItemModel *tModel = new QStandardItemModel(0, 1); tView->setModel(tModel); populateModel(tModel, tView); }
private:
void populateModel(QStandardItemModel iModel, QListView iView)
{
// Add a regular text item
QStandardItem *tItem = new QStandardItem("Item containing text");
iModel->appendRow(tItem);// Add a regular widget QLabel *tLabelRegular = new QLabel("Item containing a label"); iModel->appendRow(new QStandardItem()); iView->setIndexWidget(iModel->index(1, 0), tLabelRegular); // Add an expanding widget (using a size policy) QLabel *tLabelExpanding = new QLabel("Item containing a label with expanding size policy"); tLabelExpanding->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); iModel->appendRow(new QStandardItem()); iView->setIndexWidget(iModel->index(2, 0), tLabelExpanding); // Add an expanding widget (using an enforced size) QLabel *tLabelExpandingForcibly = new QLabel("Item containing a label with enforced minimum height"); iModel->appendRow(new QStandardItem()); tLabelExpandingForcibly->setMinimumHeight(100); iView->setIndexWidget(iModel->index(3, 0), tLabelExpandingForcibly); }
};@
Compiling and launching this window, I get to see:
!http://imgur.com/mNmhj.png(Screenshot of the testcase window.)!The issues with this testcase being:
- QSizePolicy having no effect on a item, despite there being some space left and the parent QListView being fully expanded to occupy all space in the window (see below).
- When forcing the size of the widget as a temporary work-around, the line which separates the listview items gets drawn at the place where a regularly-sized item would have ended, which is wrong.
To illustrate the first issue a bit better, I've taken an additional screenshot with borders on the listview and its children enabled:
!http://imgur.com/Dr3bd.png(Screenshot of the testcase window, with illustrative borders.)!
This clearly shows that the QListView is fully expanded within the window (no additional QSizePolicy needed, and it doesn't have any effect either), and that there is free space left which should get filled by the third item (the one with an expanding size policy). -
maleadt,
could you upload a .deb file and/or your project that contains minimal code for reproducing the issue. I could run it on my n900
-
Sure, here are all the files (haven't got the Maemo SDK at hand right now):
main.cpp
@#include <QApplication>
#include "testcase.h"int main(int argc, char **argv)
{
QApplication tApplication(argc, argv);
Testcase tWidget;
tWidget.show();
tApplication.exec();
}@testcase.h
@#include <QMainWindow>
#include <QListView>
#include <QStandardItem>
#include <QLabel>
#include <QStandardItemModel>class Testcase : public QMainWindow
{
Q_OBJECT
public:
Testcase()
{
// View
QListView* tView = new QListView();
setCentralWidget(tView);
tView->setEditTriggers(QAbstractItemView::NoEditTriggers);
tView->setSelectionBehavior(QAbstractItemView::SelectRows);
tView->setSelectionMode(QAbstractItemView::SingleSelection);// Model QStandardItemModel *tModel = new QStandardItemModel(0, 1); tView->setModel(tModel); populateModel(tModel, tView); }
private:
void populateModel(QStandardItemModel iModel, QListView iView)
{
// Add a regular text item
QStandardItem *tItem = new QStandardItem("Item containing text");
iModel->appendRow(tItem);// Add a regular widget QLabel *tLabelRegular = new QLabel("Item containing a label"); iModel->appendRow(new QStandardItem()); iView->setIndexWidget(iModel->index(1, 0), tLabelRegular); // Add an expanding widget (using a size policy) QLabel *tLabelExpanding = new QLabel("Item containing a label with expanding size policy"); tLabelExpanding->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); iModel->appendRow(new QStandardItem()); iView->setIndexWidget(iModel->index(2, 0), tLabelExpanding); // Add an expanding widget (using an enforced size) QLabel *tLabelExpandingForcibly = new QLabel("Item containing a label with enforced minimum height"); iModel->appendRow(new QStandardItem()); tLabelExpandingForcibly->setMinimumHeight(100); iView->setIndexWidget(iModel->index(3, 0), tLabelExpandingForcibly); }
};@
testcase.pro
@QT += core gui
SOURCES += main.cpp
HEADERS += testcase.h@Displays the same erroneous behaviour on desktop Qt.
-
Sadly, I really need the QListView functionality, so I cannot revert completely to a QWidget-based layout. But the part which is troubling me right now (a simple label to display there are no results), will only be visible when no QListView functionality is needed.
So I tried to add the widget which unsuccessfully expands within the listview to the main layout. But now the QListView is working against me as well: it won't compress to its minimal required size :(
See this image:
!http://imgur.com/PeJq9.png(QListView fails to compress.)!The label displaying "No history or favourites" is configured with a MinimumExpanding vertical policy, while the listview has a "Minimum" vertical policy in place. Both are added to a QVBoxLayout.
But the size policies don't matter at all... The listview only contains a single item, being the two buttons visible at top of the window. Yet the sizeHint of the QListView returns a whopping 192 px, which is WAY to much for what its worth.
How come the QListView returns a sizeHint which doesn't match the amount of items actually present in the listview?Thanks for bearing with me, I'm still quite new with Qt :)