Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. need ideas on styling a ListView
Forum Updated to NodeBB v4.3 + New Features

need ideas on styling a ListView

Scheduled Pinned Locked Moved Solved Brainstorm
27 Posts 4 Posters 4.5k Views 2 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.
  • M mzimmers
    13 Dec 2022, 20:37

    @jeremy_k said in need ideas on styling a ListView:

    As a style guide, never reference parent or anything outside of the component. Let the instantiator impose its constraints. Use implicitWidth and implicitHeight to specify reasonable defaults.

    Understood. How do you handle a case where the instantiator doesn't know the height, because (for example) there might be a variable number of rows? I'm hoping to make this reusable.

    J Offline
    J Offline
    jeremy_k
    wrote on 13 Dec 2022, 23:21 last edited by jeremy_k
    #21

    @mzimmers said in need ideas on styling a ListView:

    @jeremy_k said in need ideas on styling a ListView:

    As a style guide, never reference parent or anything outside of the component. Let the instantiator impose its constraints. Use implicitWidth and implicitHeight to specify reasonable defaults.

    Understood. How do you handle a case where the instantiator doesn't know the height, because (for example) there might be a variable number of rows? I'm hoping to make this reusable.

    implicitHeight:

    If width or height is not specified, an item's effective size will be determined by its implicitWidth or implicitHeight.

    However, if an item is the child of a layout, the layout will determine the item's preferred size using its implicit size. In such a scenario, the explicit width or height will be ignored.

    The default implicit size for most items is 0x0, however some items have an inherent implicit size which cannot be overridden, for example, Image and Text.

    Setting the implicit size is useful for defining components that have a preferred size based on their content

    Asking a question about code? http://eel.is/iso-c++/testcase/

    M 1 Reply Last reply 14 Dec 2022, 00:11
    0
    • J jeremy_k
      13 Dec 2022, 23:21

      @mzimmers said in need ideas on styling a ListView:

      @jeremy_k said in need ideas on styling a ListView:

      As a style guide, never reference parent or anything outside of the component. Let the instantiator impose its constraints. Use implicitWidth and implicitHeight to specify reasonable defaults.

      Understood. How do you handle a case where the instantiator doesn't know the height, because (for example) there might be a variable number of rows? I'm hoping to make this reusable.

      implicitHeight:

      If width or height is not specified, an item's effective size will be determined by its implicitWidth or implicitHeight.

      However, if an item is the child of a layout, the layout will determine the item's preferred size using its implicit size. In such a scenario, the explicit width or height will be ignored.

      The default implicit size for most items is 0x0, however some items have an inherent implicit size which cannot be overridden, for example, Image and Text.

      Setting the implicit size is useful for defining components that have a preferred size based on their content

      M Offline
      M Offline
      mzimmers
      wrote on 14 Dec 2022, 00:11 last edited by mzimmers
      #22

      @jeremy_k I see. So, this seems to work:

      StackLayout {
      
          Home {}
          Equipment {}
          Rectangle {
              Layout.fillHeight: true
              Layout.fillWidth: true
      
              Roundlist {
                  width: parent.width
              }
          }
      }
      
      // Roundlist.qml
      Item {
          implicitHeight: activityView.height + (rect1.rectRadius * 2)
          ...
      

      And I can live with this, though it seems weird to set the height in once place, and the width in another. Is there a cleaner way to do this?

      J 1 Reply Last reply 14 Dec 2022, 04:57
      0
      • T TomZ
        13 Dec 2022, 20:16

        Apologies if this is answered, I only read the first 2 posts of this long topic.

        I worked on something very similar just last month and the QML is really simple. (full).

        delegate: Item {
          height: 80
          clip: true
          Rectangle {
            // we always have the rounded circles, but if we should not see them, 
            // we move them out of the clipped area.
            height: {
                 var h = 80;
                 if (index == 0)
                     h += 20
                 if (index == MAX) // donno, bottom check.
                    h += 20
                return h;
              }
              radius: 20
              y: index == 0 ? 0 : -20
          }
        }
        M Offline
        M Offline
        mzimmers
        wrote on 14 Dec 2022, 00:23 last edited by
        #23

        @TomZ thanks for the reply. I think I prefer my implementation for my particular purposes, but I like elements of yours as well.

        1 Reply Last reply
        1
        • M mzimmers
          14 Dec 2022, 00:11

          @jeremy_k I see. So, this seems to work:

          StackLayout {
          
              Home {}
              Equipment {}
              Rectangle {
                  Layout.fillHeight: true
                  Layout.fillWidth: true
          
                  Roundlist {
                      width: parent.width
                  }
              }
          }
          
          // Roundlist.qml
          Item {
              implicitHeight: activityView.height + (rect1.rectRadius * 2)
              ...
          

          And I can live with this, though it seems weird to set the height in once place, and the width in another. Is there a cleaner way to do this?

          J Offline
          J Offline
          jeremy_k
          wrote on 14 Dec 2022, 04:57 last edited by
          #24

          @mzimmers said in need ideas on styling a ListView:

          And I can live with this, though it seems weird to set the height in once place, and the width in another. Is there a cleaner way to do this?

          The component can set implicitWidth as well, and probably should. Otherwise you might end up with a 0-width item that renders correctly on its own, but is overlapped when used in a row layout of some form.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          M 1 Reply Last reply 14 Dec 2022, 05:00
          0
          • J jeremy_k
            14 Dec 2022, 04:57

            @mzimmers said in need ideas on styling a ListView:

            And I can live with this, though it seems weird to set the height in once place, and the width in another. Is there a cleaner way to do this?

            The component can set implicitWidth as well, and probably should. Otherwise you might end up with a 0-width item that renders correctly on its own, but is overlapped when used in a row layout of some form.

            M Offline
            M Offline
            mzimmers
            wrote on 14 Dec 2022, 05:00 last edited by
            #25

            @jeremy_k said in need ideas on styling a ListView:

            The component can set implicitWidth as well, and probably should.

            I'll do that -- just wondering whether there was any way to derive a value from the caller. But I guess the width value is just that, so...that's what I'll use. I'll just pick some value for the implicitWidth that hopefully will never get used.

            Thanks for all the help...I think I'm done with this one.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jeremy_k
              wrote on 14 Dec 2022, 05:04 last edited by
              #26

              FontMetrics could provide an implicitWidth value based on the model's data. I presume that Text does something similar.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mzimmers
                wrote on 14 Dec 2022, 23:45 last edited by
                #27

                For anyone who might be reading this for an answer to their own question, I looked at @TomZ 's solution, and realized that I had a bug in my delegate - my text centering wasn't taking the rounded corners effectively adding height to the top and bottom rows. I had to alter my delegate:

                Component {
                    id: activityDelegate
                    Rectangle {
                        height: {
                            var h = rect1.rowHeight;
                            if (index === 0 || index === (activityModel.count - 1))
                                h -= rect1.radius
                            return h;
                        }
                        width: rect1.width
                        color: rect1.color
                        Text {
                            text: model.parameter
                            anchors {
                                left: parent.left
                                leftMargin: parent.height / 2
                                verticalCenter: parent.verticalCenter
                                verticalCenterOffset: {
                                    var o = 0;
                                    if (index === 0)
                                        o -= (radius / 2)
                                    else if (index === (activityModel.count - 1))
                                        o += (radius / 2)
                                    return o;
                                }
                            }
                
                            font.pixelSize: 14
                            font.bold: true
                        }
                        Styledswitch {
                            id: onOff
                            visible: model.switchVisible
                            anchors {
                                right: parent.right
                                rightMargin: parent.height / 2
                                verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                }
                

                As always, if this doesn't look right, please let me know, and I'll fix it.

                1 Reply Last reply
                0

                21/27

                13 Dec 2022, 23:21

                • Login

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