在Qt Creator中,将窗口中的按钮右对齐(最好不要嵌套在布局小部件中)。

huangapple go评论58阅读模式
英文:

In Qt Creator, right-align a button in a window (preferably without nesting in layout widgets)

问题

我创建了一个全新的桌面项目。

在设计师中,我在包含的mainwindow.ui中拖入了一个按钮。

当我运行应用程序并通过拖动右下角来调整窗口大小时,按钮不会移动。它锁定在左上角。

这在所有GUI工具/工具包中都是标准的默认行为。

但是当我尝试将按钮锁定到窗口的底部和右侧边缘时,我在属性编辑器中找到的任何内容似乎都不起作用。显然,这是可能的,我只是漏掉了什么。

这里的诀窍是什么?请记住,我更希望不必嵌套按钮在另一个容器中来完成它(即:窗口/布局小部件/按钮)。如果那是唯一的方法,那就没关系,但如果有一种不引入额外小部件的方法来完成它,我会更感兴趣。

英文:

I created a brand new desktop project.

In the designer, in the included mainwindow.ui, I dragged in a push button.

When I run the app and resize the window by dragging around the bottom-right corner, the button does not move. It's locked to the top-left corner.

Pretty standard default behavior in all gui tools/kits.

But when I try to lock the button to the bottom and right edges of the window, nothing I've found in the properties editor seems to work. Obviously this is possible and I'm just missing something.

What's the trick here? -- keeping in mind that I'd prefer not to have to nest the button in another container to do it (i.e: window/layoutwidget/button). If that's the only way, fine, but if there's a way to do it without bringing in extra widgets then I'd be more interested in that.

答案1

得分: 1

只返回翻译好的部分:

如果您不想使用布局,那么我认为您需要重写 resizeEvent(QResizeEvent *event) 并手动移动按钮。

void MainWindow::resizeEvent(QResizeEvent *ev)
{
    if(this->isVisible()) {
        QRect buttonGeo = ui->pushButton->geometry();
        buttonGeo.translate(ev->size().width() - ev->oldSize().width(),
                            ev->size().height() - ev->oldSize().height());
        ui->pushButton->setGeometry(buttonGeo);
    }
}

这段代码根据窗口大小的变化来移动(平移)按钮。为了防止窗口的初始“oldSize”未设置,我添加了对MainWindow的“isVisible”的检查。

在QML中,使用 anchor.bottom/anchor.right 属性非常简单进行锚定。

看起来您可以在非QML中使用锚定布局,但您需要使用 QGraphicsScene + QGraphicsAnchorLayout(https://doc.qt.io/qt-6.4/qtwidgets-graphicsview-anchorlayout-example.html)。

如果您使用常规布局:为了防止按钮调整大小,可以设置按钮的最大宽度和/或将布局的拉伸因子设置为1,0(其中0是包含按钮的列)。这两个操作都可以在Designer中完成。

英文:

If you don't want to use layouts, then I think you're stuck with overriding resizeEvent(QResizeEvent *event) and moving the button manually.

void MainWindow::resizeEvent(QResizeEvent *ev)
{
    if(this->isVisible()) {
        QRect buttonGeo = ui->pushButton->geometry();
        buttonGeo.translate(ev->size().width() -ev->oldSize().width(),
                            ev->size().height()-ev->oldSize().height());
        ui->pushButton->setGeometry(buttonGeo);
    }
}

This moves (translates) the button based on how much the size of the window has changed. To protect against the initial "oldSize" of the window not being set, I added a check against "isVisible" of the MainWindow.

Anchoring is trivial in QML, though, with the anchor.bottom/anchor.right properties.

It looks like you can do anchoring layouts in non-QML, but you need to use a QGraphicsScene + QGraphicsAnchorLayout (https://doc.qt.io/qt-6.4/qtwidgets-graphicsview-anchorlayout-example.html).

If you use regular layouts: To prevent the button from resizing, set the maximum width of the button and/or set the stretch factors of the layout to 1,0 (where 0 is the column with the button). Both can be done in Designer.

huangapple
  • 本文由 发表于 2023年6月5日 10:04:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定