Important: Please read the Qt Code of Conduct -

QT - How to add a button over a scrollArea (with absolute position)

  • I have the following layout

    enter image description here

    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:

    enter image description here

    And I would like to add that BLUE widget between home_page and scrollArea in terms of the outline, or other words in terms of GUI I would like to add that button over ScrollAreaContent BUT I don't want to move that button from its position at the moment when I'm moving the ScrollAreaContent.

    auto sizeX = 0.25 * w;
    auto sizeY = 0.9 * h;
    ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout());
    //This list is going to contain the buttons from scrollAreaWidgetContents
    auto buttons = new QList<MenuHomeButton *>();  
    //ADD buttons
    for (auto button : *buttons) {
        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?

  • Lifetime Qt Champion

    You mean so it just floats there on top ?
    alt text

    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 :)

Log in to reply