QScrollArea with a QLabel isn't auto scrolling
-
It's a bit long so please bear with me.
I have a
QLabel
with text that's wider than the window size.
TheQLabel
is inside a horizontalQScrollArea
:scrollArea->setWidget(label); scrollArea->setFixedSize(WINDOW_WIDTH , BUTTON_HEIGHT);
I navigate through letters in the
QLabel
by the keyboard arrow keys, setting selection on each letter byQLabel::setSelection()
.
For example say I have the text abcdefghijklmno|pqrstuvwxyz . The pipe (|) indicates the end of the window - it's not really there. So currently only the letters {a...o} are visible. I start by setting selection on the letter a. Then I press the right arrow key and set selection on the letter b, and so on...
Say I reached letter o :
abcdefghijklmno**|pqrstuvwxyz**
I expect the scrollArea to scroll right on the next click. But it doesn't. Namely when the letter p is selected, I need the scrollArea to scroll to make it visible.I tried
ensureVisible()
but it's not working for me - the scrollArea won't scroll whatever I passed toensureVisible()
. For example I triedensureVisible(WINDOW_SIZE+20 , 0)
orensureVisible(20 , 0)
and neither of them worked. I guessensureVisible()
is not what I need.Of course I can count letters and when I reach say the 14th letter, I scroll it. But that's not good because say I scroll to letter 20, then go back to 13 - the next right click shouldn't scroll, but it would.
Anyway, what I'm currently doing is keeping a counter for the font width up until the selected letter. I update it on every right/left click (right click: increment counter by width , left click: decrement counter by width) - and using that I decide when to scroll. It's good but it's not perfect.
Any other ideas? Preferably something neat and automatic, not a workaround.Thanks!