Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Listview hide element which is not inside Listview boundaries (height, width)
Forum Updated to NodeBB v4.3 + New Features

Listview hide element which is not inside Listview boundaries (height, width)

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 846 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.
  • A Offline
    A Offline
    aheimberger
    wrote on last edited by aheimberger
    #1

    Hey Guys,

    I have following problem. I have a Listview which has multiple items. The ListView has only a specific width and height, inheriting from its parent (shown green). When I have i.e. 3 items all of them are shown within the listview. However, I only want to show the two items, which fit into the visible area of the listview. Not cutting the last element, but removing it.

    Current Result:
    0_1509122356805_Selection_002.png

    Target:
    0_1509122378264_Selection_002 (copy).png

    import QtQuick 2.6
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Example")
    
        Rectangle {
            width: 200
            height: 160
            anchors.centerIn: parent
            border.width: 2
            border.color: "green"
    
            ListModel {
                id: myModel
    
                ListElement {
                    name: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
                }
                ListElement {
                    name: "hello"
                }
                ListElement {
                    name: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
                }
            }
    
    
            ListView {
                anchors.fill: parent
                anchors.margins: 5
                model: myModel
                clip: true
                interactive: false
    
                delegate:
                    Rectangle {
                        width: parent.width
                        height: textElement.paintedHeight
                        border.color: "grey"
                        border.width: 1
    
                        Text {
                            id: textElement
                            width: parent.width
                            text: name;
                            font.pixelSize: 20
                            wrapMode: Text.WrapAnywhere
                        }
                    }
            }
        }
    }
    
    E 1 Reply Last reply
    0
    • A aheimberger

      Hey Guys,

      I have following problem. I have a Listview which has multiple items. The ListView has only a specific width and height, inheriting from its parent (shown green). When I have i.e. 3 items all of them are shown within the listview. However, I only want to show the two items, which fit into the visible area of the listview. Not cutting the last element, but removing it.

      Current Result:
      0_1509122356805_Selection_002.png

      Target:
      0_1509122378264_Selection_002 (copy).png

      import QtQuick 2.6
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Example")
      
          Rectangle {
              width: 200
              height: 160
              anchors.centerIn: parent
              border.width: 2
              border.color: "green"
      
              ListModel {
                  id: myModel
      
                  ListElement {
                      name: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
                  }
                  ListElement {
                      name: "hello"
                  }
                  ListElement {
                      name: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
                  }
              }
      
      
              ListView {
                  anchors.fill: parent
                  anchors.margins: 5
                  model: myModel
                  clip: true
                  interactive: false
      
                  delegate:
                      Rectangle {
                          width: parent.width
                          height: textElement.paintedHeight
                          border.color: "grey"
                          border.width: 1
      
                          Text {
                              id: textElement
                              width: parent.width
                              text: name;
                              font.pixelSize: 20
                              wrapMode: Text.WrapAnywhere
                          }
                      }
              }
          }
      }
      
      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @aheimberger From the ListView docs: "Note: Views do not enable clip automatically. If the view is not clipped by another item or the screen, it will be necessary to set clip: true in order to have the out of view items clipped nicely. "

      A 1 Reply Last reply
      0
      • E Eeli K

        @aheimberger From the ListView docs: "Note: Views do not enable clip automatically. If the view is not clipped by another item or the screen, it will be necessary to set clip: true in order to have the out of view items clipped nicely. "

        A Offline
        A Offline
        aheimberger
        wrote on last edited by aheimberger
        #3

        @Eeli-K Sorry not the solution to my problem. But yes you are correct. I forgot the clip: true within my example. I will update the code.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aheimberger
          wrote on last edited by aheimberger
          #4

          This would actually solve my Problem, but actually this code is quite a bogus.

          import QtQuick 2.6
          import QtQuick.Window 2.2
          
          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Example")
          
              Rectangle {
                  width: 200
                  height: 160
                  anchors.centerIn: parent
                  border.width: 2
                  border.color: "green"
          
                  ListModel {
                      id: myModel
          
                      ListElement {
                          name: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
                      }
                      ListElement {
                          name: "hello"
                      }
                      ListElement {
                          name: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
                      }
                  }
          
          
                  ListView {
                      id: myListView
                      anchors.fill: parent
                      anchors.margins: 5
                      model: myModel
                      clip: true
                      interactive: false
          
                      property int sumUpItemsHeight: 0
                      delegate:
                          Rectangle {
                              id: myThing
                              width: parent.width
                              height: textElement.paintedHeight
                              border.color: "grey"
                              border.width: 1
                              Component.onCompleted: {
                                  myListView.sumUpItemsHeight += myThing.height
                                  if (myListView.sumUpItemsHeight > myListView.height) {
                                      myThing.visible = false;
                                  }
                              }
          
                              Text {
                                  id: textElement
                                  width: parent.width
                                  text: name;
                                  font.pixelSize: 20
                                  wrapMode: Text.WrapAnywhere
                              }
                          }
                  }
              }
          }
          
          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