PDA

View Full Version : QTextEdit not changing CharFormat.



UriTK
13th April 2019, 00:21
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.

QTextCharFormat outline;
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.

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

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

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.

ChrisW67
13th April 2019, 04:55
This seems to work when inserting text (with or without explicitly setting the format);


#include <QApplication>
#include <QTextEdit>
#include <QTextDocument>
#include <QTextCharFormat>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QTextEdit editor;
editor.resize(640, 480);

QTextCursor cursor(editor.textCursor());
cursor.movePosition(QTextCursor::Start);

QTextCharFormat format(cursor.charFormat());
format.setFontPointSize(48.0);
cursor.setCharFormat(format);

cursor.insertBlock();
cursor.insertText("One", format);
cursor.insertBlock();
cursor.insertText("Two");

QTextCharFormat outlineFormat = format;
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();
}

UriTK
13th April 2019, 05:59
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.

anda_skoa
13th April 2019, 08:31
Given ChrisW67's code, this works for me to add outline to all existing text



QTextCursor allCursor = editor.textCursor();
allCursor.movePosition(QTextCursor::Start);
allCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
allCursor.mergeCharFormat(outlineFormat);


Cheers,
_

UriTK
13th April 2019, 12:31
QTextCursor allCursor = editor.textCursor();
allCursor.movePosition(QTextCursor::Start);
allCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
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!