Can't set formatting on the QTextEdit
-
Hi, ALL,
I have Qt 5.7.1 on Gentoo Linux and I have a following weird problem.I have QTextEdit control and trying to set different styling for it.
When I try to set the styling somewhere in the middle of the text, everything works.
When I try to set the styling at the end of the text - it doesn't.I tried both setCharFormat() and mergeCharFormat() call with the simplest text and background color change.
Does anybody knows how do I change the styling at the end of the text? It should be picked up by inserting the text programmatically .
Thank you in advance!
-
@oneeyeman1 Hi,
What do you mean by "end of the text" ? Last word, or line ?
Can you provide the code showing how you are applying your styling ? -
@oneeyeman1
Do you mean you try to leave the text with some style at the end on "nothing" and then later just append text, expecting it to pick up that styling? At a guess, you need to apply the styling at the instant the new text is appended, so it has some content to apply to? -
Hi,
@Gojir4 said in Can't set formatting on the QTextEdit:@oneeyeman1 Hi,
What do you mean by "end of the text" ? Last word, or line ?
Can you provide the code showing how you are applying your styling ?Consider this:
- I have a QTextEdit.
- This control contains a phrase "A quick brown fox jumps over the lazy dog."
- The control itself is editable and the (text) cursor is blinking at the end of the phrase (after the "." symbol) waiting for input.
At this point I can successfully set the coloring to any words in the sentence.
However, when I try to set the formatting to where my cursor is - so that the next text I add will be in that color scheme - I can't. The text is still be added as black on white.So the meaning of it is - when my cursor positioned after the last character I typed.
-
Hi
What about
https://doc.qt.io/qt-5/qplaintextedit.html#setCurrentCharFormat
so it uses your format as the default one when inserting text ? -
Hi,
@JonB said in Can't set formatting on the QTextEdit:@oneeyeman1
Do you mean you try to leave the text with some style at the end on "nothing" and then later just append text, expecting it to pick up that styling? At a guess, you need to apply the styling at the instant the new text is appended, so it has some content to apply to?Well, yes - I'm trying to apply the formatting on "nothing" for the text I will add in the future.
And yes - the text is appended immediately after I set the formatting.But I don't understand what you mean by saying "so it has some content to apply to?" I can append the text at any time I want and I WILL expect for the formatting to be applied.
Thank you.
-
@oneeyeman1
So I'm purely guessing. If I understand right, you'd like to set the font on some text which isn't yet there, at the end of the line. So there's nothing to set the font on right now. Maybe that doesn't work, maybe the font change only takes/persists if there is some text to put it on. Maybe you have to save up what font you want and apply it when some text later arrives.Like I said I'm purely guessing. Take what I say with a pinch of salt.
-
Hi,
@mrjj said in Can't set formatting on the QTextEdit:Hi
What about
https://doc.qt.io/qt-5/qplaintextedit.html#setCurrentCharFormat
so it uses your format as the default one when inserting text ?I will try that tonight.
I presume it is available thru the QTextEdit as well?Thank you.
-
@oneeyeman1
Hi
Yes it is.
https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormatIt sounds like you want to set the format for new text inserted and that would do it.
-
Hi,
@mrjj said in Can't set formatting on the QTextEdit:@oneeyeman1
Hi
Yes it is.
https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormatIt sounds like you want to set the format for new text inserted and that would do it.
Thank you.
I will try that tonight and let you know. -
Hi,
@mrjj said in Can't set formatting on the QTextEdit:@oneeyeman1
Hi
Yes it is.
https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormatIt sounds like you want to set the format for new text inserted and that would do it.
I just tried to call m_edit->setCurrentCharFormat();
Nothing happened.I have a Qt 5.7.1 on Gentoo Linux and I'm trying to mix up formatting inside the text and at the end of the text.
Inside the text everything works. Trying to change it at the end does not.Maybe it was a bug in that specific version of Qt and it was later fixed?
Or maybe that call is conditional and it checks something that needs to be set?
Any idea of what I might try next?
Thank you.
-
Hi,
@oneeyeman1 said in Can't set formatting on the QTextEdit:Hi,
@mrjj said in Can't set formatting on the QTextEdit:@oneeyeman1
Hi
Yes it is.
https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormatIt sounds like you want to set the format for new text inserted and that would do it.
I just tried to call m_edit->setCurrentCharFormat();
Nothing happened.I have a Qt 5.7.1 on Gentoo Linux and I'm trying to mix up formatting inside the text and at the end of the text.
Inside the text everything works. Trying to change it at the end does not.Maybe it was a bug in that specific version of Qt and it was later fixed?
Or maybe that call is conditional and it checks something that needs to be set?
Any idea of what I might try next?
Thank you.
I think its an issue with the version I have (5.7.1).
I just took a look at https://github.com/qt/qtbase/blob/5.7/src/gui/text/qtextcursor.cpp.
Lines 1016-1019 reads:
if (d->position == d->anchor) { d->currentCharFormat = d->priv->formatCollection()->indexForFormat(format); return; }
so when the current position is an anchor, retrieve the format and return. No setting will occur.
I don't know how to workaround this though...
That is unless I'm reading the code correctly.
Anyone have an idea?
TIA!
-
Did anybody get this figured out?
BTW, I'm having the same problem with Qt 5.13.1, here is my related post: https://forum.qt.io/topic/107173/how-to-insert-italic-text-to-qplaintextedit
I can't get any new text that is inserted to contain a specific format.