qt - How to resize buttons automatically if the window changes its size? -
i have window , bunch of push buttons. window "main menu". after placing buttons, have these buttons fixed size of window. should fill window , change size if window changes (by user instance).
how do that?
you should use layouts. according documentation,
the qt layout system provides simple , powerful way of automatically arranging child widgets within widget ensure make use of available space.
example:
main.cpp
#include "widget.h" #include <qapplication> int main(int argc, char *argv[]) { qapplication a(argc, argv); widget w; w.show(); return a.exec(); }
widget.h
#ifndef widget_h #define widget_h #include <qwidget> class widget : public qwidget { q_object public: explicit widget(qwidget *parent = 0); ~widget(); private: };
widget.cpp
#include "widget.h" #include <qpushbutton> #include <qhboxlayout> widget::widget(qwidget *parent) : qwidget(parent) { qpushbutton *button1 = new qpushbutton("one"); qpushbutton *button2 = new qpushbutton("two"); //horizontal layout 2 buttons qhboxlayout *hblayout = new qhboxlayout; hblayout->addwidget(button1); hblayout->addwidget(button2); qpushbutton *button3 = new qpushbutton("three"); qpushbutton *button4 = new qpushbutton("four"); //vertical layout 2 buttons qvboxlayout *vblayout = new qvboxlayout; vblayout->addwidget(button3); vblayout->addwidget(button4); //we add our 2 layouts (horizontal & vertical) //following vertical layout. qvboxlayout *mainlayout = new qvboxlayout; mainlayout->addlayout(hblayout); mainlayout->addlayout(vblayout); this->setlayout(mainlayout); } widget::~widget() { }
Comments
Post a Comment