Results 1 to 10 of 10

Thread: Resizing buttons in Layout

  1. #1
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Resizing buttons in Layout

    So,I'm making a Tic Tac Toe game and I'd like to put my buttons,like: 3 rows of 3 buttons.
    So far I have this:
    Qt Code:
    1. win = new QWidget;
    2. btn[0] = new QPushButton(this);
    3. btn[1] = new QPushButton(this);
    4. btn[2] = new QPushButton(this);
    5. btn[3] = new QPushButton(this);
    6. btn[4] = new QPushButton(this);
    7. btn[5] = new QPushButton(this);
    8. btn[6] = new QPushButton(this);
    9. btn[7] = new QPushButton(this);
    10. btn[8] = new QPushButton(this);
    11. hl1 = new QHBoxLayout;
    12. hl2 = new QHBoxLayout;
    13. hl3 = new QHBoxLayout;
    14. vl1 = new QVBoxLayout;
    15.  
    16. hl1->addWidget(btn[0]);
    17. hl1->addWidget(btn[1]);
    18. hl1->addWidget(btn[2]);
    19.  
    20. hl2->addWidget(btn[3]);
    21. hl2->addWidget(btn[4]);
    22. hl2->addWidget(btn[5]);
    23.  
    24. hl3->addWidget(btn[6]);
    25. hl3->addWidget(btn[7]);
    26. hl3->addWidget(btn[8]);
    27.  
    28. vl1->addLayout(hl1);
    29. vl1->addLayout(hl2);
    30. vl1->addLayout(hl3);
    31.  
    32.  
    33. win->setLayout(vl1);
    34. TicTacToe::setCentralWidget(win);
    To copy to clipboard, switch view to plain text mode 

    But buttons here have the normal size..how can I make then be square?

    Thank you.

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Resizing buttons in Layout

    QButton default Horizontal Policy is Minimum and the Vertical Policy is Fixed. Try to change the sizePolicy property with QSizePolicy::Expanding or whatever you like, for example button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Resizing buttons in Layout

    A QGridLayout might be a good fit for this also.

  4. #4
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Resizing buttons in Layout

    Quote Originally Posted by viulskiez View Post
    QButton default Horizontal Policy is Minimum and the Vertical Policy is Fixed. Try to change the sizePolicy property with QSizePolicy::Expanding or whatever you like, for example button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)

    Yeah,thanks that worked.
    But now what I have is a window with buttons all over it(I mean,when I resize window,the layout keeps growing...),but what I want,is to make the buttons on the one side(like make the layout ungrowable!) and some other widgets on the other side,outside the layout...

  5. #5
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Resizing buttons in Layout

    btn[0]->setGeometry(150,260,150,35);
    ...
    setFixedSize(400, 400);

  6. #6
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Resizing buttons in Layout

    This doesn't help me...it keeps resizing layout.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Resizing buttons in Layout

    Put the other stuff into your layout(s) and adjust the size policies, stretch factors and maximum sizes of the widgets to do sane things with extra space.

  8. #8
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Resizing buttons in Layout

    U mean,to put the rests widgets to vl1?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Resizing buttons in Layout

    Yes, if that is the logical place for them. If not then you might need to nest your existing layout set into another horizontal box layout with more widgets to the right or left of the "grid" you have already.

  10. #10
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Resizing buttons in Layout

    Ok,I did the 2nd.

    Thanks

Similar Threads

  1. Replies: 0
    Last Post: 12th December 2010, 06:09
  2. Resizing dialog with form layout
    By elizabeth.h1 in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2009, 20:58
  3. Resizing a QTreeWidget inside a layout
    By bruccutler in forum Qt Programming
    Replies: 11
    Last Post: 27th March 2007, 16:35
  4. Resizing problems when applying a layout
    By JimBrown in forum Newbie
    Replies: 1
    Last Post: 21st February 2007, 23:54
  5. Resizing a Layout
    By vvbkumar in forum Qt Programming
    Replies: 2
    Last Post: 21st June 2006, 09:02

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.