在qt中,如何在调整小部件的大小时使按钮相应地移动

在qt中,如何在调整小部件的大小时使按钮相应地移动,qt,resize,move,Qt,Resize,Move,我想这样做:当我按下并移动鼠标放大我的主窗口时,我想让小部件(像按钮)相应地移动(而不是调整按钮大小),以确保按钮始终位于主窗口的边缘。我可以这样做: void MainWindow::resizeEvent(QResizeEvent *e) { int x,y,newx,newy,resizex,resizey; newx = this->width(); newy = this->height(); resizex = newx - mainwindowWidth; resize

我想这样做:当我按下并移动鼠标放大我的主窗口时,我想让小部件(像按钮)相应地移动(而不是调整按钮大小),以确保按钮始终位于主窗口的边缘。我可以这样做:

void MainWindow::resizeEvent(QResizeEvent *e)
{
int x,y,newx,newy,resizex,resizey;
newx = this->width();
newy = this->height();
resizex = newx - mainwindowWidth;
resizey = newy - mainwindowHeight;
x = ui->button->pos().x();
y = ui->button->pos().y();
ui->button->move(x+resizex, y+resizey);
mainwindowWidth = newx;
mainwindowHeight = newy;
}

但它太复杂了。QPushButton是否具有轻松完成此工作的任何属性?等待你的回答。任何建议都会有帮助。提前感谢。

您可以将按钮添加到布局中,并添加垫片以将按钮推到边缘

举个例子:

QHBoxLayout *hlayout = new QHBoxLayout;
ui->verticalLayout->addLayout(hlayout);

QPushButton *button = new QPushButton("Button");
hlayout->addWidget(button);
QSpacerItem *item = new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed);    
hlayout->addSpacerItem(item);

你能给出一些非常简单的代码来说明你的观点吗?谢谢,我试过了,但是失败了。@user2231578我修改了我的答案并添加了一个例子。基本上你的按钮是一样的。您也可以在designer中执行此操作。