When I create a QPushButton with an Icon it by default displays the text to the right of the icon. Is there a way to have the text display above the icon?
The easiest way is to use a QToolButton instead. This class has a method named setToolButtonStyle, which allows you to do exactly what you want.
In C++ (I have never messed with PyQt):
QToolButton *button = new QToolButton(this);
button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
Link to the C++ docs: QToolButton and Qt::ToolButtonStyle
Related
Does Qt offer a way to configure the layout of a QCheckBox with Qt Stylesheets so that the text remains aligned to the right but the checkbox is placed on the far right?
You can use QCheckBox::setLayoutDirection(Qt::LayoutDirection direction):
auto chkB = new QCheckBox("some example");
chkB->setLayoutDirection(Qt::RightToLeft);
How make text at button under the icon with qss ?
qustion how do it at QPushbutton, not at toolbutton.
There would be more solutions to your question but, I prefer to use QToolButton instead of using QPushButton and set toolButtonStyle property to ToolButtonTextUnderIcon. To do it fast in ui file right click on your QPushButton and choose morph into QToolButton.
I have a QToolButton. I wanted text and icon to be on it. I set button style by setToolButtonStyle(Qt::ToolButtonTextBesideIcon).
But icon and text are so close to each other. Is there a method to give some space between icon and text by css?
You can't. There is no such a property neither for QToolButton (nor for QPushButton). All the properties for these buttons are in the documentation.
One thing you can do is to create your own class inheriting from QToolButton and overriding the paintEvent(). In this function, you will manually place your icon.
This is the shortest solution, but if you're brave enough, there are longer paths, like creating your own button subclassing directly QWidget (but in this case, you will need to implement ALL its behavior).
You can achieve it although this is not the documented solution.
Just provide spaces in QPushButton text start. By Doing this, icon and text will automatically go far from each other.
By default, the text is always on the right-side of the tab icon...
What should I do to move the text to the bottom of it ?
And is there an option to do icon/text alignment ??
(I'm designing the GUI base on Qt Creator Designer.
I'm trying with Qt stylesheet but I can't. I have not yet modify the code that generated by Qt Designer.)
Thank you very much!
A tricky way:
Since we can set QToolButton to be icon above text, just create a group of QToolButtons, listed horizontally or vertically, each button needs to be checkable. Add them to a QButtonGroup.
Then hide the TabBar of QTabWidget(out of parent widget or under ToolButtons), place the TabWidget under the listed ToolButtons.
Finally, connect the QButtonGroup buttonClicked signal to the TabWidget's setCurrentIndex signal.
Notice, when you add a button to QButtonGroup, you will have to assign the ID from 0 manually.
Is it possible to format QToolButton text with HTML like in QLabel? toolbutton.SetText("Text <b>Test</b>") doesn't work
I don't think this is possible without subclassing QToolButton and overriding the paintEvent, but if you just want to style the text in the button, you can use Qt Style Sheets like this:
toolButton->setStyleSheet("font-weight: bold");