Qt - QPushButton text formatting
You really don't need to subclass to change the formatting of your button, rather use stylesheets e.g.
QPushButton {
font-size: 18pt;
font-weight: bold;
color: #ff0000;
}
Applying this to the button that you want to change will make the buttons text 18pt, bold and red. You can apply via widget->setStyleSheet()
Applying this to a widget in the hierarchy above will style all the buttons underneath, the QT stylesheet mechanism is very flexible and fairly well documented.
You can set stylesheets in the designer too, this will style the widget that you are editing immediately
QPushButton multiple lines of text formating
I try this way, I create a label and set its parent pushbutton
QString normal = "normal";
QString bold = "bold";
auto const text = QStringLiteral("<font size=18>%1</font><br><b><font size=24>%2</font></b>")
.arg(normal.toHtmlEscaped())
.arg(bold.toHtmlEscaped());
QPushButton *btn = new QPushButton(this);
btn->setGeometry(QRect(70, 30, 150, 100));
QLabel *lbl = new QLabel(btn);
lbl->setGeometry(QRect(0, 0, 150, 100));
lbl->setAlignment(Qt::AlignRight);
lbl->setText(text);
output:
QPushbutton: Two different Font size
Derive from QPushButton and draw the text yourself. You can refer this post for reference.
Two colours text in QPushButton
How to manage long string in the text of a QPushButton?
Text
item give you the WrapMode
. Refer here
You can take this as an example: MyButton.qml
import QtQuick 2.0
Rectangle {
id: buttonRect
height: 40
width: 200
color: "#404040"
radius: 5
property alias buttonText: text
signal clicked()
Rectangle {
id: maskRect
anchors.fill: parent
color: "#575757"
radius: buttonRect.radius
visible: buttonMousearea.pressed
}
Text {
id: text
text: qsTr("Button")
color: "white"
width: parent.width
anchors.centerIn: parent
wrapMode: Text.Wrap
}
MouseArea {
id: buttonMousearea
anchors.fill: buttonRect
hoverEnabled: true
acceptedButtons: Qt.LeftButton
onClicked: buttonRect.clicked()
}
states: [
State {
name: "hasMouse"
when: buttonMousearea.containsMouse
PropertyChanges {
target: buttonRect
color: "#6e6e6e"
}
PropertyChanges {
target: buttonMousearea
cursorShape: Qt.PointingHandCursor
}
}
]
}
Then use it like below
MyButton {buttonText.text: "something which is really longer than the button"}
Qt QPushbutton Icon above Text
If you're able to, the easiest thing to do is use a QToolButton instead:
QToolButton* button = new QToolButton(this);
button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
button->setIcon(myIcon);
button->setText("Sample text");
If that's not an option you could consider creating your own button widget, possibly derived from QPushButton or QAbstractButton. In this case you'll probably (I haven't tried it myself) want to focus most of your efforts on reimplementing paintEvent()
.
[Edit: read the comments for alternatives which are probably way simpler than this]
How to create a bold, red text label in Qt?
Try using HTML formatting: <b><font... etc </b>
.
Qt Designer does it like this: <span style=" font-size:8pt; font-weight:600; color:#aa0000;">TextLabel</span>
Related Topics
Ubuntu System Monitor and Valgrind to Discover Memory Leaks in C++ Applications
Macro and Member Function Conflict
Constructor-Style Casting in Function Call Parameters
Windows Named Pipe Support in Linux
Boost Graph Copy and Removing Vertex
How to Calculate a Sha-512 Hash in C++ on Linux
Dealing with Library Dependencies on Linux
Getting Errors While Compiling
How to Check That I Didn't Break Anything When Refactoring
How to Get the Precision of High_Resolution_Clock
Performance of Std::Function Compared to Raw Function Pointer and Void* This
C++ Unions VS. Reinterpret_Cast
Communicate with Codesys Program on a Linux-Based Wago Pfc200 Plc