QML одинаковые настройки для разных кнопок - PullRequest
1 голос
/ 25 января 2020

Я должен использовать одни и те же настройки для разных кнопок, но я не понимаю, как я могу сделать это без записи этих настроек в каждую кнопку :( Я имею в виду цвет текста, размер шрифта, цвет кнопки, цвет кнопки при нажатии кнопки

RowLayout {
        id: rowLayout
        width: 100
        height: 100
        Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter

        Button {
            id: button
            Text {
            color:"#666666"
            text: qsTr("й")
            font.family: "Geometria Medium"
            font.pointSize: 18
            }
            background: Rectangle {
                implicitWidth: 60
                implicitHeight: 60
                color: button.down ? "#B3B3B3" : "#E6E6E6"
                radius: 4
            }

        }

        Button {
            id: button1
            height: 40
            //                width: 70
            text: qsTr("ц")

        }

        Button {
            id: button2
            width: 50
            text: qsTr("у")
        }

1 Ответ

1 голос
/ 26 января 2020

Необходимо создать компонент, например, CustomButton.qml:

CustomButton.qml

import QtQuick 2.14
import QtQuick.Controls 2.14

Button {
    id: control
    contentItem: Text {
        color:"#666666"
        text: control.text
        font.family: "Geometria Medium"
        font.pointSize: 18
    }
    background: Rectangle {
        implicitWidth: 60
        implicitHeight: 60
        color: control.down ? "#B3B3B3" : "#E6E6E6"
        radius: 4
    }
}
RowLayout {
    id: rowLayout
    width: 100
    height: 100
    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
    CustomButton{
        text: qsTr("й")
    }
    CustomButton{
        text: qsTr("ц")
    }
    CustomButton{
        text: qsTr("у")
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...