• 138阅读
  • 4回复

[提问]想问一下,在listview里怎么实现checkbox的全选和反选 [复制链接]

上一主题 下一主题
 

只看楼主 倒序阅读 楼主  发表于: 09-11
qml实现
离线XChinux

只看该作者 1楼 发表于: 09-11
没好方法自己实现呗。很简单的。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
在线20091001753

只看该作者 2楼 发表于: 09-11
  1. ui->listWidget->setItemWidget( new QListWidgetItem(ui->listWidget) , new QCheckBox(u8"哈哈") );

(づ ̄ 3 ̄)づ

只看该作者 3楼 发表于: 09-12
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              
                      }        
}



只看该作者 4楼 发表于: 09-12
已解决
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)                
                                         }              
                                }              
               }      
快速回复
限100 字节
 
上一个 下一个