Results 1 to 8 of 8

Thread: QString text width without <qt><b> and other special params

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation QString text width without <qt><b> and other special params

    Hi,
    Is it possible to have the text width without the special params as <qt>, <b> and others ?
    I wrote an Elided QLabel custom class but then the width is not correct, and also I have this case in other places.
    For the simple way I used the remove function of QString to remove <qt>, </qt>, <b> and </b> but there is a way to remove all ? I surely don't know all so I can miss one too.
    BUT also italic and bold change the width of the text, so it has to be parsed, but maybe one function already do that in Qt ?
    Thanks for the help
    Last edited by Alundra; 27th July 2016 at 14:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString text width without <qt><b> and other special params

    If you want to remove all rich text markup you could try this

    Qt Code:
    1. doc.setHtml(yourString);
    2. QString stringWithoutMarkup = doc.toPlainText();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QString text width without <qt><b> and other special params

    Does QFontMetrics handle rich text strings? It doesn't really say so in the docs, but it might be worth checking out.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString text width without <qt><b> and other special params

    Thanks for the QTextDocument trick but if I set my text "<something>" it's removed, I would only remove special character, is it not possible ?
    The goal is for that :
    Qt Code:
    1. const QString elidedText = fontMetrics().elidedText(textWithoutSpecialParams, Qt::ElideMiddle, width, Qt::TextShowMnemonic);
    To copy to clipboard, switch view to plain text mode 
    What I know is <qt>, <b>, <i>, <u>, <font color="">
    Maybe the only solution is to do a while loop and remove each part of the string manually
    Last edited by Alundra; 27th July 2016 at 17:43.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString text width without <qt><b> and other special params

    I thought you wanted the RichText markup removed.

    If you only want to remove certain tags then that will be up to you.

    Cheers,
    _

  6. #6
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString text width without <qt><b> and other special params

    It's exactly that but not "<...>" which is not a known markup like "<not loaded>"

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString text width without <qt><b> and other special params

    Hmm, <not loaded> isn't a valid tag, that would have had to be escaped before anyway, no?

    I.e. the actual input string would be something like &lt;not loaded&gt;, right?

    Does the setHtml/toPlainText() also remove these?

    Cheers,
    _

  8. #8
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString text width without <qt><b> and other special params

    Using toPlainText(), "<not loaded>" is removed but "&lt;not loaded&gt;" is correcty shown.
    Ah yea but then the bold is not shown in the elided text, the elided QLabel doesn't look to be an easy thing to do because it has to not elide params.
    Last edited by Alundra; 28th July 2016 at 19:59.

Similar Threads

  1. adding special symbol unicode to qstring
    By wagmare in forum Newbie
    Replies: 2
    Last Post: 1st June 2016, 06:03
  2. QString Special Character Problem
    By hakiim35 in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2011, 14:38
  3. Text width greater than the QRect
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2011, 08:27
  4. Fastest way to get width of rich text
    By jgrauman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2010, 22:07
  5. maximum text width of interval
    By osiris81 in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2010, 14:37

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.