• 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. }

  • 相关阅读:
    decapoda-research/llama-7b-hf 的踩坑记录
    安卓逆向(二)httpClient使用
    Kafka消费者分区分配策略
    【gpt实践】同时让chatgpt和claude开发俄罗斯方块
    集群时的缓存同步
    常见JVM面试题及答案整理
    vue前端拿到后端pdf与zip等重新打包为一个新的zip包
    经历多次面试后,来自美团面试官给我的建议(附:java岗经验分享)
    Axure8.0教程:自动带出邮箱
    算法手撕代码101~110
  • 原文地址:https://blog.csdn.net/notfindjob/article/details/139797265