Results 1 to 9 of 9

Thread: Styling a QTextEdit

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Styling a QTextEdit

    Hello everyone,

    i'm not new to Qt, however i'm very new to applying stylesheets to widgets. That's why i'm posting in this topic. If you feel that i should be posting in the "Programming" section, tell me and i'll delete this post and create it later.

    So basically, i have a QTextEdit widget on a dialog, and i'm trying to apply a stylesheet on it. My stylesheet is in a resource file, and contains this:
    Qt Code:
    1. * {
    2. margin: 0;
    3. padding: 0;
    4. color: Black;
    5. white-space: normal;
    6. font-family: Arial;
    7. font-size: 10px;
    8. font-weight: normal;
    9. }
    10. h1 {
    11. color: Red;
    12. font-size: 11px;
    13. }
    14. h2 {
    15. color: Red;
    16. font-size: 10px;
    17. }
    18. h3 {
    19. color: Red;
    20. font-size: 10px;
    21. }
    To copy to clipboard, switch view to plain text mode 

    Then on my dialog constructor, i do:
    Qt Code:
    1. // Set up stylesheet on slip summary widget.
    2. QFile fStyle(":/Resources/Stylesheets/SlipSummary.css");
    3. fStyle.open (QIODevice::ReadOnly|QIODevice::Text);
    4. _ui.txtSlipSummary->setStyleSheet(fStyle.readAll());
    5. fStyle.close();
    To copy to clipboard, switch view to plain text mode 

    My main problem is that no titles are being shown in red. If i put the "color: red" on the first "*" selector, everything turns red, but no luck with the individual selectors... What can be the problem?

    Related to styling widgets, i have a question. Lets say a QTextEdit base contents is set to:
    Qt Code:
    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    2. <html>
    3. <head>
    4. <meta name="qrichtext" content="1" />
    5. </head>
    6. <body>
    7. <h1>Test</h1>
    8. <h2>Foo</h2>
    9. <p>Bar</p>
    10. </body>
    11. </html>
    To copy to clipboard, switch view to plain text mode 
    Then would it be possible to set a stylesheet which would be in a QRC file like this within the <head /> tag:
    Qt Code:
    1. <LINK href=":/Resources/CSS/Main.css" rel="stylesheet" type="text/css">
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot for your help,
    Pierre.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Styling a QTextEdit

    Hi,

    with _ui.txtSlipSummary->setStyleSheet(fStyle.readAll()); you are setting a style sheet for a widget not for the HTML page, what is what you want. You have to set the css file to file in the editor. For that see my old wiki post that you can get a clue how to do that: [WIKI]QTextBrowser with images and CSS[/WIKI]

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

    hickscorp (30th March 2012)

  4. #3
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: Styling a QTextEdit

    Dear Lykurg,

    Thank you very much, i'll have a look at this method in a few hours. It seems to be exactly what i'm looking for!

    Pierre.

  5. #4
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Styling a QTextEdit

    Hello,

    i have been playing around with QHtmlDocument with no luck regarding Lykurg's reply. i am still able to change some of the CSS properties, but not all of them work (Colors are fine now, but list-style and float / positions aren't taken into account). The code i am now using to style up my text is as follows:
    Qt Code:
    1. // Prepare HTML document.
    2. _slipSummaryDoc = new QTextDocument();
    3. // Set up stylesheet on slip summary widget.
    4. QFile fStyle (":/Resources/Stylesheets/SlipSummary.css");
    5. fStyle.open(QIODevice::ReadOnly|QIODevice::Text);
    6. QString css = fStyle.readAll();
    7. fStyle.close();
    8. _slipSummaryDoc->addResource(QTextDocument::StyleSheetResource, QUrl("SlipSummary.css"), css);
    9. // Set document.
    10. _ui.txtSlipSummary->setDocument(_slipSummaryDoc);
    11. _slipSummaryDoc->setHtml(QString("<html><head><link rel='stylesheet' type='text/css' href='SlipSummary.css'></head><body>%1</body></html>").arg(_documents->htmlSummary()));
    To copy to clipboard, switch view to plain text mode 

    The CSS style looks like this (Although it's really becoming a sandbox that has changed upon each test i've made):
    Qt Code:
    1. * {
    2. color: #222;
    3. font-size: 10px;
    4. font-weight: normal;
    5. margin: 0;
    6. padding: 0;
    7. list-style: none;
    8. text-indent: 0;
    9. }
    10. h1 {
    11. color: #555;
    12. font-size: 12px;
    13. font-weight: bold;
    14. }
    15. h2 {
    16. color: #444;
    17. font-size: 11px;
    18. font-weight: bold;
    19. }
    20. h3 {
    21. color: #333;
    22. font-size: 10px;
    23. font-weight: bold;
    24. }
    25. ul, li {
    26. list-style: none;
    27. margin: 0;
    28. padding: 0;
    29. text-indent: 0;
    30. }
    31. span.amount {
    32. float: right;
    33. width: 60px;
    34. margin: 0;
    35. padding: 0;
    36. text-align: right;
    37. }
    To copy to clipboard, switch view to plain text mode 

    [edit]Oh and here is the generated HTML code being inserted:
    Qt Code:
    1. <h2>4 Invoices<span class='amount'>$30.42</span></h2>
    2. <h3>2 Utility<span class='amount'>$0.00</span></h3>
    3. <ul>
    4. <li>2 Energy<span class='amount'>$0.00</span></li>
    5. </ul>
    6. <h3>2 Car<span class='amount'>$30.42</span></h3>
    7. <ul>
    8. <li>1 Gas<span class='amount'>$8.50</span></li>
    9. <li>1 Repairs<span class='amount'>$8.10</span></li>
    10. </ul>
    To copy to clipboard, switch view to plain text mode 
    [/edit]

    Again, thank you for any kind help you may provide!
    Pierre.
    Last edited by hickscorp; 3rd April 2012 at 16:38.

  6. #5
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Styling a QTextEdit

    Bump, is there anyone able to change list-styles, padding, margins, and float positioning in a QTextDocument via stylesheet?

  7. #6
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Styling a QTextEdit

    You can do this two ways

    1. Register your stylesheet as a resource, then in your html add
    Qt Code:
    1. <link rel='stylesheet' type='text/css' href='qrc:/Resources/Stylesheets/SlipSummary.css' />
    To copy to clipboard, switch view to plain text mode 

    OR

    2. Read your stylesheet manually into a QString and if you are using a QTextBrowser you can call
    Qt Code:
    1. mTextBrowser->setDefaultStyleSheet(myStylesheetContentString);
    To copy to clipboard, switch view to plain text mode 

    You mentioned that your styles are actually just not applying properly (i.e. the global style is working but nothing else) there may be a bug in your actual style sheet not the loading/applying of it.

    QTextDocument does not support all styles have a look at http://doc.qt.nokia.com/4.7-snapshot...ml-subset.html

  8. #7
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Styling a QTextEdit

    Dear Berryblue031,
    Thank you for answering me!

    i tried it both ways. None of them actually worked for the properties i'm trying to apply changes on. My CSS validates and looks good.

    Could you please try to make a simple HTML document containing a <ul><li>Foo</li></ul> and a <span>Bar<>, put it in a QDocument, and tell me if you're able to change the padding, the margins, make the bar span float right, and remove list bullets?

    Thanks a lot for your help,
    Pierre.

  9. #8
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Styling a QTextEdit

    If you can see just one of the styles in your stylesheet (i.e. the text turning all red) then the stylesheet is being applied correctly.

    Your problem is the majority of your styles are not compatible with QTextEdit - It only supports a subset of html / css

    If you look at the link I posted earlier ( http://doc.qt.nokia.com/4.7-snapshot...ml-subset.html )
    • float attribute is only supported by images and tables - you cannot apply it to a span
    • list-style attribute is not supported at all - if you really want a list without any bullets make it manually with a table
    • margin and padding are only supported on block tags i.e. span/p/div - so put whatever content you want to apply padding/margins too into a span

  10. The following user says thank you to Berryblue031 for this useful post:

    hickscorp (24th April 2012)

  11. #9
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Styling a QTextEdit

    Dear BerryBlue,

    Thank you for your kind help!
    i will use the table technique, as styling floating isn't supported for the tags i'm applying on.

    Best to you!
    Pierre.

Similar Threads

  1. Styling SeekSlider
    By alexandernst in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2011, 21:52
  2. Styling QTabBar?
    By arbi in forum Qt Programming
    Replies: 0
    Last Post: 18th January 2011, 13:46
  3. Styling a tab bar without frame
    By longh in forum Qt Programming
    Replies: 2
    Last Post: 15th August 2008, 16:02
  4. qscrollbar styling
    By 20khz in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2008, 14:17
  5. Styling a QComboBox
    By Enygma in forum Qt Programming
    Replies: 6
    Last Post: 23rd August 2007, 10:22

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.