查看完整版本: [-- 如何跟据C++信号动态改变qml 中Listview的一个或几个delegate的颜色 --]

QTCN开发网 -> Qt嵌入式开发 -> 如何跟据C++信号动态改变qml 中Listview的一个或几个delegate的颜色 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

zjq19930322 2017-08-29 11:16

如何跟据C++信号动态改变qml 中Listview的一个或几个delegate的颜色

  1. ListView {
                        id: bTList
                        width: 50 * dpi
                        height: 40 * dpi
                        currentIndex: -1
                        anchors.left: parent.left
                        anchors.leftMargin: 3 * dpi
                        anchors.top: blueToothServicecolumn.top
                        anchors.topMargin: 6 * dpi
                        clip: true
                        spacing: 2 * dpi
                        model: device.devicesList
                        focus: true

                        delegate: Rectangle {
                            id: delegate
                            property string temperagunconnected: device.TemperagunConnected
                            property string bloodressureconnected: device.BloodpressureConnected
                            property string bdoxygenconnected: device.BDoxygenConnected
                            property string bdsugarconnected: device.BDsugarConnected
                            color: ListView.isCurrentItem ? "lightblue" : "green" // directly change properties depending on isCurrentItem
                            width: 50 * dpi
                            height: 8 * dpi

                            states: State {
                                name: "maximized"
                                when: delegate.ListView.isCurrentItem // bind to isCurrentItem to set the state
                                PropertyChanges {
                                    target: delegate
                                    height: 200
                                }
                            }
                            MouseArea {
                                anchors.fill: parent
                                //onClicked: delegate.ListView.view.currentIndex = model.index // if only selection is wanted
                                onClicked: {
                                    //console.debug("click");
                                    device.bindservices(modelData.deviceName, modelData.deviceAddress);

                                }
                            }
                            Image {
                                id: bticon
                                source: "images/BT.png";
                                width: btNameText.height;
                                height: btNameText.height;
                                anchors.top: parent.top
                                anchors.left: parent.left
                                anchors.margins: 0.3 * dpi
                            }
                            Text {
                                id: btNameText
                                text: modelData.deviceName
                                anchors.left: bticon.right
                                anchors.leftMargin: 0.3 * dpi
                                //color: white
                                font.family: "微软雅黑"
                                font.pointSize: 20
                            }
    }
    }

ListView的内容如上,delegate是由一个rectangle实现的

never_forget 2017-08-30 08:26
你想通过信号改变 delegate 的颜色,可以试试把颜色绑定到 model 里面,然后通过 C++ 信号来触发 model 值的改变,这样可以实现,可能也有其他的方法....

zjq19930322 2017-08-31 09:41
never_forget:你想通过信号改变 delegate 的颜色,可以试试把颜色绑定到 model 里面,然后通过 C++ 信号来触发 model 值的改变,这样可以实现,可能也有其他的方法.... (2017-08-30 08:26) 

请教一下如果是上面给的代码,把颜色绑定到 model 里面,要怎么绑定?

never_forget 2017-09-04 08:39
zjq19930322:请教一下如果是上面给的代码,把颜色绑定到 model 里面,要怎么绑定? (2017-08-31 09:41)

你的 model 里面有哪些数据项,添加一个 string 类型的数据项,数据为颜色数据类型 #333333;
然后将这个属性绑定到你的 delegate 的顶层 Rectangle 的背景色 color 属性!

zjq19930322 2017-09-05 21:57
never_forget:你的 model 里面有哪些数据项,添加一个 string 类型的数据项,数据为颜色数据类型 #333333;
然后将这个属性绑定到你的 delegate 的顶层 Rectangle 的背景色 color 属性!
 (2017-09-04 08:39) 

用了另外一种方法实现了,QtCreator 不补全我还以为敲错了,事实证明这个IDE还是有缺陷的,不能完全信它。

我没试过你这种方法,但是觉得应该会一改全改,我只需要根据条件改其中一项


查看完整版本: [-- 如何跟据C++信号动态改变qml 中Listview的一个或几个delegate的颜色 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled