初学QML的时候,遇到了一些问题,参考这篇文章,实现了按钮的正常、鼠标经过和鼠标按下的图片切换。遇到了一些问题,比如背景色有白边,设置图片大小不生效。尝试了几种方法后,还是发现原来的方法比较好。记录一下自己改良后的代码:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Button
{
id: root_Button
property string nomerPic: "qrc:/images/login_normal.png"
property string hoverPic: "qrc:/images/login_hover.png"
property string pressPic: "qrc:/images/login_pressed.png"
style: ButtonStyle {
background:Rectangle{
implicitHeight: root_Button.height
implicitWidth: root_Button.width
color: "transparent" //设置背景透明,否则会出现默认的白色背景
BorderImage{
anchors.fill: parent
source: control.hovered ? (control.pressed ? pressPic : hoverPic) : nomerPic;
}
}
}
}
参考:https://blog.csdn.net/z609932088/article/details/70500029