I want to display 10-8 in QComboBox item. But it displays "sup" tags.
3 Answers
the easiest way is to use special Unicode characters and use them in translation file (direct usage in code may be problematic):
10⁻⁸
If you don't like use translation file try this code:
ui->comboBox->addItem(QString::fromWCharArray(L"10\x207B\x2078"));
ui->comboBox->addItem(QString::fromWCharArray(L"10⁻⁸"));
On my Qt.5.2.1 (Linux) it works. Also pasting above string in designer also works.

- 32,568
- 6
- 55
- 140
-
yes it is not localizeable string, but nothing forbids to use additional translation file which will contain pure Unicode strings (I prefer this solution instead have some problems with encoding of source code file). – Marek R Mar 16 '14 at 13:48
-
As far as I understand the OP's problem is that this does not work, not even when the editor is correctly set up for the encoding. – László Papp Mar 16 '14 at 13:53
-
yes but he tried use html tags, I recommend use Unicode characters which are in superscript without any tags. If you don't like translation file, there are other means to achieve those characters (pasted in designer for example). – Marek R Mar 16 '14 at 14:10
-
No, the OP first tried unicode letters, and then tags. It is simple. Try out yourself. :-) Either you can set the editor to the unicode encodings (or read it with QTextStream with proper encoding set, but it is very hacky) or you need rich text. – László Papp Mar 16 '14 at 14:19
-
-
With this addition, I withdraw my -1 for the translation file disguise, thanks. :-) Btw, I fully understood your answer, I just do not like "translation file" hacks as in your original answer. – László Papp Mar 16 '14 at 15:26
-
@LaszloPapp I've found [this](http://qt-project.org/wiki/Strings_and_encodings_in_Qt#4a92171d9f4a12672141d13bf1b7aaa3), so using `tr` for such cases is even recommended by Qt documentation. – Marek R Mar 20 '14 at 13:50
-
As the sentence writes, it is for plurals, not the case here in question. – László Papp Mar 20 '14 at 14:11
There is no simple way of making this happen. The best way is to override style of QComboBox using QProxyStyle class. Then you can paint the text of a combobox using QTextDocument or similar.
http://qt-project.org/doc/qt-5.0/qtwidgets/qproxystyle.html
More specifically, QComboBox uses
void QComboBox::paintEvent(QPaintEvent *)
{
QStylePainter painter(this);
....
// draw the icon and text
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
and from there you can find how this is drawn from qtbase/src/widgets/styles/qcommonstyle.cpp. Overrides to draw custom text instead of basic text should be apparent, at least for read-only QComboBox.
If you need to edit things in the line edit of the QComboBox, then you'll need to override that QLineEdit with your own.
QComboBox::itemDelegate() only overrides painting of popup list, as it indicated in the documentation. It does not override display of the of editbox text and thus it cannot be used to completely address your problem.

- 1,769
- 11
- 15
You cannot do this easily, e.g. with html, because QComboBox
does not seem to support rich text like some other QWidget
subclasses.
However, you could replace the default delegate to draw as you wish, including this. You would need to set your item delegate for the QComboBox
, and use QTextDocument
in your paint method.
Here is the corresponding bugreport that was submitted a while ago:
Alternatively, you double could check the encoding. It may be just a simple issue about the unicode handling. If both work for you, then it is up to you how to handle it.
Based on the following thread thread, you could try this QString method.

- 51,870
- 39
- 111
- 135
-
-
@Alen: ah, yes, there is some limitation for the minimum characters entered. It will be a bit more work than you anticipated as there is no off-hand support for it, but at least it will work. – László Papp Mar 16 '14 at 10:53
-
@Alen the normal way to say thanks at SO is to upvote... :-) And as asker, you also have the option to accept one answer to indicate it solved your problem (but it's good idea to wait a few days in case more good answers are given). – hyde Mar 16 '14 at 19:34