Results 1 to 8 of 8

Thread: How to center items into a cell of a GridView?

  1. #1
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to center items into a cell of a GridView?

    Hi,

    I have a GridView
    Qt Code:
    1. GridItemView
    2. {
    3. id: grid
    4. width: 500
    5. height: 200
    6.  
    7. cellWidth: 200
    8. cellHeight: 80
    9.  
    10. anchors.verticalCenter: parent.verticalCenter;
    11. anchors.horizontalCenter: parent.horizontalCenter;
    12.  
    13. model: myModel
    14. delegate: MyDelegate{}
    15. }
    To copy to clipboard, switch view to plain text mode 

    and MyDelegate is a simple rectagle with parent size:

    Qt Code:
    1. Rectangle
    2. {
    3. property int margin: 20;
    4. width: GridView.view.cellWidth - margin
    5. height: GridView.view.cellHeight - margin
    6.  
    7. Text
    8. {
    9. id: textValue
    10. text: modelVal
    11. horizontalAlignment: Text.AlignVCenter
    12. font.pixelSize: 30
    13. anchors.horizontalCenter: parent.horizontalCenter
    14. anchors.verticalCenter: parent.verticalCenter
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    The delegates have correct size but elements is aligned on the top-left of the cell grid.

    I've tried to add
    Qt Code:
    1. anchors.centerIn: parent.Center
    To copy to clipboard, switch view to plain text mode 
    in MyDelegate but don't works:all elements is placed at the center of GridView.

    I've tried also to add margins but don't works.

    How I can center the delegate element into the cell?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center items into a cell of a GridView?

    Try adding vertical alignment too ( similar to horizontalAlignment: Text.AlignVCenter)

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to center items into a cell of a GridView?

    javascript Code:
    1. Item {
    2. property int margin: 20
    3. width: GridView.view.cellWidth
    4. height: GridView.view.cellHeight
    5. Rectangle {
    6. anchors.fill: parent
    7. anchors.margins: margin
    8. Text {
    9. anchors.centerIn: parent
    10. // ...
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to center items into a cell of a GridView?

    Quote Originally Posted by aamer4yu View Post
    Try adding vertical alignment too ( similar to horizontalAlignment: Text.AlignVCenter)
    Tried but doesn't works.

    Quote Originally Posted by wysota View Post
    javascript Code:
    1. Item {
    2. property int margin: 20
    3. width: GridView.view.cellWidth
    4. height: GridView.view.cellHeight
    5. Rectangle {
    6. anchors.fill: parent
    7. anchors.margins: margin
    8. Text {
    9. anchors.centerIn: parent
    10. // ...
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    This works even if I wanted to avoid using another inner element.

    Thanks anyway!

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to center items into a cell of a GridView?

    If you don't care about colors, you can get rid of the rectangle element and use the following:

    javascript Code:
    1. Item {
    2. property int margin: 20
    3. width: ...
    4. height: ...
    5. Text {
    6. anchors { fill: parent; margins: margin }
    7. horizontalAlignment: Text.AlignHCenter
    8. verticalAlignment: Text.AlignVCenter
    9. // ...
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to center items into a cell of a GridView?

    I'm interested in colors. However, the second solution that's okay.. thanks again

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to center items into a cell of a GridView?

    It's not ok if you want to change the item background color.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to center items into a cell of a GridView?

    sorry I meant the solution at #3

Similar Threads

  1. Replies: 2
    Last Post: 28th October 2016, 11:12
  2. Center a widget in a cell on a QTableWidget
    By roleroz in forum Qt Programming
    Replies: 16
    Last Post: 5th March 2009, 14:47
  3. How to align list items to the center?
    By zgulser in forum Qt Tools
    Replies: 4
    Last Post: 9th February 2009, 09:52
  4. QTreeView: How to center a checkbox in a cell
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 12:11
  5. Q3listbox center items
    By sreedhar in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2006, 11:21

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.