Qt tooltip with arrow - c++

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.

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.

Set CSS both in designer and in code for my custom widget

I have a custom widget subclassed from QPushButton, MyButton. It's implemented in a single .cpp file and I'm using it in Qt Creator in my application's form (I've added a QPushButton then promoted it to MyButton).
As discussed here: Should I really use a single qss file for my application instead of having one for each (UI) class? I wish to generally set its CSS inside its constructor (the general look of the button, the background image, hover behavior etc), but I would also like to be able to set it in Qt Creator (maybe customize the font size/color; generally specific to a particular button).
The issue is, as the setupUi call is issued, my buttons are created, they set their styles in their constructors, but then Qt applies the styles set in Qt Creator, immediately overriding mine.
What can I do to achieve this effect?

How to display superscript in QComboBox item?

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.

QT4 C++ GUI Design - Tabbed interface alternative

Designing an interface with QT4 I have been advised that using multiple "Tabs" at the top of an interface to navigate different pages is not the most elegant design. Instead I would like to do something similar to the Options dialog in QT Creator 4.8.
Does anyone know the name of a widget that resembles that in the Options Dialog of QT Creator.
ex. Click Tools->Options... In QT Creator.
Notice the layout on the left hand side of the screen, which to me seems nicer than only tabs across the top of the screen.
Thanks for your help!
It's QLiveView/QListWidget with delegate that draw icon and string.
Here is link to documentation for QListView, If you are not happy with default list view delegate then you can create your own QItemDelegate to handle custom drawing. Here is sample code to create custom QItemDelegate
I just stumbled upon this implementation of a ribbon interface this morning. Maybe this is helpful to you?
http://qt-project.org/forums/viewthread/4214

Creating a tree-view with buttons? in QT

I am trying to make a dialog box like below in QT, the only problem is I have no idea what the widget is called. The bar on the left is like a tree-view widget, but when you click on it, it updates the text on the right. Does anybody happen to know what the widget is called or what widget(s) are required to perform this? I am using QT C++ on Windows.
There is an example with Qt showing you how to do this.
https://doc-snapshots.qt.io/4.8/dialogs-configdialog.html
If you're using Qt Creator as IDE, you can find it under the "Demos and Examples" tab in the Welcome Screen too.
It uses a QListWidget for the selector, and QStackedWidget to control the different pages. Connect the currentItemChanged signal of the list widget to change what page should be shown. Everything you'll need is in configdialog.cpp.
If you realy need to add QPushButton into QListWidget, use setItemWidget, or into ListView use QAbstractItemView::setIndexWidget