Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Expanding QListView within QScrollArea

Expanding QListView within QScrollArea

Scheduled Pinned Locked Moved Mobile and Embedded
29 Posts 5 Posters 19.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maleadt
    wrote on last edited by
    #19

    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).

    1 Reply Last reply
    0
    • I Offline
      I Offline
      infoctopus
      wrote on last edited by
      #20

      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

      Qt rulez

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maleadt
        wrote on last edited by
        #21

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #22

          The size policy is only taken into account for Layouts (QLayout and subclasses), but not for widgets in item views.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maleadt
            wrote on last edited by
            #23

            I see. But then the obvious question is: how do I make a widget within an item view consume all available space, without manually doing that?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #24

              As far as I know, this is not possible.

              You might consider to drop the list view and only use widgets and a layout, if that's possible with the logic you want to implement.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • M Offline
                M Offline
                maleadt
                wrote on last edited by
                #25

                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 :)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #26

                  I have similar use cases in my applications. In that case I use a "QStackedWidget":http://doc.qt.nokia.com/latest/qstackedwidget.html. On page is a QLabel with expanding/expanding size policy that displays the "nothing to show" hint, the other is the list view. Maybe that would be of help for you too?

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maleadt
                    wrote on last edited by
                    #27

                    Thanks for mentioning that! For some screens that widget will come in very handy, but I won't be able to use it for all of them. The example above requires two buttons to be scrollable all the time, so they need to be part of the QListView. Hence, the QListView needs to be visible all the time as well, therefore I cannot use the QStackedWidget.

                    I however have found a workaround to fix the layout compression issue. When the "No history or favourites" widget is visible, I configure the listview to have a fixed size equal to the sum of all sizeHints of the containing items (only the button header, in this case). When however the QListView actually contains items, I configure the size to be the sizehint of the QListView itself.

                    Not nice, as the QListView should really manage itself (or I don't get a certain aspect of QListView item management), but it at least doesn't rely on hard-coded pixel constants like the other workarounds I tried during the last days :)

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #28

                      "Not nice" is relative - if it works for you, the solution is at least a good workaround. Sometimes on has to do these nasty tricks...

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        maleadt
                        wrote on last edited by
                        #29

                        I know that. I just like to keep as close to the intended semantics as possible, because that quite often improves maintainability a lot. But hey, I'm happy I got it working, and maybe one day I'll find a proper solution :) Thanks for the help!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved