如何管理QPushButton文本中的长字符串?

时间:2020-05-04 18:00:07

标签: qt

我想写的文本很长时间都只能在一行中看到。文本如何适应按钮的大小,并显示在多行上。 What I don't want to have

1 个答案:

答案 0 :(得分:1)

Text项给您WrapMode。请参阅here

您可以以此为例: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
            }
        }
    ]
}

然后按如下所示使用它

MyButton {buttonText.text: "something which is really longer than the button"}
相关问题