QPushButton size.

  • Hello guys, Inside a QWidget I put a Layout with two QPushButtons and a QLabel. I can change the geomtry of the QWidget that works as container for the layout but can not change the size of the QPushButton: here my code

    QTitleBar::resizeEvent( QResizeEvent * event)
    QSize size = event->size();
    int widgetHeight = size.rheight();
    int widgetWidth = size.rwidth();

    std::cout << "resizeEvent h:" <<
    << " w: " << widgetWidth << std::endl;

    int layoutH = (int)((float)widgetHeight*(float)0.20);

    std::cout << "resizeEvent h:" <<
    << " w: " << widgetWidth << std::endl;

    mapButton->setGeometry(0, 0, 120, layoutH);
    titleWidget->setGeometry(0, 0, widgetWidth, layoutH);

    // title->setGeometry(130, 0, widgetWidth - (2130), layoutH);
    // closeButton->setGeometry(widgetWidth - (2
    130), 0, 130, layoutH);

    //closeButton->setGeometry(0, 0, widgetWidth, layoutH);


    Is there something wrong?

  • Hi,
    If you want to change the layout according to the changing sizePolicy then use void "QWidget::adjustSize ()":http://qt-project.org/doc/qt-4.8/qwidget.html#adjustSize

  • need I to override QWidget::sizeHint()?

  • Actually if you have added the QWidgets to the layout and used widget->setLayout then when the user will change size of your widget, the elements in the layout will change size automatically. You can set -
    void setMaximumHeight ( int maxh )
    void setMaximumSize ( const QSize & )
    void setMaximumSize ( int maxw, int maxh )
    void setMaximumWidth ( int maxw )
    void setMinimumHeight ( int minh )
    etc....to restrict the sizes. However if you want to have a fixed defined layout then "QGroupBox":http://doc.qt.nokia.com/4.7-snapshot/qgroupbox.html and "QGridLayout":http://qt-project.org/doc/qt-4.8/QGridLayout.html will be more useful.

  • I see. If I set setMinimumSize of the QWidget "container", call QWidget::size() will return the size of the container?

  • Moderators

    Please use layouts instead of resizing widgets manually... That is just too much work and too fragile.

    So far I had to reimplement the resizeEvent once AFAIR, so if you do that regularly, then you are most likely doing something wrong (or work on a very unusual project where you might want to consider switching to QML).

  • I alredy tried it in this way:

    @ titleLayout = new QHBoxLayout( this );

    titleLayout->addWidget( mapButton , Qt::AlignLeft );
    titleLayout->addWidget( title, Qt::AlignCenter );
    titleLayout->addWidget( closeButton, Qt::AlignRight);@

    change the geometry of the layout will change the geometry of its children? Could you please provide an example?

    thank you

  • Using void QLayout::addWidget ( QWidget * w ), the layout of the elements will be from left to right (HBoxLayout) ....so just this should work fine -
    titleLayout = new QHBoxLayout( this );

    titleLayout->addWidget( mapButton);
    titleLayout->addWidget( title);
    titleLayout->addWidget( closeButton);

    change the geometry of the layout will change the geometry of its children?

    I am not sure what you want to achieve. Could you please elaborate.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.