• 4283阅读
  • 4回复

[提问]如何跟据C++信号动态改变qml 中Listview的一个或几个delegate的颜色 [复制链接]

上一主题 下一主题
离线zjq19930322
 

只看楼主 倒序阅读 楼主  发表于: 2017-08-29
  1. ListView {
  2.                     id: bTList
  3.                     width: 50 * dpi
  4.                     height: 40 * dpi
  5.                     currentIndex: -1
  6.                     anchors.left: parent.left
  7.                     anchors.leftMargin: 3 * dpi
  8.                     anchors.top: blueToothServicecolumn.top
  9.                     anchors.topMargin: 6 * dpi
  10.                     clip: true
  11.                     spacing: 2 * dpi
  12.                     model: device.devicesList
  13.                     focus: true
  14.                     delegate: Rectangle {
  15.                         id: delegate
  16.                         property string temperagunconnected: device.TemperagunConnected
  17.                         property string bloodressureconnected: device.BloodpressureConnected
  18.                         property string bdoxygenconnected: device.BDoxygenConnected
  19.                         property string bdsugarconnected: device.BDsugarConnected
  20.                         color: ListView.isCurrentItem ? "lightblue" : "green" // directly change properties depending on isCurrentItem
  21.                         width: 50 * dpi
  22.                         height: 8 * dpi
  23.                         states: State {
  24.                             name: "maximized"
  25.                             when: delegate.ListView.isCurrentItem // bind to isCurrentItem to set the state
  26.                             PropertyChanges {
  27.                                 target: delegate
  28.                                 height: 200
  29.                             }
  30.                         }
  31.                         MouseArea {
  32.                             anchors.fill: parent
  33.                             //onClicked: delegate.ListView.view.currentIndex = model.index // if only selection is wanted
  34.                             onClicked: {
  35.                                 //console.debug("click");
  36.                                 device.bindservices(modelData.deviceName, modelData.deviceAddress);
  37.                             }
  38.                         }
  39.                         Image {
  40.                             id: bticon
  41.                             source: "images/BT.png";
  42.                             width: btNameText.height;
  43.                             height: btNameText.height;
  44.                             anchors.top: parent.top
  45.                             anchors.left: parent.left
  46.                             anchors.margins: 0.3 * dpi
  47.                         }
  48.                         Text {
  49.                             id: btNameText
  50.                             text: modelData.deviceName
  51.                             anchors.left: bticon.right
  52.                             anchors.leftMargin: 0.3 * dpi
  53.                             //color: white
  54.                             font.family: "微软雅黑"
  55.                             font.pointSize: 20
  56.                         }
  57. }
  58. }

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

只看该作者 1楼 发表于: 2017-08-30
你想通过信号改变 delegate 的颜色,可以试试把颜色绑定到 model 里面,然后通过 C++ 信号来触发 model 值的改变,这样可以实现,可能也有其他的方法....
离线zjq19930322

只看该作者 2楼 发表于: 2017-08-31
回 never_forget 的帖子
never_forget:你想通过信号改变 delegate 的颜色,可以试试把颜色绑定到 model 里面,然后通过 C++ 信号来触发 model 值的改变,这样可以实现,可能也有其他的方法.... (2017-08-30 08:26) 

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

只看该作者 3楼 发表于: 2017-09-04
回 zjq19930322 的帖子
zjq19930322:请教一下如果是上面给的代码,把颜色绑定到 model 里面,要怎么绑定? (2017-08-31 09:41)

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

只看该作者 4楼 发表于: 2017-09-05
回 never_forget 的帖子
never_forget:你的 model 里面有哪些数据项,添加一个 string 类型的数据项,数据为颜色数据类型 #333333;
然后将这个属性绑定到你的 delegate 的顶层 Rectangle 的背景色 color 属性!
 (2017-09-04 08:39) 

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

我没试过你这种方法,但是觉得应该会一改全改,我只需要根据条件改其中一项
快速回复
限100 字节
 
上一个 下一个