Results 1 to 6 of 6

Thread: How to write subscript and superscript texts in any Qwidgets

  1. #1
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up How to write subscript and superscript texts in any Qwidgets

    Hi All,

    Is there any way to write subscripts and superscripts texts in Qt widgets like QListWidget,QLabel and QLineEdit.,

    Thanks and Regards
    Rajesh.S

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to write subscript and superscript texts in any Qwidgets

    Quote Originally Posted by rajeshs View Post
    Is there any way to write subscripts and superscripts texts in Qt widgets like QListWidget,QLabel and QLineEdit.,
    Subscript and superscript tags belong to supported HTML subset so one can use them in all the widgets (like QLabel and QTextEdit) that support rich text. However, QListWidget and QLineEdit do not support rich text. One can enable rich text support in model-view classes by a custom delegate like this.
    J-P Nurmi

  3. #3
    Join Date
    Apr 2012
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write subscript and superscript texts in any Qwidgets

    Can anyone please help with a small piece of code for the same

  4. #4
    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: How to write subscript and superscript texts in any Qwidgets

    Qt Code:
    1. label->setText("x<sub>1</sub><sup>2</sup>");
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #5
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to write subscript and superscript texts in any Qwidgets

    To QTableWidget, I derive paint() function of QItemDelegate like this:

    Qt Code:
    1. void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. painter->save();
    5.  
    6. QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
    7. painter->setFont(font);
    8. painter->drawStaticText(option.rect.x(),option.rect.y(), QStaticText(index.data().toString()));
    9.  
    10. painter->restore();
    11. }
    To copy to clipboard, switch view to plain text mode 

    _ Draw rich text with Qt::AlignCenter:

    Qt Code:
    1. void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
    5. QBrush brush = qvariant_cast<QBrush>(index.data(Qt::ForegroundRole));
    6.  
    7. QFontMetrics fm(font);
    8. QRect boundingRect = fm.boundingRect(index.data().toString().remove("<sub>").remove("</sub>")); //We can remove other html tags
    9. int stringWidth = boundingRect.width();
    10. int stringHeight = boundingRect.height();
    11.  
    12. int xPoint = option.rect.x() + option.rect.width()/2 - stringWidth/2;
    13. int yPoint = option.rect.y() + option.rect.height()/2 - stringHeight/2;
    14.  
    15. painter->save();
    16.  
    17. painter->setFont(font);
    18. painter->setPen(brush.color());
    19. painter->drawStaticText(xPoint , yPoint, QStaticText(index.data().toString()));
    20. }
    To copy to clipboard, switch view to plain text mode 
    Regards.
    Last edited by huyhoangfool; 1st November 2012 at 11:12.

  6. #6
    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: How to write subscript and superscript texts in any Qwidgets

    Can anyone please help with a small piece of code for the same
    The last, broken, link in JPN's 5 year-old answer is an example.
    Last edited by ChrisW67; 2nd November 2012 at 05:04.

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.