How to display superscript in QComboBox item? - c++

I want to display 10-8 in QComboBox item. But it displays "sup" tags.

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.

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.

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:
Rich-text in combo box items
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.

Related

How to Create Custom, Instant, Arrow-Shaped ToolTip with unique shape?

On many web-pages nowadays, you'll frequently see instant tooltips with an arrow that points to their target, similar to:
https://www.w3schools.com/css/tryit.asp?filename=trycss_tooltip_arrow_bottom
(More specifically, I'm looking for something like: https://www.youtube.com/watch?v=0Jedht9Arec)
How would you exactly replicate this in QT? I'm not necessarily looking for something super automated, just something that can be given a position to appear at, and a function call to remove it. Furthermore, if possible, it should have curved, anti-aliased corners.
I've tried using custom QToolTip, but it's behavior does not meet my standards. I've also tried a custom QDialog with a Popup flag, but it freezes the dialog it appears above.
Any recommendations on how to proceed?
As requested by two comments below, here is the code for the QDialog scenario previously referenced. Prepare yourself, it's a lot:
// Assuming "this" is the parent dialog
QDialog* popup = new QDialog(this, Qt::Popup);
popup->show();
This code blocks mouse hover events of the parent dialog (the "this" object), thus making it unsuitable as a tool-tip replacement.
You can use QWidget::setMask to specify custom shape of a widget. Additionally you'll have to set widget's window flags to include Qt::ToolTip.

How to set only one tab in a QTabWidget as not moveable in Qt?

I need to set only one tab in a QTabWidget as not moveable. I cannot find a way to do this. Also in Qt designer you can only (un)check "moveable", but this doesn't work for just one specific tab.
Looking at the source code of QTabBar (sub-element of QTabWidget), it seems that this is not possible. There is only one movable property for all tabs.
However, you could try the following:
Subclass QTabBar and catch the mouse press events before a drag starts to enable or disable the moving of tabs. Something like this:
void MyTabBar::mousePressEvent(QMouseEvent *event) {
// all tabs movable except first
setMovable(tabAt(event->pos()) != 0);
QTabBar::mousePressEvent(event);
}
Then before you add the tabs, replace the default tabbar with your subclass:
myTabWidget->setTabBar(new MyTabBar());
This is probably a bit leaky, I haven't tried it out. The first thing that comes to mind is that setMovable() does not work anymore. Maybe also dragging some other tabs will result in some odd behavior.
Have you considered, that maybe a QTabWidget is not the right GUI-element? There is no graphical hint that tabs are draggable or not and might confuse the user. Maybe it's better to use a QStackedWidget and a custom way to change between the pages.

Qt tooltip with arrow

I have Qt 5.2.1 widgets-based project. I want to show cool tooltip with arrow while user hovers some labels (or any other widgets that can show image), like this:
But i found no examples for this. So am i able to do this with QToolTip? Or should i use another widgets to reach same look instead?
It is no matter what classes and styles i have to use, i need to reach similar effect any way.
I've tried to customize QToolTip with basic CSS but had failed.
You can use QBalloonTip which is an internal class defined in
QtDir/5.2.1/Src/qtbase/src/widgets/util/qsystemtrayicon_p.h
QBalloonTip inherits QWidget and it is implemented in qsystemtrayicon.cpp at the same directory. It has the following method to show a balloon tip:
void QBalloonTip::balloon(const QPoint& pos, int msecs, bool showArrow)
You can modify the source code of this class to have your desired balloon tip.

How to use .hide on a Qt LineEdit, still take input?

I have a QLineEdit which I would like to hide from the user but still take in input form somewhere. I am creating a typing tutor and I want to take input in a hidden manner in order to provide a more dynamic form of feedback.
Any other suggestions as to best accomplish would be greatly appreciated
You can not do it. When QLineEdit is hidden, there is no focus on it, and you can not grab events.
If you persist on using QLineEdit there's an option to turn off displaying text.QLineEdit::NoEcho.
lineEdit->setEchoMode(QLineEdit::NoEcho);
This will show the edit box, but it doesn't show any text.
Otherwise, you should write a slot to grab window keyPressed signals, and handle everything yourself.
For other people who try to do such a thing, a workaround is simply to implement a QLineEdit visible, but with a MinimumSize = MaximumSize = 0x0 :)

retranslateUi() clears QLabel text

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.
As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code