View Full Version : How to write subscript and superscript texts in any Qwidgets
rajeshs
1st July 2007, 13:24
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
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 (http://doc.trolltech.com/4.3/richtext-html-subset.html) so one can use them in all the widgets (like QLabel and QTextEdit) that support rich text (http://doc.trolltech.com/4.3/richtext.html). 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 (http://www.qtcentre.org/forum/f-qt-programming-2/t-qstandarditem-subpart-of-the-text-as-bold-5548.html#4).
DEEPADEV
23rd June 2012, 12:40
Can anyone please help with a small piece of code for the same
wysota
23rd June 2012, 12:51
label->setText("x<sub>1</sub><sup>2</sup>");
huyhoangfool
1st November 2012, 09:33
To QTableWidget, I derive paint() function of QItemDelegate like this:
void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->save();
QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
painter->setFont(font);
painter->drawStaticText(option.rect.x(),option.rect.y(), QStaticText(index.data().toString()));
painter->restore();
}
_ Draw rich text with Qt::AlignCenter:
void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
QBrush brush = qvariant_cast<QBrush>(index.data(Qt::ForegroundRole));
QFontMetrics fm(font);
QRect boundingRect = fm.boundingRect(index.data().toString().remove("<sub>").remove("</sub>")); //We can remove other html tags
int stringWidth = boundingRect.width();
int stringHeight = boundingRect.height();
int xPoint = option.rect.x() + option.rect.width()/2 - stringWidth/2;
int yPoint = option.rect.y() + option.rect.height()/2 - stringHeight/2;
painter->save();
painter->setFont(font);
painter->setPen(brush.color());
painter->drawStaticText(xPoint , yPoint, QStaticText(index.data().toString()));
}
Regards.
ChrisW67
2nd November 2012, 03:38
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.