QT 代理

    SpinBoxDelegate例子是Qt Assistant中提供的一个非常优秀的例子,虽然讲的是继承于QItemDelegate的例子。但对于我们理解Delegate-委托这个概念,非常有帮助。

它重载了必须的几个函数:

        (1)  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
       (2)  void setEditorData(QWidget *editor, const QModelIndex &index) const;
       (3)  void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const;
       (4)  void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const;


Main.cpp

 

Cpp代码   收藏代码
  1. #include <QApplication>  
  2. #include <QHeaderView>  
  3. #include <QItemSelectionModel>  
  4. #include <QStandardItemModel>  
  5. #include <QTableView>  
  6.   
  7. #include "delegate.h"  
  8.   
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.     QApplication app(argc, argv);  
  13.   
  14.     //构建一个4行,2列的项模型  
  15.     QStandardItemModel model(4, 2);  
  16.     //声明一个TableView  
  17.     QTableView tableView;  
  18.     //绑定模型  
  19.     tableView.setModel(&model);  
  20.     //声明一个委托  
  21.     SpinBoxDelegate delegate;  
  22.     //设定视图的委托  
  23.     tableView.setItemDelegate(&delegate);  
  24.     //ensuring that the view does not waste any of the space assigned to it for its header  
  25.     //最后一列全部填充View  
  26.     tableView.horizontalHeader()->setStretchLastSection(true);  
  27.   
  28.     //初始化Model  
  29.     for (int row = 0; row < 4; ++row) {  
  30.         for (int column = 0; column < 2; ++column) {  
  31.             QModelIndex index = model.index(row, column, QModelIndex());  
  32.             model.setData(index, QVariant((row+1) * (column+1)));  
  33.         }  
  34.   
  35.     }  
  36.   
  37.   
  38.     tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));  
  39.     tableView.show();  
  40.     return app.exec();  
  41. }  

 

 

 delegate.h

 

Cpp代码   收藏代码
  1. #ifndef DELEGATE_H  
  2. #define DELEGATE_H  
  3.   
  4. #include <QItemDelegate>  
  5. #include <QModelIndex>  
  6. #include <QObject>  
  7. #include <QSize>  
  8. #include <QSpinBox>  
  9.   
  10.   
  11. class SpinBoxDelegate : public QItemDelegate  
  12. {  
  13.     Q_OBJECT  
  14.   
  15. public:  
  16.     SpinBoxDelegate(QObject *parent = 0);  
  17.   
  18.     //返回一个编辑控件,用来编辑指定项的数据  
  19.     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
  20.                           const QModelIndex &index) const;  
  21.     //将Model中数据赋值到控件上  
  22.     void setEditorData(QWidget *editor, const QModelIndex &index) const;  
  23.     //设定模型数据,根据指定项中对应编辑控件的数据  
  24.     void setModelData(QWidget *editor, QAbstractItemModel *model,  
  25.                       const QModelIndex &index) const;  
  26.     //更新编辑框几何形状  
  27.     void updateEditorGeometry(QWidget *editor,  
  28.         const QStyleOptionViewItem &option, const QModelIndex &index) const;  
  29. };  
  30.   
  31.   
  32. #endif  

 

 

 delegate.cpp

 

Cpp代码   收藏代码
  1. #include <QtGui>  
  2.   
  3. #include "delegate.h"  
  4.   
  5.   
  6. SpinBoxDelegate::SpinBoxDelegate(QObject *parent)  
  7.     : QItemDelegate(parent)  
  8. {  
  9. }  
  10.   
  11. //返回一个编辑控件,用来编辑指定项的数据(用户双击时调用此函数,创建对应控件) 
  12. QWidget *SpinBoxDelegate::createEditor(QWidget *parent,  
  13.     const QStyleOptionViewItem &/* option */,  
  14.     const QModelIndex &/* index */const  
  15. {  
  16.     //返回该QSpinBox控件  
  17.     QSpinBox *editor = new QSpinBox(parent);  
  18.     editor->setMinimum(0);  
  19.     editor->setMaximum(100);  
  20.   
  21.     return editor;  
  22. }  
  23. //将Model中数据赋值到控件上 (双击时调用此函数,控件刚显示时对应的数据)
  24. void SpinBoxDelegate::setEditorData(QWidget *editor,  
  25.                                     const QModelIndex &index) const  
  26. {  
  27.     //返回该索引的模型,继而返回该模型中此索引的编辑角色数据  
  28.     int value = index.model()->data(index, Qt::EditRole).toInt();  
  29.     //给控件赋值  
  30.     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
  31.     spinBox->setValue(value);  
  32. }  
  33. //设定模型数据,根据指定项中对应编辑控件的数据 (控件隐藏时调用,主要用于回显数据,提供数据给model,用户可以看见)
  34. void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,  
  35.                                    const QModelIndex &index) const  
  36. {  
  37.     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
  38.     spinBox->interpretText();  
  39.     int value = spinBox->value();  
  40.     //设置模型的数据  
  41.     model->setData(index, value, Qt::EditRole);  
  42. }  
  43. //更新编辑框几何形状  
  44. void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,  
  45.     const QStyleOptionViewItem &option, const QModelIndex &/* index */const  
  46. {  
  47.     //根据option,设置编辑框位置  
  48.     editor->setGeometry(option.rect);  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值