Założyłem komponent przycisku QML i wykorzystał składnik o nazwie ColorImage
dla ikony. Po wyszukaniu sposobu, aby zmienić kolor zdjęć. Dowiedziałem się, że Qt nie obsługuje ColorOverlay
Umieść kursor, po prostu zdobył "kolor" w Qt Design Studio i ColorImage
wyskoczył. Próbowałem znaleźć dokumentację w Internecie, ale nic nie mogłem znaleźć. Jednak, gdy postanowiłem spróbować, to działa dokładnie tak, jak się spodziewałem:
To odpowiedni kod z mojej przyciski:
contentItem: ColorImage {
id: buttonIcon
source: imageSource
fillMode: Image.PreserveAspectFit
height: parent.height
color: iconColor
anchors.fill: actionBarButton
anchors.margins: 4
}
Kiedy hovered
stan za pomocą przycisku staje się true
pozwala to uzyskać następujący stan:
State {
when: (hovered && !checked)
name: "hoveredNotChecked"
PropertyChanges {
target: buttonIcon
color: "white"
}
PropertyChanges {
target: buttonBackground
color: iconColor
}
},
który zamienia miejscami ikony i kolor tła przycisku.
To działa w podglądzie Qt Designer. Jednak, gdy próbuję go uruchomić z Pyside, on informuje mnie: ColorImage is not a type
i po prostu nie można załadować przycisk.
Próbowałem znaleźć w dokumentacji ColorImage
aby dowiedzieć się, być może, czegoś brakuje w imporcie. Jednak nic nie mogłem znaleźć. Wewnętrzna pomoc Qt Designer również nic nie dała. Jakby tego składnika nie istnieje. Ale to i tak działa w design studio.
Oto pełny kod mojej przyciski:
Button {
id: actionBarButton
property color iconColor: "red"
property color backgroundColor: "blue"
property string toolTipText: "Play video!"
property string imageSource: "images/round_play_arrow_white_36dp.png"
property string imageSourceChecked: "images/round_play_arrow_white_36dp.png"
states: [
State {
when: (hovered && !checked)
name: "hoveredNotChecked"
PropertyChanges {
target: buttonIcon
color: "white"
}
PropertyChanges {
target: buttonBackground
color: iconColor
}
},
State {
when: (hovered && checked)
name: "hoveredChecked"
PropertyChanges {
target: buttonIcon
source: imageSourceChecked
color: "white"
}
PropertyChanges {
target: buttonBackground
color: iconColor
}
},
State {
when: checked
name: "checked"
PropertyChanges {
target: buttonIcon
source: imageSourceChecked
}
}
]
transitions: Transition {
ColorAnimation {
duration: 300
}
}
contentItem: ColorImage {
id: buttonIcon
source: imageSource
fillMode: Image.PreserveAspectFit
height: parent.height
color: iconColor
anchors.fill: actionBarButton
anchors.margins: 4
}
onHoveredChanged: {
}
background: Rectangle {
id: buttonBackground
color: backgroundColor
anchors.fill: actionBarButton
}
ToolTip.delay: 1000
ToolTip.timeout: 5000
ToolTip.visible: hovered
ToolTip.text: actionBarButton.toolTipText
}
Oto jak to wygląda w konstruktorze:
Może ktoś pomóc mi zrozumieć, dlaczego on skarży się na ColorImage
nie w moim typie, gdy próbuję uruchomić?
Edytuj:
Import w pliku powyżej:
import QtQuick 2.15
import QtQuick.Controls 2.15