Qt-QML-ComboBox-自定义,实现状态表示,内容可以动态正价,使用ListModel

简介: Qt-QML-ComboBox-自定义,实现状态表示,内容可以动态正价,使用ListModel

哎呀呀呀,

问:杀死一个程序员一个程序要需要进步?

答:改三次需求

 

我感觉我就要再这需求的变更中被杀死了。不管怎么说,总是要跟着需求走的的,客户才是第一么(要不是因为钱,我才不会了)

 

下面先上个效果image.png

如图中显示一样,我需要再再这个Item前面用来显示一个我的当前设备的状态,这里的状态是有底层数据提供,这里试试做出相应的显示,


绿色标识可以设备可用,而红色表示设备故障,不能使用。


找啊找啊,最好还是找到了Qt的帮助文档,这里有自定义的ComboBox的Demo


先吧人家的源代码贴上来



import QtQuick 2.6

 import QtQuick.Controls 2.1


 ComboBox {

     id: control

     model: ["First", "Second", "Third"]


     delegate: ItemDelegate {

         width: control.width

         contentItem: Text {

             text: modelData

             color: "#21be2b"

             font: control.font

             elide: Text.ElideRight

             verticalAlignment: Text.AlignVCenter

         }

         highlighted: control.highlightedIndex == index

     }


     indicator: Canvas {

         id: canvas

         x: control.width - width - control.rightPadding

         y: control.topPadding + (control.availableHeight - height) / 2

         width: 12

         height: 8

         contextType: "2d"


         Connections {

             target: control

             onPressedChanged: canvas.requestPaint()

         }


         onPaint: {

             context.reset();

             context.moveTo(0, 0);

             context.lineTo(width, 0);

             context.lineTo(width / 2, height);

             context.closePath();

             context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";

             context.fill();

         }

     }


     contentItem: Text {

         leftPadding: 0

         rightPadding: control.indicator.width + control.spacing


         text: control.displayText

         font: control.font

         color: control.pressed ? "#17a81a" : "#21be2b"

         horizontalAlignment: Text.AlignLeft

         verticalAlignment: Text.AlignVCenter

         elide: Text.ElideRight

     }


     background: Rectangle {

         implicitWidth: 120

         implicitHeight: 40

         border.color: control.pressed ? "#17a81a" : "#21be2b"

         border.width: control.visualFocus ? 2 : 1

         radius: 2

     }


     popup: Popup {

         y: control.height - 1

         width: control.width

         implicitHeight: listview.contentHeight

         padding: 1


         contentItem: ListView {

             id: listview

             clip: true

             model: control.popup.visible ? control.delegateModel : null

             currentIndex: control.highlightedIndex


             ScrollIndicator.vertical: ScrollIndicator { }

         }


         background: Rectangle {

             border.color: "#21be2b"

             radius: 2

         }

     }

 }




这里了,大家可以看到,这里的model就是一个简单的ListModel,这里我所卡住的难点就是这个“modelData”


这个之所以困扰我,就是因为再这里,我是没法当我自己定义自己的数据类型的时候,我第一个名字开始是没有想到MOdedata,我所想到的是吧text的内容设置成为这个内容,但是了,由于自己现在对QMl还是不怎么深入了解,所以也没法重新高太底层的,所以,好烦啊。



看到网友的启发,说默认的QStringList咋QLisview中,默认的键值就是modelData。所以,小脑瓜灵机一抖,把我的ListModel第一个参数就设置成为modelData,哈哈哈,是不是很聪明,就是这样,就可以实现我默认的功能了,而生下的功能就是自己加的的,后面就好实现了,当然,好实现是我心想的,但是实现起来,坑是必然的,所以我要接着去挖坑,天坑。。。。。。



下面附上我的自己的代码



