Results 1 to 4 of 4

Thread: widget size does not change inside layout

  1. #1
    Join Date
    Nov 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default widget size does not change inside layout

    Hi,

    I am planning to add two widgets on my main widget in horizantal layout.
    The width of left most widget (buttonsWindow) should be very small compared to width of right most widget (imageWindow).The purpose of left most widget is to show some buttons with some text , and purpose right most widget is to show some image based on button from left most widget.

    I am using resize function to resize the widgets , but its not getting effected.

    Somebody please show some pointers on how to change widgets size inside layouts.




    Qt Code:
    1. #include "widget.h"
    2. #include<QVBoxLayout>
    3. #include<QHBoxLayout>
    4. #include<QPushButton>
    5. #include <QDesktopWidget>
    6. #include<QRect>
    7. #include <QApplication>
    8. #include <QDebug>
    9.  
    10. Widget::Widget(QWidget *parent)
    11. : QWidget(parent)
    12. {
    13. QRect rect;
    14. rect = setScreenSize();
    15.  
    16. QWidget *buttonsWindow = new QWidget;
    17. QWidget *imageWindow = new QWidget;
    18. QHBoxLayout *hbox = new QHBoxLayout;
    19.  
    20.  
    21. QVBoxLayout *vbox = new QVBoxLayout;
    22. QPushButton *ltButton = new QPushButton(tr("&ltButton"));
    23. QPushButton *NoC = new QPushButton(tr("&cdButton"));
    24.  
    25.  
    26. // buttonsWindow->resize(rect.width()-800, rect.height()-100);
    27. vbox->addWidget(ltButton);
    28. vbox->addWidget(NoC);
    29. buttonsWindow->setLayout(vbox);
    30.  
    31. QPalette Pal(palette());
    32.  
    33. // set black background
    34. Pal.setColor(QPalette::Background, Qt::gray);
    35. buttonsWindow->setAutoFillBackground(true);
    36. buttonsWindow->setPalette(Pal);
    37. // buttonsWindow->show();
    38. hbox->addWidget(buttonsWindow);
    39.  
    40. //imageWindow->resize(rect.width()-200, rect.height()-200);
    41. hbox->addWidget(imageWindow);
    42.  
    43. Pal.setColor(QPalette::Background, Qt::color1);
    44. imageWindow->setAutoFillBackground(true);
    45. imageWindow->setPalette(Pal);
    46.  
    47. setLayout(hbox);
    48. }
    49.  
    50. Widget::~Widget()
    51. {
    52.  
    53. }
    54.  
    55.  
    56. QRect Widget:: setScreenSize()
    57. {
    58. QRect rec = QApplication::desktop()->screenGeometry();
    59. resize(rec.width(), rec.height());
    60.  
    61. qDebug()<<"height="<<rec.height();
    62. qDebug()<<"width="<<rec.width();
    63.  
    64. return rec;
    65. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: widget size does not change inside layout

    If imageWindow should take the available space that is not needed by buttonWindow, then add it with a stretch factor > 0
    e.g.
    Qt Code:
    1. hbox->addWidget(imageWindow, 1);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: widget size does not change inside layout

    Wow, thanks, that worked well.

    Now in my vertical layout , the space between buttons is more , one is at the top and other is at the bottom.
    How can i arrange them with a fixed gap between them irrespective of buttons.
    i tried
    Qt Code:
    1. vbox->addWidget(ltButton);
    2. vbox->setSpacing(0);
    3. vbox->addWidget(NoC);
    To copy to clipboard, switch view to plain text mode 

    but does not work.

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

    Default Re: widget size does not change inside layout

    So you want the two buttons to be vertically centered?
    I.e. if the widget gets higher they stick together in the vertical middle?

    If so you can add stretch before and after them

    Qt Code:
    1. vbox->addStretch(1);
    2. vbox->addWidget(ltButton);
    3. vbox->addWidget(NoC);
    4. vbox->addStretch(1);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 5th September 2013, 17:33
  2. Resize widget to layout inside it.
    By mrc_pl in forum Newbie
    Replies: 2
    Last Post: 14th January 2012, 21:22
  3. Qt Designer Size of table widget inside Tab Widget is not flexible.
    By akash in forum Qt Tools
    Replies: 2
    Last Post: 14th September 2011, 12:45
  4. Hide widget, but not change layout
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 15:32
  5. How do I move a widget inside a grid Layout
    By barnabyr in forum Newbie
    Replies: 1
    Last Post: 9th May 2006, 00:23

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
  •  
Qt is a trademark of The Qt Company.