Results 1 to 5 of 5

Thread: Getting superscript of arbitrary number to use in a QString

  1. #1
    Join Date
    May 2013
    Posts
    13
    Thanks
    4

    Default Getting superscript of arbitrary number to use in a QString

    Hello,

    I have some QGraphicsItems onto which text is drawn from a QString, as well as some buttons whose title again is set from a QString. I 'd like that to include superscripts. Hard-coding it like this

    Qt Code:
    1. QString text("σ²");
    To copy to clipboard, switch view to plain text mode 

    works, but I 'd like to superscript an arbitrary number. Ie say there was such a function Qsup then ideally I 'd do something like this:

    Qt Code:
    1. QString text("σ" + Qsup(x));
    To copy to clipboard, switch view to plain text mode 

    What would be the best approach? Is it possible to use one of qt formats that employ html tags and convert that to a QString? If so a short example would be appreciated.

    As an aside, it 'd be ideal to load the text data from a file. But I have trouble loading superscript characters from a plain *.txt file (which is what I 'm using now), no matter what encoding I choose. Any hints about that would also be great.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting superscript of arbitrary number to use in a QString

    Not every digit contains a superscript version in Unicode so in a general case you can't do it like that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting superscript of arbitrary number to use in a QString

    Unicode includes range of code points that should render as superscripted or subscripted 0-9 glyphs. Font support for these is patchy AFAICT. You should be able to trivially write a function to map normal Unicode digits 0-9 (U+0030-39) to their super- (U+2070-79) or subscripted (U+2080-89) equivalent.

    This is not in any way the same thing as using HTML <sup> or <sub> markup, which causes normal glyphs to be rendered above or below the current baseline in HTML renderers. HTML renderers do not try to convert "&sigma;<sup>7</sup>" into the two Unicode characters U+03C3 and U+2077.


    You need to know what encoding you are using your text file because generally you cannot infer it. It might be UTF-8, it might be ISO8859-1 or Windows 1252, it might be UTF16 (big or little endian)...

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

    Durkin (13th May 2013)

  5. #4
    Join Date
    May 2013
    Posts
    13
    Thanks
    4

    Default Re: Getting superscript of arbitrary number to use in a QString

    Thanks. I wrote a small function myself then, that returns a QString with the superscripts 0-9. Though this is in the code -does qt have a way of converting any of the representations found here to the corresponding character?
    -> http://www.fileformat.info/info/unic...2075/index.htm

    Anyhow, Arial seems to render them all fine for now.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting superscript of arbitrary number to use in a QString

    Yes. Qt can convert from several of those encodings...
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. // UTF-8
    8. const char s[] = {
    9. 0x78, // Lower case X
    10. 0xE2, 0x81, 0xB0, // superscript zero
    11. 0xC2, 0xB9, // superscript one
    12. 0xC2, 0xB2, // superscript two
    13. 0xE2, 0x82, 0x83, // subscript three
    14. 0xE2, 0x82, 0x84, // subscript four
    15. 0x00 // NUL terminator
    16. };
    17. QString str = QString::fromUtf8(s);
    18.  
    19. // UTF-16, BOM then same characters as above
    20. const ushort s16[] = {
    21. 0xFEFF, // byte order mark
    22. 0x0078,
    23. 0x2070,
    24. 0x00B9,
    25. 0x00B2,
    26. 0x2083,
    27. 0x2084,
    28. 0x0
    29. };
    30. QString str16 = QString::fromUtf16(s16);
    31.  
    32. // str == str16 is true
    33.  
    34. QFont font("Arial", 36);
    35. QLabel l(str16);
    36. l.setFont(font);
    37. l.show();
    38.  
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    You will probably see that the rendering of the superscript 1, 2 and 3 (U+00Bx) are different from 0 and 4-9 (U+207x).
    Last edited by ChrisW67; 13th May 2013 at 23:57.

Similar Threads

  1. Replies: 5
    Last Post: 2nd November 2012, 03:38
  2. Strange QString::number behaviour
    By sastrian in forum Qt Programming
    Replies: 1
    Last Post: 7th September 2012, 08:29
  3. Replies: 16
    Last Post: 21st May 2011, 12:22
  4. Can I use arbitrary constants in a connect()?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 18th February 2008, 22:48
  5. strange problem about QString::number(double)
    By yuzr in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 24th December 2007, 13:05

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.