import QtQuick 2.6

 import QtQuick.Controls 2.1


 ComboBox {

     id: control

//      model: ["First", "Second", "Third"]

     delegate: ItemDelegate {

         width: control.width

         contentItem: Rectangle

         {

             color:"transparent"

             Row

             {

                 spacing: 10

                 Rectangle

                 {

                     width: parent.height

                     height: parent.height

                     radius: parent.height/2

                     color:

                     {

                         if(model.value == "true")

                         {

                             "#00FF00"

                         }

                         else if(model.value == "false")

                         {

                             "#FF0000"

                         }

                     }

                 }


                 Text {

                 id:myText

                 text: modelData

                 color: "#21be2b"

                 font: control.font

                 elide: Text.ElideRight

                 verticalAlignment: Text.AlignVCenter

             }

             }

         }





         highlighted: control.highlightedIndex == index

     }


     indicator: Canvas {

         id: canvas

         x: control.width - width - control.rightPadding

         y: control.topPadding + (control.availableHeight - height) / 2

         width: 12

         height: 8

         contextType: "2d"


         Connections {

             target: control

             onPressedChanged: canvas.requestPaint()

         }


         onPaint: {

             context.reset();

             context.moveTo(0, 0);

             context.lineTo(width, 0);

             context.lineTo(width / 2, height);

             context.closePath();

             context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";

             context.fill();

         }

     }


     contentItem: Text {

         leftPadding: 0

         rightPadding: control.indicator.width + control.spacing


         text: control.displayText

         font: control.font

         color: control.pressed ? "#17a81a" : "#21be2b"

         horizontalAlignment: Text.AlignLeft

         verticalAlignment: Text.AlignVCenter

         elide: Text.ElideRight

     }


     background: Rectangle {

         implicitWidth: 120

         implicitHeight: 40

         border.color: control.pressed ? "#17a81a" : "#21be2b"

         border.width: control.visualFocus ? 2 : 1

         radius: 2

     }


     popup: Popup {

         y: control.height - 1

         width: control.width

         implicitHeight: listview.contentHeight

         padding: 1


         contentItem: ListView {

             id: listview

             clip: true

             model: control.popup.visible ? control.delegateModel : null

             currentIndex: control.highlightedIndex


             ScrollIndicator.vertical: ScrollIndicator { }

         }


         background: Rectangle {

             border.color: "#21be2b"

             radius: 2

         }

     }

 }


调用部分





DLComboBox

   {

       x: 100

       y:100

       width: 200

       height: 20

       model: listmodel


   }



   ListModel

   {

       id:listmodel

       ListElement{modelData:"xxx";value:"true"}

       ListElement{modelData:"sss";value:"false"}

       ListElement{modelData:"ddd";value:"true"}

       ListElement{modelData:"fff";value:"false"}

       ListElement{modelData:"ggg";value:"true"}

       ListElement{modelData:"hhh";value:"false"}

   }


目录
相关文章
|
2月前
|
存储 机器学习/深度学习 人工智能
Qt魔法书:打造自定义鼠标键盘脚本(二)
Qt魔法书:打造自定义鼠标键盘脚本
37 0
|
4月前
QT自定义信号,信号emit,信号参数注册
使用signals声明返回值是void在需要发送信号的地方使用emit 信号名字(参数)进行发送在需要链接的地方使用connect进行链接ct进行链接。
19 0
QT自定义信号,信号emit,信号参数注册
|
4月前
Qt提升控件类为自定义类
Qt提升控件类为自定义类
|
5月前
|
搜索推荐 C++ 索引
C++ Qt开发:QItemDelegate自定义代理组件
在Qt中,`QStyledItemDelegate` 类是用于创建自定义表格视图(如`QTableView`和`QTableWidget`)的委托类,允许你自定义表格中每个单元格的外观和交互。`QStyledItemDelegate` 是`QItemDelegate` 的子类,提供了更现代、更易用的接口。此处我们将实现对`QTableView`表格组件的自定义代理功能,例如默认情况下表格中的缺省代理就是一个编辑框,我们只能够在编辑框内输入数据,而有时我们想选择数据而不是输入,此时就需要重写编辑框实现选择的效果,代理组件常用于个性化定制表格中的字段类型。
41 0
C++ Qt开发:QItemDelegate自定义代理组件
|
6月前
10 QT - 自定义信号和槽
10 QT - 自定义信号和槽
35 0
|
API 计算机视觉
Qt实用技巧:自定义窗口标题栏
Qt实用技巧:自定义窗口标题栏
Qt实用技巧:自定义窗口标题栏
|
4月前
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
44 0
|
2月前
|
开发框架 Linux API
Qt魔法书:打造自定义鼠标键盘脚本(一)
Qt魔法书:打造自定义鼠标键盘脚本
25 0
|
2月前
使用代码实现QT自定义布局
使用代码实现QT自定义布局
|
4月前
Qt6自定义QML控件的方式
Qt6自定义QML控件的方式
76 1

相关实验场景

更多

推荐镜像

更多