【问题标题】:QT - How to add a button over a scrollArea (with absolute position)QT - 如何在滚动区域上添加按钮(具有绝对位置)
【发布时间】:2020-10-21 22:21:07
【问题描述】:

我有以下布局

我在这里尝试做的是在滚动区域上放置一个自定义小部件(蓝色小部件)。

这是我的大纲:

我想在 home_pagescrollArea 之间添加那个蓝色小部件,就轮廓而言,或者换句话说,就 GUI 而言,我想在 ScrollAreaContent 上添加那个按钮,但我不想要在我移动 ScrollAreaContent 时将该按钮从其位置移开。

auto sizeX = 0.25 * w;
auto sizeY = 0.9 * h;

ui->scrollArea->setBaseSize(w,h);
ui->scrollAreaWidgetContents->setBaseSize(w,h);

ui->home_page->layout()->setMargin(0);
ui->home_page->layout()->setContentsMargins(0,0,0,0);

ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout());
ui->scrollAreaWidgetContents->layout()->setMargin(0);
ui->scrollAreaWidgetContents->layout()->setContentsMargins(0,0,0,0);

//This list is going to contain the buttons from scrollAreaWidgetContents
auto buttons = new QList<MenuHomeButton *>();  

//...
//ADD buttons
//...

ui->stackedWidget->setCurrentIndex(0);


for (auto button : *buttons) {
    ui->scrollAreaWidgetContents->layout()->addWidget(button);
    connect(button, SIGNAL(buttonClicked(int)), ui->stackedWidget, SLOT(setCurrentIndex(int)));
}
QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture);

你们中有人知道我该怎么做吗?

【问题讨论】:

  • 也许一种方法是按照 Pablo 的建议。将蓝色小部件设为子部件,并在每次水平滚动时移动/更新其位置。它会给人一种蓝色小部件固定在其位置的错觉。

标签: c++ qt5 qt-designer


【解决方案1】:

如果你想自己定位一个小部件,你不能将它添加到布局中。

您可以将小部件创建为容器的子小部件,然后在其上调用 show 方法。这样,新小部件将位于相对于容器的位置 (0,0)(当然,您可以将其移动到任何您想要的位置)。 您仍然可以在容器中使用布局来管理其他小部件,即:滚动区域。

我认为在您的情况下,您可以执行以下操作:

auto but=new QPushButton(ui->scrollAreaWidget);
but->show();

此外,我强烈建议您在计划这样做时不要使用设计器,根据我的经验,它更清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2015-03-28
    • 2013-07-13
    • 1970-01-01
    • 2016-05-04
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多