Results 1 to 5 of 5

Thread: QListView max number of items in view

  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Question QListView max number of items in view

    Hi, I need to calculate max number of items in current view of QListView. I've wrote code like this:

    Qt Code:
    1. void MyListView::resizeEvent(QResizeEvent *event)
    2. {
    3. QListView::resizeEvent(event);
    4. qDebug()<<"QListView spacing: " <<this->spacing(); //its 0
    5. qDebug()<<"column: " << this->modelColumn(); //its 0
    6.  
    7. QFontMetrics fm (this->font());
    8. int fontHeight = fm.lineSpacing();
    9.  
    10. QRect cr = contentsRect();
    11. int windowHeight = cr.bottom() - cr.top();
    12.  
    13. int maxItemsCount = windowHeight / fontHeight;
    14. qDebug()<<"max items in view: "<< maxItemsCount; //incorrect :( why?
    15.  
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    but calculated max number of items is is incorrect. E.g. in case of my window height and font height I get 32 items when in fact current view has 28 items max. Perhaps someone can suggest something, how to calculate it properly?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,532
    Thanks
    253
    Thanked 723 Times in 713 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QListView max number of items in view

    First, resize event can be called multiple times as its parent widget is calculating the initial layout. So unless isVisible() returns true, you cannot use any of the dimensions for making calculations. So surround all of your code in the resizeEvent() with an "if ( isVisible() )" clause (except the call to the base class method, of course).

    Your code does not take into account the margins added around each item, which would result in a larger size (and thus fewer items visible). In any case, since you are using a QListView as the basis for your MyListView, then you are probably also using a QAbstractItemModel to supply information to the view. It is the QAbstractItemModel::data() with the role Qt::SizeHintRole that returns the size of each item and is used to calculate the layout. You should implement that in your model as a call to the base class method, then set a breakpoint to step in and see how the base class calculates the sizes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,642
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListView max number of items in view

    Quote Originally Posted by ArkKup View Post
    I need to calculate max number of items in current view of QListView.
    What problem are you trying to solve by knowing this? There may be a better solution.

  4. #4
    Join Date
    Jan 2011
    Posts
    23
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListView max number of items in view

    Quote Originally Posted by ChrisW67 View Post
    What problem are you trying to solve by knowing this? There may be a better solution.
    the problem is I can't load >50.000 items in listview at once so I just want to load items to fill in the current view

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,532
    Thanks
    253
    Thanked 723 Times in 713 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QListView max number of items in view

    the problem is I can't load >50.000 items in listview at once so I just want to load items to fill in the current view
    QListView and QTableView automatically manage the number of items in the view and do not load more than is required to fill the view. They use the methods implemented in QAbstractItemModel to retrieve only what they need. If you are having performance problems, it is probably in your model implementation, not in the views.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    ArkKup (3rd June 2020)

Similar Threads

  1. Replies: 0
    Last Post: 25th July 2015, 05:37
  2. Add Items to QListView
    By gaganbm in forum Newbie
    Replies: 5
    Last Post: 1st December 2012, 23:28
  3. QListView Items with Buttons
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2010, 16:48
  4. Replies: 4
    Last Post: 18th August 2009, 19:53
  5. Model/View Confusion in QTreeView - number of rows
    By themolecule in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 22:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.