Change font on QTextCursor before start typing any text
-
Hello,
I'm working on a text editor using QML TextArea and a C++ backend with QTextDocument, QTextCursor, etc.
I’m facing an issue with font formatting behavior when applying font changes via QTextCursor::setCharFormat() or QTextCursor::mergeCharFormat().
Here’s the situation:
When I change the font (e.g., via a combo box) and have no text selected or document is empty, I apply the new font using:void DocumentHandler::setFont(const QFont & font){ QTextCursor cursor = textCursor(); if (!cursor.isNull() && cursor.charFormat().font() == font) return; auto temp = font; if(font.pointSize() > 0) { temp.setPointSize(cursor.charFormat().font().pointSize()); } else if(font.pixelSize() > 0){ temp.setPixelSize(cursor.charFormat().font().pixelSize()); } QTextCharFormat format; format.setFont(temp); mergeFormatOnWordOrSelection(format); emit fontChanged(); }
void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) { QTextCursor cursor = textCursor(); if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor); cursor.mergeCharFormat(format); // cursor.setBlockCharFormat(format); //!! CRASH, need another way... }
However, if I click somewhere in the document (e.g., in an empty line or at the end of the document), the cursor seems to "forget" the previously set font and reverts back to the default font.
I expect that after setting font via setCharFormat(), and clicking inside the document or starting a new paragraph would preserve the last applied character format , just like in most word processors (e.g., Word, Google Docs).
What I’ve tried:
- Using mergeBlockCharFormat() instead of mergeCharFormat()
- Setting the default font for the entire QTextDocument before editing
- Checking whether the cursor is in a "valid" position after clicking
Can I set a QTextCharFormat in QTextCursor that will be supported on the next input?
there is also no such functionality in the official example, the format can only be changed if a word is highlighted or the cursor is on a word.I'm thinking about making a workaround: make pending changes, display them on the UI, and when the user enters the text, immediately apply the pending changes, but I still hope that there is a way to do this natively.
Thank you.
-
Hello,
I'm working on a text editor using QML TextArea and a C++ backend with QTextDocument, QTextCursor, etc.
I’m facing an issue with font formatting behavior when applying font changes via QTextCursor::setCharFormat() or QTextCursor::mergeCharFormat().
Here’s the situation:
When I change the font (e.g., via a combo box) and have no text selected or document is empty, I apply the new font using:void DocumentHandler::setFont(const QFont & font){ QTextCursor cursor = textCursor(); if (!cursor.isNull() && cursor.charFormat().font() == font) return; auto temp = font; if(font.pointSize() > 0) { temp.setPointSize(cursor.charFormat().font().pointSize()); } else if(font.pixelSize() > 0){ temp.setPixelSize(cursor.charFormat().font().pixelSize()); } QTextCharFormat format; format.setFont(temp); mergeFormatOnWordOrSelection(format); emit fontChanged(); }
void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) { QTextCursor cursor = textCursor(); if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor); cursor.mergeCharFormat(format); // cursor.setBlockCharFormat(format); //!! CRASH, need another way... }
However, if I click somewhere in the document (e.g., in an empty line or at the end of the document), the cursor seems to "forget" the previously set font and reverts back to the default font.
I expect that after setting font via setCharFormat(), and clicking inside the document or starting a new paragraph would preserve the last applied character format , just like in most word processors (e.g., Word, Google Docs).
What I’ve tried:
- Using mergeBlockCharFormat() instead of mergeCharFormat()
- Setting the default font for the entire QTextDocument before editing
- Checking whether the cursor is in a "valid" position after clicking
Can I set a QTextCharFormat in QTextCursor that will be supported on the next input?
there is also no such functionality in the official example, the format can only be changed if a word is highlighted or the cursor is on a word.I'm thinking about making a workaround: make pending changes, display them on the UI, and when the user enters the text, immediately apply the pending changes, but I still hope that there is a way to do this natively.
Thank you.
@Deymos said in Change font on QTextCursor before start typing any text:
cursor.mergeCharFormat(format);
Aren't you changing a copy here?
-
@Deymos said in Change font on QTextCursor before start typing any text:
cursor.mergeCharFormat(format);
Aren't you changing a copy here?
@jsulm said in Change font on QTextCursor before start typing any text:
Aren't you changing a copy here?
Yes, but as I understand it, there is no other way to get a QTextCursor other than
QTextCursor DocumentHandler::textCursor() const { QTextDocument *doc = textDocument(); if (!doc) return QTextCursor(); QTextCursor cursor = QTextCursor(doc); return cursor; }