Change Font Size of Button in Qt QML

前端 未结 2 1409
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 20:01

How can the font size of the text in a Button control be set in QML? The designer has not option, and \'font\' is not a valid property of Button.

Button {
    id         


        
2条回答
  •  一个人的身影
    2021-02-07 20:37

    For QtQuick 2, you have to use the contentItem property as shown here: https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-button

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    
    Button {
        id: control
        text: qsTr("Button")
    
        contentItem: Text {
            text: control.text
            font: control.font
            font.pointSize: 20
            opacity: enabled ? 1.0 : 0.3
            color: control.down ? "#17a81a" : "#21be2b"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            elide: Text.ElideRight
        }
    }
    

提交回复
热议问题