How to bind pushButton to a widget?

I created a very simple window with one button on it. My button id is 10 pixels from the right edge of the window and 10 from the bottom. I would like to keep this position even when the window is resized. This means that there are still 10 on the right and 10 on the bottom.

How to do it?

thank

zalkap

+3
source share
2 answers

Install a QGridLayout in a widget with two columns and two rows, add a button to the lower right cell, then set the first row and first column to stretch.

QWidget *widget = new QWidget(); // The main window
QGridLayout *layout = new QGridLayout(widget); // The layout
QPushButton *button = new QPushButton(QString("Button"), widget); // The button

layout->setContentsMargin(10,10,10,10); // To have 10 pixels margins all around the widget
layout->addWidget(button, 1, 1);
layout->setRowStretch(0, 1);
layout->setColumnStretch(0, 1);
+6
source

, . , ( - , , ..). - ( ), resizeEvent() . .

void MyParentWidget::resizeEvent( QResizeEvent* ) {
    m_child->move( width() - m_child->width() - 10, height() - m_child->height() - 10 ); 
}

.

+3

Source: https://habr.com/ru/post/1763917/


All Articles