查看完整版本: [-- 想问一下,在listview里怎么实现checkbox的全选和反选 --]

QTCN开发网 -> Qt QML开发 -> 想问一下,在listview里怎么实现checkbox的全选和反选 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

扁桃体也发言 2019-09-11 15:22

想问一下,在listview里怎么实现checkbox的全选和反选

用qml实现

XChinux 2019-09-11 15:58
没好方法自己实现呗。很简单的。

20091001753 2019-09-11 18:06
  1. ui->listWidget->setItemWidget( new QListWidgetItem(ui->listWidget) , new QCheckBox(u8"哈哈") );


扁桃体也发言 2019-09-12 08:22
ListView   {              
       id: dataItemView              
       y: rect.y + rect.height                
       width: parent.width                
      eight: parent.height * 0.75              
      model: ListModel{                    
                 id: dataItemModel                    
                 ListElement{number:"ID"}                
      }              
      delegate: DataItemDelegate{}                
}
在另一个qml文件:DataItemDelegate.qml
ItemDelegate{    
        id: dataItemDelegate    
         width: parent.width  
         height: 30  
         background: Rectangle{      
                color: getBackgroundColor()  
        }
       function getBackgroundColor(){        
              if(index === 0){          
                   return "#082567"        
              }      
              else if(index % 2 === 0){            
                     return "#cecece"      
             }        
            else if(index % 2 === 1) {          
                   return "#f9f9f9"        
             }  
       }
       contentItem: ColumnLayout{        
             anchors.fill: parent          
             CheckBox{              
                   id: numberCheckBox              
                    x: dataItemDelegate.x                
                   width: dataItemDelegate.width/8                
                   display: AbstractButton.IconOnly                
                   indicator: Rectangle{                    
                           width: 12                  
                           height: 12                    
                           color: "#b7b7b7"                    
                           radius: 2                  
                           anchors.verticalCenter: parent.verticalCenter                  
                           anchors.left: parent.left                    
                           anchors.leftMargin: 5                    
                          Image {                        
                                    anchors.verticalCenter: parent.verticalCenter                        
                                     anchors.horizontalCenter: parent.horizontalCenter                        
                                     source: numberCheckBox.checked ? "qrc:/images/select.png" : ""                  
                          }              
                  }                
                  onClicked:{                  
                                if(index === 0){                
                                         在这里实现点击checkbox,全选和反选的功能,我现在可以获取点击的行数,但是不知道怎么用这个行数找到chexkbox ,然                                  后  通过轮训修改checkbox的checked属性                    
                               }              
               }              
              Text{                  
                      id: numberText                  
                      text: model.number                    
                     color: (index === 0) ? "white":"black"                  
                     font.pointSize: 12                  
                       anchors.left: parent.left                  
                     anchors.leftMargin: 30                  
                       anchors.verticalCenter: parent.verticalCenter                
                       clip: true              
                      }        
}



扁桃体也发言 2019-09-12 16:09
已解决
listmodel里增加一个状态信息
model: ListModel{                    
                 id: dataItemModel                    
                 ListElement{statu:false,number:"ID"}                
      }
在checkbox里面把checked属性设置成model.statu,然后再点击事件里面轮训 listmodel,使用setProperty函数修改statu的值


CheckBox{              
                   id: numberCheckBox              
                    x: dataItemDelegate.x                
                   width: dataItemDelegate.width/8                
                   display: AbstractButton.IconOnly        
                   checked:model.statu      
                   indicator: Rectangle{                    
                           width: 12                  
                           height: 12                    
                           color: "#b7b7b7"                    
                           radius: 2                  
                           anchors.verticalCenter: parent.verticalCenter                  
                           anchors.left: parent.left                    
                           anchors.leftMargin: 5                    
                          Image {                        
                                    anchors.verticalCenter: parent.verticalCenter                        
                                     anchors.horizontalCenter: parent.horizontalCenter                        
                                     source: numberCheckBox.checked ? "qrc:/images/select.png" : ""                  
                          }              
                  }                
                  onClicked:{                  
                                if(index === 0){                
                                         for(i = 1;i < dataItemView.count;i++){                            
                                                    dataItemModel.setProperty(i,"statu",numberCheckBox.checked)                
                                         }              
                                }              
               }      


查看完整版本: [-- 想问一下,在listview里怎么实现checkbox的全选和反选 --] [-- top --]



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