提问者:小点点

如何在滚动区上添加按钮(带绝对位置)


我有以下布局

我在这里要做的是将一个定制的小部件(蓝色的那个)放在一个滚动区上。

下面是我的大纲:

我想在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);

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


共1个答案

匿名用户

如果您想自己定位一个小部件,则不得将其添加到布局中。

您可以将小部件创建为容器的子小部件,然后对其调用show方法。 这样,新的小部件将位于相对于容器的位置(0,0)(当然,您可以将其移动到您想要的任何地方)。 您仍然可以在容器中有一个用于管理其他小部件的布局,例如:ScrollArea。

我觉得在你的情况下你可以这样做:

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

此外,我强烈建议你不要使用设计师,当你正在计划这样做,在我的经验是更清楚的。