View Full Version : QScrollArea scrolls to top when hiding QPushButton

13th March 2013, 09:47
I have a custom datetimewidget that is set up like this:

1. When invalid datetime:
- 2 QPushButtons showing that the date and time are invalid.

2. When valid datetime:
- A QDateEdit and a QTimeEdit. These are hidden if invalid datetime, and shown only if valid datetime

This datetimewidget is placed within a QGroupBox and then on a QScrollArea.

If starting out with an invalid datetime, clicking one of the buttons give you a valid datetime, hides the buttons and shows the controls. This works fine, except that the QScrollArea scrolls to top. Does anyone have an idea on where to look for what initiates this?

I've found some hints, but are still stuck on this:

If I drop the QPushButton.hide() on the button that is hit, I don't get the unwanted scroll-behaviour. However, I really want to hide this button...
If I drop the blockSignals true/false around the setting of current datetime to the QDateEdit and QTimeEdit, I don't get this problem. However, the blockSignals are needed to avoid signals of edited datetime when it's actually not edited.

Thanks alot!

18th March 2013, 14:05
Answering myself here:

This issue was actually caused by the fact that hiding a button gives focus to the next widget in the layout. In this case, the datetimewidget was placed at the bottom, and the next widget in the layout was the widget at the very top of the scroll area.

This was solved by
setFocusPolicy(Qt::StrongFocus); //on the datetimewidget
setFocusPolicy(Qt::NoFocus); //on the QPushButtons showing invalid date and time