Results 1 to 5 of 5

Thread: QTextEdit not changing CharFormat.

  1. #1
    Join Date
    Apr 2019
    Posts
    8
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Question 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.
    Qt Code:
    1. outline.setTextOutline(QPen(Qt::black, 2));
    2. QTextEdit->mergeCurrentCharFormat(outline);
    To copy to clipboard, switch view to plain text mode 


    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.
    Qt Code:
    1. QTextEdit->setStyleSheet("text-shadow: 0px 2px #000000, 0px -2px #000000, -2px 0px #000000, 2px 0px #000000")
    To copy to clipboard, switch view to plain text mode 
    Even though I've both tried to do this standalone and tried adding it on top of other stylesheet changes that were functioning via
    Qt Code:
    1. style.append("; text-shadow: 0px 2px #000000, 0px -2px #000000, -2px 0px #000000, 2px 0px #000000;");
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. QTextEdit->textCursor().insertText(qstring, qtextcharformat);
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by UriTK; 13th April 2019 at 00:23. Reason: reformatted to look better

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,528
    Thanks
    13
    Thanked 1,575 Times in 1,504 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextEdit not changing CharFormat.

    This seems to work when inserting text (with or without explicitly setting the format);
    Qt Code:
    1. #include <QApplication>
    2. #include <QTextEdit>
    3. #include <QTextDocument>
    4. #include <QTextCharFormat>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. QTextEdit editor;
    11. editor.resize(640, 480);
    12.  
    13. QTextCursor cursor(editor.textCursor());
    14. cursor.movePosition(QTextCursor::Start);
    15.  
    16. QTextCharFormat format(cursor.charFormat());
    17. format.setFontPointSize(48.0);
    18. cursor.setCharFormat(format);
    19.  
    20. cursor.insertBlock();
    21. cursor.insertText("One", format);
    22. cursor.insertBlock();
    23. cursor.insertText("Two");
    24.  
    25. QTextCharFormat outlineFormat = format;
    26. outlineFormat.setTextOutline(QPen(Qt::red, 1));
    27. cursor.setCharFormat(outlineFormat);
    28.  
    29. cursor.insertBlock();
    30. cursor.insertText("Three", outlineFormat);
    31. cursor.insertBlock();
    32. cursor.insertText("Four");
    33.  
    34. editor.show();
    35.  
    36.  
    37. return app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    UriTK (13th April 2019)

  4. #3
    Join Date
    Apr 2019
    Posts
    8
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit not changing CharFormat.

    Quote Originally Posted by ChrisW67 View Post
    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.
    Last edited by UriTK; 13th April 2019 at 06:00. Reason: spelling corrections

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit not changing CharFormat.

    Given ChrisW67's code, this works for me to add outline to all existing text

    Qt Code:
    1. QTextCursor allCursor = editor.textCursor();
    2. allCursor.movePosition(QTextCursor::Start);
    3. allCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    4. allCursor.mergeCharFormat(outlineFormat);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    UriTK (13th April 2019)

  7. #5
    Join Date
    Apr 2019
    Posts
    8
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit not changing CharFormat.

    Quote Originally Posted by anda_skoa View Post
    Qt Code:
    1. QTextCursor allCursor = editor.textCursor();
    2. allCursor.movePosition(QTextCursor::Start);
    3. allCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    4. allCursor.mergeCharFormat(outlineFormat);
    To copy to clipboard, switch view to plain text mode 
    _
    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!

Similar Threads

  1. CharFormat how set backgroud color ??
    By Pablik in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2012, 07:15
  2. How to get CharFormat from each char
    By Pablik in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2012, 22:07
  3. changing a QTextEdit from a dll
    By hcetiner in forum Newbie
    Replies: 1
    Last Post: 23rd April 2010, 17:38
  4. Changing line height in QTextEdit (Qt4)
    By ultr in forum Qt Programming
    Replies: 0
    Last Post: 19th February 2010, 15:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.