Qt5.12.2 qml中实现ComboBox以及CheckBox

ComboBox:    

ComboBox {
                           id: control6
                           x: 110
                           y: 27
                           width: 122
                           height: 30
                           model: ["禁用","普通告警","车牌抓拍","手动告警","断电告警","倒车映像"]
                           currentIndex: setform.alarmEn3
                           implicitWidth: implicitBackgroundWidth
                           implicitHeight: implicitBackgroundHeight
                           leftPadding: padding + (!control6.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
                           rightPadding: padding + (control6.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)

                           // 可在此定义多为改变的特殊属性,在调用时直接指定此属性即可
                           font.pointSize: 8
                         //  font.family: "Microsoft YaHei"
                           property color fontColor: "black"             // 字体颜色
                           property color backgroundColor: "#E5E5E5"    // 背景色

                           delegate: ItemDelegate {
                                   width: parent.width
                                   height: 30

                                   // 行字体样式
                                   contentItem: Text {
                                       text: modelData
                                       font: control6.font
                                       color: control6.fontColor
                                       elide: Text.ElideRight
                                       verticalAlignment: Text.AlignHCenter
                                       renderType: Text.NativeRendering

                                   }

                                //   palette.text: control4.palette.text;
                                //   palette.highlightedText: control4.palette.highlightedText;
                                   font.weight: control6.currentIndex === index ? Font.DemiBold : Font.Normal
                                   highlighted: control6.highlightedIndex === index
                                   hoverEnabled: control6.hoverEnabled
                               }
                               // 右侧下拉箭头
                               indicator: Canvas {
                                   id: canvas6
                                   x: control6.width - width - control6.rightPadding
                                   y: control6.topPadding + (control6.availableHeight - height) / 2
                                   width: 10
                                   height: 6
                                   contextType: "2d"

                                   Connections {
                                       target: control6
                                       onPressedChanged: canvas6.requestPaint()
                                   }

                                   onPaint: {
                                       context.reset()
                                       context.moveTo(0, 0)

                                       context.lineWidth = 2
                                       context.lineTo(width / 2, height*0.8)
                                       context.lineTo(width, 0)
                                       context.strokeStyle = control6.pressed ? "#EEEFF7" : "#999999"
                                       context.stroke()
                                   }
                               }

                               // ComboBox的文字位置样式
                               contentItem: TextField {
                                   leftPadding: !control6.mirrored ? 12 : control6.editable && activeFocus ? 3 : 1
                                   rightPadding: control6.mirrored ? 12 : control6.editable && activeFocus ? 3 : 1
                                   topPadding: 6 - control6.padding
                                   bottomPadding: 6 - control6.padding

                                   text: control6.editable ? control6.editText : control6.displayText

                                   enabled: control6.editable
                                   autoScroll: control6.editable
                                   readOnly: control6.down
                                   inputMethodHints: control6.inputMethodHints
                                   validator: control6.validator

                                   font: control6.font
                                   color: control6.fontColor
                                   //color: control.editable ? control.palette.text : control.palette.buttonText
                                   selectionColor: control6.palette.highlight
                                   selectedTextColor: control6.palette.highlightedText
                                   verticalAlignment: Text.AlignVCenter
                                   renderType: Text.NativeRendering

                                   background: Rectangle {
                                       visible: control6.enabled && control6.editable && !control6.flat
                                       border.width: parent && parent.activeFocus ? 2 : 1
                                       border.color: parent && parent.activeFocus ? control6.palette.highlight : control6.palette.button
                                       color: control6.palette.base
                                   }
                               }

                               // ComboBox 的背景样式
                               background: Rectangle {
                                   implicitWidth: 120
                                   implicitHeight: 30

                                   radius: 3;
                                   color: control6.enabled ? "#FFFFFF" : control6.backgroundColor
                                   border.color: control6.backgroundColor
                                   border.width: !control6.editable && control6.visualFocus ? 2 : 1
                                   visible: !control6.flat || control6.down
                               }

                               // 弹出窗口样式
                               popup: Popup {
                                   y: control6.height
                                   width: control6.width
                                   //height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
                                   height: contentItem.implicitHeight
                                   topMargin: 3
                                   bottomMargin: 3

                                   contentItem: ListView {
                                       // 防止显示过界
                                       clip: true
                                       //禁止滑动
                                       // interactive: false;
                                       //禁用橡皮筋效果
                                       boundsBehavior: Flickable.StopAtBounds

                                       implicitHeight: contentHeight
                                       model: control6.delegateModel
                                       currentIndex: control6.highlightedIndex
                                       highlightMoveDuration: 0

                                       Rectangle {
                                           z: 10;
                                           width: parent.width
                                           height: parent.height
                                           color: "transparent"
                                       //    border.color: control.palette.mid
                                       }

                                       ScrollIndicator.vertical: ScrollIndicator { }
                                   }

                                   background: Rectangle {
                                       color: control6.pressed ? "#EEEFF7" : control6.palette.window
                                       border.width: 1
                                       border.color: control6.backgroundColor
                                       radius: 3
                                   }
                               }
                           }

CheckBox:

CheckBox {
                id: checkBox4
               // text: qsTr("启用")
                font.pointSize: 10
                checked: setform.nbEn
                x: 23
                y: 50
                width: 120
                height: 30
                spacing: 0
                enabled: true
                indicator:
                Image {
                      id: image4
                      height: 20
                      width: 20
                      anchors.verticalCenter: parent.verticalCenter
                      source: checkBox4.checked ? "/new/prefix1/img/21.jpg" : "/new/prefix1/img/22.jpg"
                }

                contentItem: Text {
                            id: text4
                            x:45
                            y:40
                            width: 30
                            height: 30
                            text: qsTr("启用")//checkBox10.text
                            font.pointSize: 9
                            opacity: enabled ? 1.0 : 0.3
                            color: "black"   //checkBox10.down ? "#AA0000" : "#148014"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            //leftPadding: checkBox.indicator.width + checkBox.spacing
                        }
            }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你实现一个steps控件。你需要先确定一下这个控件的具体需求和样式,比如步骤数量、步骤名称、当前步骤、步骤完成情况等等。以下是一个简单的实现示例,你可以根据自己的需求修改。 首先在QML文件定义一个Steps控件: ```qml import QtQuick 2.12 import QtQuick.Controls 2.12 Item { property int currentStep: 1 property int totalSteps: 3 property alias stepNames: stepList.model width: 200 height: 40 ListView { id: stepList anchors.fill: parent model: ListModel { ListElement { name: "Step 1"; completed: true } ListElement { name: "Step 2"; completed: false } ListElement { name: "Step 3"; completed: false } } delegate: Rectangle { width: parent.width / stepList.count height: parent.height color: index + 1 === currentStep ? "lightblue" : completed ? "green" : "white" border.color: "black" Text { text: name anchors.centerIn: parent } Rectangle { width: parent.width height: 2 color: index + 1 === currentStep ? "lightblue" : completed ? "green" : "white" anchors.bottom: parent.bottom } } } } ``` 这个控件包含一个ListView,每个步骤用一个矩形表示,矩形的颜色根据步骤的完成情况和当前步骤来确定。未完成的步骤用白色表示,已完成的步骤用绿色表示,当前步骤用浅蓝色表示,完成情况和名称从一个ListModel获取。 在使用这个控件时,你可以设置currentStep和stepNames属性来更新当前步骤和步骤名称列表。例如: ```qml Steps { currentStep: 2 totalSteps: 4 stepNames: ["Step 1", "Step 2", "Step 3", "Step 4"] } ``` 这将创建一个有4个步骤的控件,当前步骤为第2步,步骤名称分别为Step 1、Step 2、Step 3、Step 4。 当然,你可以根据自己的需求修改这个控件的样式和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值