qt delegate 委托 多级联动 小结 .

最近使用qt写程序用到了treewidget和tablewidget

涉及到编辑默认情况下可以设置treewidget和tablewidget的item可编辑属性来进行编辑

默认情况只是简单的文本输入格式

在qt的demo中看到了spin box delegate实例

 

想到了使用delegate 委托

该实例只是对整个tablewidget使用同一个spin box格式的delegata

那么对于不同的列时候不通格式的委托那?

 

 
 
  1. QWidget *EventsDelegate::createEditor(QWidget *parent,  
  2.     const QStyleOptionViewItem & option ,  
  3.     const QModelIndex & index ) const  
  4. {  
  5.    if(index.column() == 0)  
  6.     {  
  7.         QComboBox *editor;  
  8.         editor = new QComboBox(parent);      
  9.     }else if(index.column() == 1)  
  10.     {  
  11.         QSpinBox *editor;  
  12.          editor = new QSpinBox(parent);      
  13.     }    
  14.     
  15.     return editor;  
  16. }  
这样就可以通过判断不同的列使用不同的委托,当然对于不同的行,或者是特定的某一行某一列使用的委托都可以控制
进一步的想法就是实现一行中不同列的级联
类似php或者其他语言中中经常用到的
思路起始很清晰,两个列都是使用QComboBox,只是如何在同一行的前一列选中后,根据选中的数据初始化后一列的数据选项
起始说白了就是如何获取前一列的数据了
上网搜索了好长时间,从中有所启示
最后找到了这个函数
index.model()->index(index.row(),index.column() -1);
 
所以最后的代码就是:
 

 
 
  1. QWidget *EventsDelegate::createEditor(QWidget *parent,  
  2.    const QStyleOptionViewItem & option ,  
  3.    const QModelIndex & index ) const  
  4.      
  5.    QComboBox *editor;  
  6.    editor = new QComboBox(parent);  
  7.   
  8.    if(index.column() == 0)  
  9.    {  
  10.        //控件列   
  11.         //初始化下拉框数据   
  12.         ..........  
  13.     }else if(index.column() == 1)  
  14.    {  
  15.        //获取同一行前一列的数据,根据数据指定该index的数据   
  16.        QModelIndex brothModelIndex  = index.model()->index(index.row(),index.column() -1);  
  17.        QString str = brothModelIndex.data().toString();  
  18.           
  19.        //根据str初始化本列的下拉框数据项   
  20.         .........  
  21.    }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值