scroll a QWidget with children



  • Hi,

    I'd like to create an app with scrolling ability. The central widget is based on QWidget, that has some "slides" inside a QHBoxLayout. There are a lot of small details that I think are not important for the question, but feel free to ask in case.

    So the central widget can scroll horizontally, but I didn't want scroll bars at all, so I'm using the built in

    void QWidget::scroll(int dx, int dy)
    

    function to scroll the content. Mainly it works as expected, but there's a problem. Each child slide has a QLabel on them, and when I change the text with

    void QLabel::setText(const QString &)
    

    It moves the scroll position to the left side of the container. No matter if that slide is visible or not, no matter which label, it always jumps to the far left. Both on linux desktop and android.

    I think it's something with a dirty unpainted area, but I can't fix it anyhow. Please help!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should add which version of Qt you are using, which Linux distribution and Android version.

    You should also provide a minimal compilable example that shows the behaviour so other people can take a look at it.


  • Qt Champions 2017

    Hi and welcome to the forums.
    If you scroll the labels inside a layout , i assume what is happening is that when setting text,
    the layout recalculate to take account the new text.

    At least that idea was very fast to reproduce.
    alt text

    And the reason i think its that is because requesting it to recalculate reset labels positions.
    ui->verticalLayout->invalidate();
    ui->verticalLayout->activate();


    Could you not just use a ScrollArea and hide the bars ?



  • Hi,

    I'm using currently latest Qt 5.11.1-1 on my Arch linux. I'm using Android 7.1.2 .

    I looked at the code of QScrollArea, and saw that it uses

    void QWidget::move(const QPoint &)
    

    in the background for the viewport widget. Replacing scroll into move wasn't a good idea though in all of the cases, but a mixture of them will be fine I guess. I think it will be okay soon, but if I don't succeed, I'll replace the whole thing with a QScrollArea.


Log in to reply
 

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