• QT QML 界面设计教程2——图标(图片)按钮样式


    MyIconButton.xml

    1. import QtQuick 2.4
    2. Rectangle {
    3. id: rec
    4. property alias img_src: icon.source
    5. property alias btn_txt: button.text
    6. property color clr_enter: "#dcdcdc"
    7. property color clr_exit: "#ffffff"
    8. property color clr_click: "#aba9b2"
    9. property color clr_release: "#ffffff"
    10. //自定义点击信号
    11. signal clickedLeft()
    12. signal clickedRight()
    13. signal release()
    14. width: 130
    15. height: 130
    16. radius: 10
    17. Image {
    18. id: icon
    19. width: 100
    20. height:100
    21. source: "qrc:/mycamera.png" //图片资源
    22. fillMode: Image.PreserveAspectFit
    23. clip: true
    24. anchors.top: parent.top
    25. anchors.right: parent.right
    26. anchors.left: parent.left
    27. anchors.margins: 0
    28. }
    29. Text {
    30. id: button
    31. text: qsTr("button")
    32. anchors.top: icon.bottom
    33. anchors.topMargin: 5
    34. anchors.horizontalCenter: icon.horizontalCenter
    35. anchors.bottom: icon.bottom
    36. anchors.bottomMargin: 5
    37. font.bold: true
    38. font.pointSize: 14
    39. }
    40. MouseArea {
    41. id: mouseArea
    42. anchors.fill: parent
    43. hoverEnabled: true
    44. //接受左键和右键输入
    45. acceptedButtons: Qt.LeftButton | Qt.RightButton
    46. onClicked: {
    47. //左键点击
    48. if (mouse.button === Qt.LeftButton)
    49. {
    50. parent.clickedLeft()
    51. // console.log(button.text + " Left button click")
    52. }
    53. else if(mouse.button === Qt.RightButton)
    54. {
    55. parent.clickedRight()
    56. // console.log(button.text + " Right button click")
    57. }
    58. }
    59. //按下
    60. onPressed: {
    61. color = clr_click
    62. }
    63. //释放
    64. onReleased: {
    65. color = clr_enter
    66. parent.release()
    67. console.log("Release")
    68. }
    69. //指针进入
    70. onEntered: {
    71. color = clr_enter
    72. // console.log(button.text + " mouse entered")
    73. }
    74. //指针退出
    75. onExited: {
    76. color = clr_exit
    77. // console.log(button.text + " mouse exited")
    78. }
    79. }
    80. }

    调用:

    1. MyIconButton {
    2. x: mainWindow.width-40
    3. y: -8
    4. width: 20
    5. height: 20
    6. radius: 5
    7. id: btClose
    8. Image {
    9. id: name
    10. width: 20
    11. height:20
    12. source: "qrc:/image/close.png"
    13. }
    14. btn_txt: ""
    15. onClickedLeft: close()
    16. //onClickedRight: close()
    17. }

  • 相关阅读:
    Spring 中容器启动分析之refresh方法执行之前
    计算机网络第四章——网络层(中)
    【Matlab】笔记:matlab Optimization Tool使用一——pattern search
    数据结构_链表
    javaweb——socket
    模块与包——
    BeanShell 如何加密加签?
    图像处理黑科技—破解文档识别难题(PS检测、弯曲拉平、切边切片、摩尔纹)
    【疯壳·ARM功能手机开发教程3】整板资源介绍
    大厂面试题-Java并发编程基础篇(五)
  • 原文地址:https://blog.csdn.net/notfindjob/article/details/139797265