Unable to move ScrollBar to the top
-
Hello,
I have a QScrollArea where I display buttons. Here's a screen:
I want to move the scroll bar to the top like in the screenshot shown above every time that a button is selected. For example, in a situation like this:
If the button "Churrasquera" is selected, It'll be moved to the top of the layout of the QScrollArea. This works fine, but when trying to move the scroll bar to the top to show the button previously selected, nothing happens. I'm using this line of code:ui->scrollArea->verticalScrollBar()->setValue(0);
I've tried adding a QScrollBar manually, like this:
QScrollBar* scrollBar = new QScrollBar(this); ui->scrollArea->setVerticalScrollBar(scrollBar);
But the result is the same. It's weird, because when I ask which are the current value of the scrollBar, the minimum, and the maximum, I get 0 for each value.
Code:qDebug() << "current: " << ui->scrollArea->verticalScrollBar()->value(); qDebug() << "min: " << ui->scrollArea->verticalScrollBar()->minimum(); qDebug() << "max: " << ui->scrollArea->verticalScrollBar()->maximum();
Output:
current: 0 min: 0 max: 0
It's like the scroll bar doesn't change position, even though it clearly does.
Does anyone know how to fix this?Thanks in advance.
-
@SGaist I changed the desing: Now QPushButtons are displayed in the QScrollArea, so the Button with the ssid and the other elements (The line to write a password, the button to connect to a network) are shown separately. With these simple changes, the code I wrote before works like a charm! True, It wasn't what I originally intended, but I realized that it's far better due to the circumstances where this app is going to be used.
Thanks for your time and your help! -
Hi,
How are you moving this button ?
Do you have a widget set on the QScrollArea ? -
Wouldn't a QListWidget be simpler to use in your case ?
-
@SGaist Well, I've tried it but it doesn't work (I think).
The problem is that, when one of those buttons is clicked, It'll show a QLineEdit for password and a QPushButton to connect, like this:
Button in normal state:
Button clicked:
The problem is that these are not simple QPushButtons, but widgets of my own custom class that extends QFrame. So, when I put these same buttons in a QListWidget, this is what I get:
At first, every button is just a blue rectangle like seen in the first rows, but when clicked they extend onto the buttons below. So, the only button that works correctly is the last one. Is there a way to give more space to each button? And allow them to change size without breaking everything? -
Ok, I thought you were showing just a list of item but since it's a custom widget, the QScrollArea should be fine. Did you try to use QScrollArea::ensureWidgetVisible ?
-
AFAIK, it should not.
Can you reproduce that issue if you build a minimal application with a QScrollArea that you fill with plain QPushButton ?
-
@SGaist Nope, I did what you ask and it works perfectly. It doesn't make sense, the code is the almost the same. This is what I wrote:
QVBoxLayout* lay = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout()); for (int i = 0; i<20; i++) { QPushButton * button = new QPushButton(QString::number(i),this); button->setStyleSheet("QPushButton {color:white; background:blue}"); connect(button, &QPushButton::clicked, this, [this]() { QVBoxLayout* lay = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout()); QPushButton* button = qobject_cast<QPushButton*>(sender()); lay->removeWidget(button); lay->insertWidget(0,button); ui->scrollArea->verticalScrollBar()->setValue(0); }); lay->addWidget(button); }
-
@SGaist I changed the desing: Now QPushButtons are displayed in the QScrollArea, so the Button with the ssid and the other elements (The line to write a password, the button to connect to a network) are shown separately. With these simple changes, the code I wrote before works like a charm! True, It wasn't what I originally intended, but I realized that it's far better due to the circumstances where this app is going to be used.
Thanks for your time and your help!