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. QListView set height to show n rows
Forum Updated to NodeBB v4.3 + New Features

QListView set height to show n rows

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 261 Views 1 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have created a QListView, I want to set the fixed height to show 4 rows:

    mplvRecs = new QListView(this);
    mpsiModel = QStandardItemModel(4,4,this);
    mplvRecs->setModel(mpsiModel);
    QFontMetrics fntMetrics(mplvRecs->fontMetrics());
    int intHeight(fntMetrics.capHeight());
    intHeight += mplvRecs->spacing();
    mplvRecs->setFixedHeight(intHeight * 4);
    

    Something isn't right, intHeight is 8, spacing is 0 and the list view shows barely large enough to display 2 rows, how can I fix this?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by
      #2

      The cells do not only consist of the text (If that was the case the text of each row would touch at the top/bottom and it would look really messed up), but there is also some margin around it.
      Therefore you cannot use fontMetrics to get the row height.
      Why not use rowHeight() method to get row height?

      https://doc.qt.io/archives/qt-4.8/qtableview.html#rowHeight

      SPlattenS 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The cell is much higher than the text which is printed in there (as you can see when you look at your QListView). You should take a look at https://doc.qt.io/qt-5/qwidget.html#height-prop. Also there is some sacing around the actual view which you must consider.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • gde23G gde23

          The cells do not only consist of the text (If that was the case the text of each row would touch at the top/bottom and it would look really messed up), but there is also some margin around it.
          Therefore you cannot use fontMetrics to get the row height.
          Why not use rowHeight() method to get row height?

          https://doc.qt.io/archives/qt-4.8/qtableview.html#rowHeight

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          @gde23, because I'm using QListView.

          Kind Regards,
          Sy

          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