user2789451
user2789451

Reputation:

Horizontal Scroll QTextEdit to specific column number

I have a QTextEdit and given a column number, say x, I want to be around to scroll horizontally (by calling QTextEdit.horizontalScrollBar().setValue() to a value such that column x is the first visible column in the QTextEdit. Is there any way to do that?

From what I seen, QTextEdit.horizontalScrollBar().setValue() takes a value representing the pixel, not the column number. So simply setting the scroll value to x doesn't work.

Upvotes: 1

Views: 443

Answers (1)

J Webster
J Webster

Reputation: 219

So what you're suggesting only works for fixed width fonts I think. So, assuming that's what you have, then you can get the width of the font using QFontMetrics, then just multiply the font width by the column number that you want.

from PyQt5.QtGui import QFont, QFontMetrics
myfont = QFont("courier", 24)
fm = QFontMetrics(myfont)
f_width = fm.width("A")
QTextEdit.horizontalScrollBar().setValue(f_width * x)

Upvotes: 1

Related Questions