Results 1 to 5 of 5

Thread: Change elements size in a grid

  1. #1
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Change elements size in a grid

    Hello and Happy New Year,
    I'm trying to create a simple grid in QML whose elements are rectangles that contains a image and a text. I want to create a mobile-like menu.
    I've two files, one with a generic menu item and another with the grid of those items.
    It works well if the height and width of the items are hardcoded but it doesen't if I try to adapt on the parent size (height: parent.height/3). All of the items appear on the center of the parent and the image is not show.

    How can I achive it, so when the parent size changes the elements will adapt?

    Code:
    Qt Code:
    1. //mitem.qml
    2. import QtQuick 2.0
    3.  
    4. Rectangle {
    5. width: 50 // Doesn't work width: parent.width/3
    6. height: width
    7. Image{
    8. id:img
    9. width: parent.width*3/5
    10. height: width
    11. anchors.horizontalCenter: parent.horizontalCenter
    12. source: "http://qmlbook.org/_images/flow.png"
    13.  
    14. }
    15. Text{
    16. anchors.bottom: parent.bottom
    17. anchors.horizontalCenter: parent.horizontalCenter
    18. text: "Test"
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //main.qml
    2. import QtQuick 2.0
    3.  
    4. Rectangle{
    5. id: root
    6. width: 160
    7. height: 160
    8. Grid {
    9. id: grid
    10. rows: 2
    11. columns: 2
    12. anchors.centerIn: parent
    13. spacing: 8
    14. mitem { }
    15. mitem { }
    16. mitem { }
    17. mitem { }
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Change elements size in a grid

    Well, if the Grid's size is determined by the size of its children, then the children can hardly reference the Grid's size. Otherwise you have an unsolvable loop.

    Maybe you want to refer to the size of the Rectangle root?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Change elements size in a grid

    Hello,
    I noticed that after I posted.
    I've found a workaround, it won't be the best, but at least It works in some way.
    Qt Code:
    1. //in mitem.qml
    2. //In Rectangle
    3. width: parent.parent.width/4
    To copy to clipboard, switch view to plain text mode 

    The grids width isn't known but the grid's parent's is, So knowing the layout of the app It could help.

    Anyway, Is there any better way to achive a resizable elements grid?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Change elements size in a grid

    Quote Originally Posted by asieriko View Post
    Hello,
    I noticed that after I posted.
    I've found a workaround, it won't be the best, but at least It works in some way.
    Qt Code:
    1. //in mitem.qml
    2. //In Rectangle
    3. width: parent.parent.width/4
    To copy to clipboard, switch view to plain text mode 
    I would do that in main.qml instead

    Qt Code:
    1. //main.qml
    2. import QtQuick 2.0
    3.  
    4. Rectangle{
    5. id: root
    6. width: 160
    7. height: 160
    8. Grid {
    9. property int itemWidth: parent.width / 3
    10. id: grid
    11. rows: 2
    12. columns: 2
    13. anchors.centerIn: parent
    14. spacing: 8
    15. mitem { width: parent.itemWidth }
    16. mitem { width: parent.itemWidth }
    17. mitem { width: parent.itemWidth }
    18. mitem { width: parent.itemWidth }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    I.e. don't make assumptions inside a component about the components surroundings.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Change elements size in a grid

    Thank you,
    This is much more elegant!

    And It had helped me understanding more the way qml works. Thank you!

Similar Threads

  1. Change state on dynamic elements.
    By Binpix in forum Qt Quick
    Replies: 5
    Last Post: 1st October 2013, 23:20
  2. change text size and window size - qmessagebox
    By smemamian in forum Newbie
    Replies: 3
    Last Post: 4th July 2013, 07:38
  3. Replies: 1
    Last Post: 19th July 2010, 07:21
  4. Style size of elements
    By 1111 in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2009, 14:43
  5. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 19:16

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.