QT - How to add a button over a scrollArea (with absolute position)
-
I have the following layout
What I'm trying to do here it's to place a custom widget (the BLUE one) over a scrollArea.
Here it's how looks my outline:
And I would like to add that BLUE widget between
home_page
andscrollArea
in terms of the outline, or other words in terms of GUI I would like to add that button overScrollAreaContent
BUT I don't want to move that button from its position at the moment when I'm moving theScrollAreaContent
.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);
Does any of you have any idea how can I do this?
-
Hi
You mean so it just floats there on top ?
You simply make sure the parent you set on it, is not the scrollArea, but the widget that holds the scrollArea
In your case i think mainwindow's centralWidget() would be fine.You do understand that having a floating widget like that might have side effects when
you resize the window as it wont follow ScrollArea at all. So might end up in odd position if
you resize main window a lot :)