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. QML GridView items size update. How?

QML GridView items size update. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 793 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello all!
    I have in QML GridView and I need to change size of items in it dynamically. How to do it?
    For example I have GridView on mobile device and when I see portrait I want to have one size of items and when landscape another size of item. I am perfectly aware of detecting orientation change. The question is only about QML GridView. How to redraw it by force when I need it?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      The question is only about QML GridView. How to redraw it by force when I need it?

      The same way you react to size changes everywhere: by binding to some properties (width and height for each cell), or using anchors, or using layouts (there is GridLayout, by the way).

      Maybe share the relevant part of your code here, it will be easier to help on a concrete example.

      (Z(:^

      B 1 Reply Last reply
      0
      • sierdzioS sierdzio

        The question is only about QML GridView. How to redraw it by force when I need it?

        The same way you react to size changes everywhere: by binding to some properties (width and height for each cell), or using anchors, or using layouts (there is GridLayout, by the way).

        Maybe share the relevant part of your code here, it will be easier to help on a concrete example.

        B Offline
        B Offline
        bogong
        wrote on last edited by
        #3

        @sierdzio

        ListModel {
        
        	id: oModel;
        
        	ListElement {name: "Jim Williams";}
        	ListElement {name: "John Brown";}
        	ListElement {name: "Bill Smyth";}
        	ListElement {name: "Sam Wise";}
        	ListElement {name: "Jim Williams";}
        	ListElement {name: "John Brown";}
        	ListElement {name: "Bill Smyth";}
        	ListElement {name: "Sam Wise";}
        }
        
        GridView {
        
        	property double pCellWidth: 0;
        	property double pCellHeight: 0;
        
        	signal sgItemSizeChanged();
        
        	id: oGridView;
        	anchors.fill: parent;
        	model: oModel;
        	cellHeight: oGridView.pCellHeight;
        	cellWidth: oGridView.pCellWidth;
        
        	delegate: Rectangle {
        
        		id: oDelegate;
        		width: oGridView.pCellWidth;
        		height: oGridView.pCellHeight;
        
        		Text {
        
        			id: name;
        			text: model.name;
        			anchors.centerIn: parent;
        		}
        
        		Connections {
        			target: oGridView;
        			function onSgItemSizeChanged() {
        
        				console.log("onSgItemSizeChanged");
        
        				oDelegate.mResizeItem();
        			}
        		}
        
        		function mResizeItem() {
        
        			console.log("mResizeItem");
        
        			oDelegate.width = oGridView.pCellWidth;
        			oDelegate.height = oGridView.pCellHeight;
        		}
        	}
        
        	onWidthChanged: {
        
        		console.log("onWidthChanged");
        
        		oGridView.mSetCellWidth();
        		oGridView.sgItemSizeChanged();
        	}
        
        	Component.onCompleted: {
        
        		console.log("Component.onCompleted");
        
        		oGridView.mSetCellWidth();
        		oGridView.sgItemSizeChanged();
        	}
        
        	function mSetCellWidth() {
        
        		console.log("mSetCellWidth");
        
        		if (oGridView.width > oGridView.height) {
        			oGridView.pCellWidth = oGridView.width * 0.5;
        		} else {
        			oGridView.pCellWidth = oGridView.width;
        		}
        	}
        }
        

        For now it's working wrong:

        • Ignoring first rotation
        • Using portrait proportion for landscape and landscape for portrait.
        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Whoa, that's complicated. Here's how I'd suggest to simplify it:

          GridView {
            id: oGridView
            anchors.fill: parent
            cellWidth: width > height? width * .5 : width
          
            delegate: Rectangle {
              width: oGridView.cellWidth
              height: oGridView.cellHeight
          
              Text {
                text: model.name
                anchors.centerIn: parent
              }
            }
          }
          

          (Z(:^

          B 1 Reply Last reply
          1
          • sierdzioS sierdzio

            Whoa, that's complicated. Here's how I'd suggest to simplify it:

            GridView {
              id: oGridView
              anchors.fill: parent
              cellWidth: width > height? width * .5 : width
            
              delegate: Rectangle {
                width: oGridView.cellWidth
                height: oGridView.cellHeight
            
                Text {
                  text: model.name
                  anchors.centerIn: parent
                }
              }
            }
            
            B Offline
            B Offline
            bogong
            wrote on last edited by bogong
            #5

            @sierdzio Ha-ha-ha! Coffee first! That is what I've been seeking ... Thx. 2 hours of sleepy-coding :-))) Issue closed.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bogong
              wrote on last edited by
              #6

              Example published here

              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