QTextEdit loses rich text formatting after pressing enter
-
Can someone please help me. I have setup a simple QTextEdit and it accepts rich text. And whenever I press enter after the text that has a certain formatting, the cursor goes to the next line as expected. But when I press enter again, the cursor doesn't move. And when I press enter again, the cursor moves down but when it does so, it loses the text formatting that was applied. You can see it decrease in size and when I type new text it doesn't have any formatting.
How can I stop this behaviour? I was it to retain the previous formatting no matter how many empty lines I put by pressing enter.The code for the setup of the QTextEdit is like this:
#include <QTextEdit> Editor2::Editor2(QWidget *parent) : QTextEdit(parent) { setAcceptRichText(true); showMaximized(); setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:'Comic Relief'; font-size:15px; font-weight:600; font-style:normal;\">\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p></body></html>"); } -
@tapsbrighton
Well, at least that explains why it shows as it does, right? But not why that happened, or whether you can do anything about it.@JonB I reimplemented the keyPressEvent and it seems to be working.
void Editor2::keyPressEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Return){ qInfo()<<"Enter key pressed"; append(""); }else{ QTextEdit::keyPressEvent(e); } } -
Can someone please help me. I have setup a simple QTextEdit and it accepts rich text. And whenever I press enter after the text that has a certain formatting, the cursor goes to the next line as expected. But when I press enter again, the cursor doesn't move. And when I press enter again, the cursor moves down but when it does so, it loses the text formatting that was applied. You can see it decrease in size and when I type new text it doesn't have any formatting.
How can I stop this behaviour? I was it to retain the previous formatting no matter how many empty lines I put by pressing enter.The code for the setup of the QTextEdit is like this:
#include <QTextEdit> Editor2::Editor2(QWidget *parent) : QTextEdit(parent) { setAcceptRichText(true); showMaximized(); setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:'Comic Relief'; font-size:15px; font-weight:600; font-style:normal;\">\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" font-family:'Sanlulus'; font-size:22pt; font-weight:400; color:#00ffff; background-color:#aa0000;\">Will you shine and light the brightest</span></p></body></html>"); }@tapsbrighton
Not sure what your <Enters>/newlines are doing. You show the HTML you initially put into theQTextEdit. It would be interesting to see what that HTML ends up as when you do as you say and it goes wrong. -
@tapsbrighton
Not sure what your <Enters>/newlines are doing. You show the HTML you initially put into theQTextEdit. It would be interesting to see what that HTML ends up as when you do as you say and it goes wrong.@JonB The first line 'After pressing enter once' has the formatting as the text above it. However the last one, 'After pressing enter twice' has lost all formatting
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css"> p, li { white-space: pre-wrap; } hr { height: 1px; border-width: 0; } </style></head><body style=" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">After pressing enter once</span></p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sanlulus'; font-size:22pt; color:#00ffff;"><br /></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">After pressing enter twice</p></body></html> -
@JonB The first line 'After pressing enter once' has the formatting as the text above it. However the last one, 'After pressing enter twice' has lost all formatting
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css"> p, li { white-space: pre-wrap; } hr { height: 1px; border-width: 0; } </style></head><body style=" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">Will you shine and light the brightest</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Sanlulus'; font-size:22pt; color:#00ffff; background-color:#aa0000;">After pressing enter once</span></p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sanlulus'; font-size:22pt; color:#00ffff;"><br /></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">After pressing enter twice</p></body></html>@tapsbrighton
Well, at least that explains why it shows as it does, right? But not why that happened, or whether you can do anything about it. -
@tapsbrighton
Well, at least that explains why it shows as it does, right? But not why that happened, or whether you can do anything about it.@JonB I reimplemented the keyPressEvent and it seems to be working.
void Editor2::keyPressEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Return){ qInfo()<<"Enter key pressed"; append(""); }else{ QTextEdit::keyPressEvent(e); } } -
@JonB I reimplemented the keyPressEvent and it seems to be working.
void Editor2::keyPressEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Return){ qInfo()<<"Enter key pressed"; append(""); }else{ QTextEdit::keyPressEvent(e); } }@tapsbrighton
Well that simply discards the Return (why bother withappend("")?). Which is OK if you are OK with that! -
@tapsbrighton
Well that simply discards the Return (why bother withappend("")?). Which is OK if you are OK with that!@JonB It adds the new line that was supposed to be added and keeps the current text formatting
-
@JonB It adds the new line that was supposed to be added and keeps the current text formatting
@tapsbrighton This is a better solution rather than append:
insertPlainText("\n");