Tabs inconsistent width in class inheriting from QPlainTextEdit
-
I'm working with a class that inherits from QPlainTextEdit.
Text is loaded into this object with the setPlainText function.
Some of the lines of text contain tab characters. When I highlight the tabs, on some lines they are barely a few pixels wide, but on the very next line they can be as wide as two characters.
I'd really like them to be a uniform width (ideally a multiple of the character width, as I've set a monospace font). I think the tabStopWidth isn't what I actually want; I don't want to control where the tab will jump to in even divisions across the page - I want a tab character to take up exactly <this many> characters, every time, no matter where it is.
I tried to set them (in the constructor of the QWidget inheriting class) to a fixed width, like this:
@ ui->setupUi(this);
// editor is the class inheriting from QPlainTextEdit
QFont font;
font.setFamily("Courier");
font.setStyleHint(QFont::Monospace);
font.setFixedPitch(true);
font.setPointSize(10);
ui->editor->setFont(font);int tabStop = 4; // 4 characters QFontMetrics metrics(ui->editor->font()); ui->editor->setTabStopWidth(tabStop * metrics.width(' '));@
but that didn't seem to do anything. None of the tabs look like they're four characters wide.
Edit: And it also isn't monospacing :( I'm sure I must be missing something obvious; setting a monospace font and a constant tab width must be a pretty common demand.
-
I hate to BUMP, but this is causing me no end of trouble, sadly :(
I need to place the cursor at a specified location, and the tabs don't even seem to count as a consistent number of characters wide, so when I see a tab in the text, it's impossible to know how many characters that counts as for using the QTextCursor::setPosition function.
It seems ridiculous that I can't set the number of characters a tab takes up. Is that really the case?
-
Hi,
AFAIK, QPlainTextEdit does not implement what you need, it's optimized for plain text. You should rather be using QTextEdit
-
Okey dokey. I have had a quick look at QTextEdit and to be honest, I don't see a function there that looks like it might do what I want, but I'd need to get into it a bit to be sure. I see setting tab stop width, but that's not the same thing as setting the width of a tab-character.