user3599288
user3599288

Reputation: 68

In QT "ui->scrollArea->horizontalScrollBar()->setValue(0)" has no effect

So in my UI designer I have a ScrollArea Widget, I then in my MainWindow.cpp, I create a QGraphicScene and a QGraphics View. I create a new widget and give that widget a QVBoxLayout so that it will auto-size(which is correct to my understanding).

I then use ui->scrollArea->setWidget(widget); to make this new widget the child of my scrollView.

All of this seems correct because I have scroll bars that I can navigate my scene with. However; using the line ui->scrollArea->horizontalScrollBar()->setValue(0); still has no effect on the scroll bar values.

scene = new QGraphicsScene();
scene->setSceneRect(0,0,2500,2500);
view = new QGraphicsView(scene);

QWidget *widget = new QWidget;
view->setBackgroundBrush(Qt::white);
QVBoxLayout* bLayout = new QVBoxLayout(widget);
ui->scrollArea->setWidget(widget);

bLayout->addWidget(view);

widget->show();

ui->scrollArea->horizontalScrollBar()->setValue(0);
ui->scrollArea->verticalScrollBar()->setValue(0);

Upvotes: 1

Views: 2719

Answers (2)

Evandro Coan
Evandro Coan

Reputation: 9418

I just had the this problem. Then, after debugging with ui->scrollArea->verticalScrollBar()->value() I realized that the scrolling area has no size before the component is show on the screen, i.e., it does not change the scrolling because it is not visible yet.

This is a sample Python code, but the is the same for C++, except the Language Syntax.

from PyQt5.QtWidgets import QDialog
...

    dialog = QDialog()
    ...

    verticalScrollBar = dialog.QPlainTextEdit.verticalScrollBar()
    horizontalScrollBar = dialog.QPlainTextEdit.horizontalScrollBar()

    # Has no effect, if you print the values, you will see always 0
    verticalScrollBar.setValue( horizontalScrollBar.maximum() )
    horizontalScrollBar.setValue( horizontalScrollBar.minimum() )

    dialog.show()

    # Now it has effect
    verticalScrollBar.setValue( horizontalScrollBar.maximum() )
    horizontalScrollBar.setValue( horizontalScrollBar.minimum() )
  1. Autoscroll PyQT QTextWidget

Upvotes: 2

Mikitori
Mikitori

Reputation: 617

If you are sure to address the correct scrollbar (as pointed in the comments by thuga), maybe check if your scroll area is modified after that init. I mean I'm not sure of the bahaviour, but if you modified some text in your widget for example, I think the scrollbar will be impacted.

You may need to catch some of your widget's event to reapply those:

ui->scrollArea->horizontalScrollBar()->setValue(0);
ui->scrollArea->verticalScrollBar()->setValue(0);

If it doesn't help, you should try to debug by tracking scrollbar value with

ui->scrollArea->verticalScrollBar()->value()

and see if your set is well applied, and maybe when it is overriden

Just in case, you may also try some of the methods indicated here, but it's probably not relevant to your issue: setValue of ScrollBar Don't work at first time

Upvotes: 1

Related Questions