Am făcut o QML butonul component și am folosit o componenta nume ColorImage
pentru icoana. După căutarea pentru o modalitate de a schimba imagine de culoare. Am aflat că Qt nu mai suport ColorOverlay
Hover, am tastat "culoare" în Qt Studio de Design și ColorImage
a apărut. Am încercat să găsesc documentație on-line, dar nu a putut găsi nimic. Cu toate acestea, atunci când am decis să-l încercați, pur și simplu funcționează ca m-am asteptat:
Acest lucru este în conformitate cu codul de la butonul meu:
contentItem: ColorImage {
id: buttonIcon
source: imageSource
fillMode: Image.PreserveAspectFit
height: parent.height
color: iconColor
anchors.fill: actionBarButton
anchors.margins: 4
}
Atunci când hovered
de stat din butonul devine true
acesta permite următoarele stat:
State {
when: (hovered && !checked)
name: "hoveredNotChecked"
PropertyChanges {
target: buttonIcon
color: "white"
}
PropertyChanges {
target: buttonBackground
color: iconColor
}
},
care swap-uri pe pictogramă și culoarea de fundal pe buton.
Acest lucru funcționează în previzualizare Qt Designer. Cu toate acestea, atunci când am încercați să executați din Pyside, îmi spune: ColorImage is not a type
și pur și simplu nu reușește să se încarce buton.
Am incercat sa gasesc documentatie despre ColorImage
să-mi dau seama că există o lipsă de import. Cu toate acestea, nu am putut să nu apară nimic. Qt Designer internă a ajuta nu a iesit nimic la fel de bine. Este ca și cum dacă această componentă nu există. Dar nu, și funcționează în Studio de Design.
Aici este codul pentru butonul meu:
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
}
Acesta este modul în care arată în designer:
Ma poate ajuta cineva sa mi dau seama de ce se plânge ColorImage
nu sunt un tip atunci când am încerca pentru a lansa?
Edit:
Importurile în dosarul de mai sus:
import QtQuick 2.15
import QtQuick.Controls 2.15