Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set size of the QListView to fit to it's content
QtWS25 Last Chance

Set size of the QListView to fit to it's content

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 12.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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    Hi,

    I have a QListView which is filling using a model. But my QlistView height is far greater than the size of the contents it hold. In other words, even it consist only two items it shows list-view with amount of space for 10 or 15 items. I want to fit the size of the ListView to contents.

    How can I do that?

    I already read "this thread":http://qt-project.org/forums/viewthread/10097/ and "this thread":http://qt-project.org/forums/viewthread/2312 but those solutions not works. Because of I'm using my own model I can not use the visualReact() method to find the size of the item. Simply, haven't model index.

    Thanks for reading..

    1 Reply Last reply
    1
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      those are old links.

      what Qt version are you using?

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hareen Laks
        wrote on last edited by
        #3

        Eddy,

        Thanks for replying.

        I'm using Qt 4.7.4.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          Why such an old version?

          bq. Because of I’m using my own model I can not use the visualReact() method to find the size of the item.

          Why not?

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hareen Laks
            wrote on last edited by
            #5

            @ChrisW67,

            I asked my superior the same question. But he said we have enough features in Qt 4.7.4 for our project.

            I created a new list view as below by inheriting QListView.

            @class MoreConvenienceListView : public QListView
            {
            Q_OBJECT

            public:
            MoreConvenienceListView(QWidget *parent = 0) : QListView(parent)
            {

            }
            
            void selectRow(int row)
            {
                QItemSelection selection(
                            model()->index(row, 0),
                            model()->index(row, model()->columnCount()-1));
                selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect );
            }
            

            };@

            Then I create a listview like below.

            @
            KeyValueModel<QString,QString> width_model;

            MoreConvenienceListView *width_selector;

            QListView * createWidthSelector()
            {
            width_selector = new MoreConvenienceListView;
            width_selector->setModel(&width_model);
            width_selector->setModelColumn(1);
            connect(width_selector->selectionModel(),
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this,
            SLOT(applyWidthChange(QItemSelection,QItemSelection)));
            return d->width_selector;
            }
            @

            And added to the UI.

            @
            QVBoxLayout *width_box_layout;
            width_box_layout = new QVBoxLayout;
            width_box_layout->addWidget(createWidthSelector());
            ui->component_width_group_box->setLayout(width_box_layout);
            @

            Even I inherited MoreConvenienceListView from QListView I can not use visualRect() method. I think it is because of using KeyValueModel.

            Thank you for helping me.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              Qt 4.7.4 might have all the features but it also has all the bugs that have been fixed since ;)

              The type of QAbstractItemModel has no effect on the utility of the view's visualRect() function. This example seems to work reasonably. It makes little effort to handle cases where the list is empty or taller than the screen.
              @
              #include <QtGui>
              #include <QDebug>

              class ListView: public QListView
              {
              Q_OBJECT
              public:
              ListView(QWidget *p = 0): QListView(p) {
              setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
              }

              QSize sizeHint() const {
                  QSize hint = QListView::sizeHint();
                  if (model()) {
                      // Determine the vertical space allocated beyond the viewport
                      const int extraHeight = height() - viewport()->height();
              
                      // Find the bounding rect of the last list item
                      const QModelIndex index = model()->index(model()->rowCount() - 1, modelColumn());
                      const QRect r = visualRect(index);
              
                      // Size the widget to the height of the bottom of the last item
                      // plus the extra determined earlier
                      hint.setHeight(r.y() + r.height() + extraHeight);
                  }
                  return hint;
              }
              

              };

              int main(int argc, char **argv)
              {
              QApplication app(argc, argv);

              QStandardItemModel model(20, 4);
              for (int row = 0; row < model.rowCount(); ++row) {
                  for (int column = 0; column < model.columnCount(); ++column) {
                      QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
                      model.setItem(row, column, item);
                  }
              }
              
              QWidget w;
              QVBoxLayout *layout = new QVBoxLayout(&w);
              layout->addWidget(new QLabel("Hello"));
              
              ListView *v = new ListView;
              v->setModel(&model);
              v->setModelColumn(3);
              layout->addWidget(v);
              
              w.adjustSize();
              w.show();
              return app.exec(&#41;;
              

              }
              #include "main.moc"
              @

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Hareen Laks
                wrote on last edited by
                #7

                @ ChrisW67,

                Thank you very much for your helping hand..

                But this solution not working with me. :(

                Thanks anyway.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  "Not working," is not a useful description of a problem that can be solved. Please try to help us help you rather than waiting for someone else to guess and provide a copy and paste solution to your unstated problem. What is "not working" about it? Does it work mostly, not at all, only in some circumstances but not in others? Can you provide an example of it "not working"?

                  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