QTextEdit not changing CharFormat.
So, I'm trying to add an outline into a QTextEdit via CharFormat. For some reason, I can't seem to get the format to actually apply.
Let me go through some things I've tried.
This doesn't change current nor future text, even when I do set it to select everything, or try different cursor positions.
Code:
outline.
setTextOutline(QPen(Qt
::black,
2));
QTextEdit->mergeCurrentCharFormat(outline);
This is about the point where I started getting desperate, so I tried to instead simulate an outline via setting text-shadow on the stylesheet.
Code:
QTextEdit->setStyleSheet("text-shadow: 0px 2px #000000, 0px -2px #000000, -2px 0px #000000, 2px 0px #000000")
Even though I've both tried to do this standalone and tried adding it on top of other stylesheet changes that were functioning via
Code:
style.append("; text-shadow: 0px 2px #000000, 0px -2px #000000, -2px 0px #000000, 2px 0px #000000;");
it does not actually do anything either.
The only way I've been to apply a charformat to a QTextEdit on my code was doing it alongside an insertText operation, which was
Code:
QTextEdit->textCursor().insertText(qstring, qtextcharformat);
but I'd rather not rework this code to use insertText on this QTextEdit I want to change just so I can set an outline.
Re: QTextEdit not changing CharFormat.
This seems to work when inserting text (with or without explicitly setting the format);
Code:
#include <QApplication>
#include <QTextEdit>
#include <QTextDocument>
#include <QTextCharFormat>
int main(int argc, char **argv)
{
editor.resize(640, 480);
format.setFontPointSize(48.0);
cursor.setCharFormat(format);
cursor.insertBlock();
cursor.insertText("One", format);
cursor.insertBlock();
cursor.insertText("Two");
outlineFormat.
setTextOutline(QPen(Qt
::red,
1));
cursor.setCharFormat(outlineFormat);
cursor.insertBlock();
cursor.insertText("Three", outlineFormat);
cursor.insertBlock();
cursor.insertText("Four");
editor.show();
return app.exec();
}
Re: QTextEdit not changing CharFormat.
Quote:
Originally Posted by
ChrisW67
This seems to work when inserting text (with or without explicitly setting the format);
Thank you! But, unfortunately, I've already tried pretty much that exact thing in the past, and I tried it again just now, but still no result.
The cursor is recognized just fine, the format is actually applied and displays an outline when I explicitly set it on insertText on my QTextEdit that actually uses insertText (as supposed to the QTextEdit that I'm trying to change the format of, which uses insertHtml), but none of my textedits end up with the format I tell them to stay with outside of that.
The only thing I can think of that I'm doing differently, it shouldn't *need* to return anything, right? I've just got it on the same void I use to determine it if should set it's font to X, but not under any if statements, just in case. And also, the function is called everytime text needs to be displayed, which *shouldn't* mess with anything. I'd think.
Re: QTextEdit not changing CharFormat.
Given ChrisW67's code, this works for me to add outline to all existing text
Code:
allCursor.mergeCharFormat(outlineFormat);
Cheers,
_
Re: QTextEdit not changing CharFormat.
Quote:
Originally Posted by
anda_skoa
Code:
allCursor.mergeCharFormat(outlineFormat);
_
Even though I already tried this before, this ended up working since doing it a second time led me to notice what was interfiering with it and where. Tl;dr I'm working with big arcane undocumented code and you lads saved me by pure chance. Cheers!