Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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->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?


  • Lifetime Qt Champion

    Hi
    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