Results 1 to 6 of 6

Thread: setting text also changes color?

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default setting text also changes color?

    Here is a small prog that just sets a color and some text in a QTextEdit.
    (QT 4.2 on X11)

    Qt Code:
    1. #include <QApplication>
    2. #include <QTextEdit>
    3.  
    4. int main( int argc, char **argv )
    5. {
    6. QApplication app( argc, argv );
    7.  
    8. qDebug("init color: %08x", t.textColor().rgb());
    9.  
    10. t.setTextColor(0xdd9933);
    11. qDebug("after set color: %08x", t.textColor().rgb());
    12.  
    13. t.setPlainText("Some text");
    14. qDebug("after set text: %08x", t.textColor().rgb());
    15.  
    16. t.show();
    17. app.exec();
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    The console output is:
    Qt Code:
    1. init color: ff000000
    2. after set color: ffdd9933
    3. after set text: ff000000
    To copy to clipboard, switch view to plain text mode 

    Apparently, setting the text has the side-effect of resetting the current color. Looks like a bug to me. Anyone who can interpret it as a "feature"?
    Last edited by drhex; 18th November 2006 at 10:54. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting text also changes color?

    This is probably happening because you are using setPlainText().

    Try using

    textCursor()->insertText();

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

    drhex (18th November 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: setting text also changes color?

    textCursor()->insertText() works. (or I could have set the color again after setting the text)
    but why does setPlainText() change the color?

  5. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting text also changes color?

    From the docs


    Changes the text of the text edit to the string text. Any previous text is removed.
    text is interpreted as plain text.
    Plain text means black as text color and probably "Arial" as font. This might even be OS specific.

  6. #5
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: setting text also changes color?

    The text set with setPlainText() was rendered in the color set just previously with setTextColor(), not black. I'll ask the Trolls.
    Last edited by drhex; 18th November 2006 at 17:45. Reason: spelling error

  7. #6
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: setting text also changes color?

    The Trolls says it will be fixed in QT 4.3

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 2
    Last Post: 23rd July 2012, 08:42
  3. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 07:25
  4. Replies: 1
    Last Post: 17th March 2006, 08:19
  5. Change the text color tab page
    By gtthang in forum Qt Programming
    Replies: 4
    Last Post: 18th February 2006, 17:38

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.