[Solved] QScrollArea into QFrame



  • Hi, i have create my custom widget, this is like a rectangle. Now i want a cointaner and add this widget row by row, like a 'table'. All works well. I crate widget add in QFrame and move position under the previuos widget. The problem is when i have more widget but my QFrame is too short and some widget is hidden. So i think add a QScrollArea is the solution but i have more problem.

    This is my code to add a QScrollArea to my QFrame

    @
    QScrollArea scroll = new QScrollArea(this);
    scroll->setWidget(ui->frame);
    @

    Problem #1 QScrollArea become the parent of QFrame so QFrame change its geometry,
    Solution#1 I resolved with this code (but i don't know if is the best way)

    @
    QScrollArea scroll = new QScrollArea(this);
    QRect pos = ui->frame->geometry();
    scroll->setWidget(ui->frame);
    scroll->setGeometry(pos);
    @

    Problem #2 Now the Scrollbar is show in correct position and show the QScrollBar. But i don't understand why show the orizontal QScrollBar the geometry is the geometry of the QFrame so not need an horizontal QScrollBar but the scrollbar add a 'special' space and i don't know why.

    Problem #3 The Vertical scrollbar is view but scroll only the widget that i see the other (under the QFrame so is hidden) don't scroll.

    I think that is because QScrollBar is add to a QFrame and QFrame haven't to scroll something, the scrollbar don't know the subwidget of the QFrame.

    So how can i resolve this?

    In the fact i only create a container (and i like have a border 1px) with i have my widget row by row with a QScrollBar.
    Thanks.



  • You're making "spaghetti widgets" :-)

    Usually you have the ui object and put it into the current widget with ui.setupUi(this). If you now add an additional scrollarea to this widget, that's ok, but you mess up with the layout! If you now move something from this widgets into the scroll area, things get really messed up!

    I would try a solution like this (brain to terminal, not tested):

    @
    Ui:myWidget ui;
    QScrollArea *scrollArea;
    QWidget *uiWidget;

    myWidget::myWidget(QWidget *parent)
    {
    scrollArea = new QScrollArea(this);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(scrollArea);
    setLayout(mainLayout);

    uiWidget = new QWidget(this);
    ui.setupUi(uiWidget);
    
    scrollArea->setWidget(uiWidget);
    

    }
    @



  • I found a solution. The solution is create a layout like this:

    QScrollArea -> QWidget (like a containers) -> QVBoxLayout -> QWidget (my real qwidget)

    The code is something like this:

    @
    containerWidget = new QWidget();
    scrollContainer = new scrollContainer();
    scrollContainer->setWidget(containerWidget);
    layout = new QVBoxLayout(this);
    containerWidget->setLayout(layout);

    layout->addWidget(myWidget1);
    layout->addWidget(myWidget2);
    layout->addWidget(myWidget3);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.