`
qimo601
  • 浏览: 3415908 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

基于QItemDelegate的例子1 SpinBoxDelegate

    博客分类:
  • 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

 

#include <QApplication>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>

#include "delegate.h"


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    //构建一个4行,2列的项模型
    QStandardItemModel model(4, 2);
    //声明一个TableView
    QTableView tableView;
    //绑定模型
    tableView.setModel(&model);
    //声明一个委托
    SpinBoxDelegate delegate;
    //设定视图的委托
    tableView.setItemDelegate(&delegate);
    //ensuring that the view does not waste any of the space assigned to it for its header
    //最后一列全部填充View
    tableView.horizontalHeader()->setStretchLastSection(true);

    //初始化Model
    for (int row = 0; row < 4; ++row) {
        for (int column = 0; column < 2; ++column) {
            QModelIndex index = model.index(row, column, QModelIndex());
            model.setData(index, QVariant((row+1) * (column+1)));
        }

    }


    tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
    tableView.show();
    return app.exec();
}

 

 

 delegate.h

 

#ifndef DELEGATE_H
#define DELEGATE_H

#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>


class SpinBoxDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    SpinBoxDelegate(QObject *parent = 0);

    //返回一个编辑控件,用来编辑指定项的数据
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const;
    //将Model中数据赋值到控件上
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    //设定模型数据,根据指定项中对应编辑控件的数据
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const;
    //更新编辑框几何形状
    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const;
};


#endif

 

 

 delegate.cpp

 

#include <QtGui>

#include "delegate.h"


SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}

//返回一个编辑控件,用来编辑指定项的数据
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    //返回该QSpinBox控件
    QSpinBox *editor = new QSpinBox(parent);
    editor->setMinimum(0);
    editor->setMaximum(100);

    return editor;
}
//将Model中数据赋值到控件上
void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    //返回该索引的模型,继而返回该模型中此索引的编辑角色数据
    int value = index.model()->data(index, Qt::EditRole).toInt();
    //给控件赋值
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}
//设定模型数据,根据指定项中对应编辑控件的数据
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();
    //设置模型的数据
    model->setData(index, value, Qt::EditRole);
}
//更新编辑框几何形状
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    //根据option,设置编辑框位置
    editor->setGeometry(option.rect);
}
2
0
分享到:
评论
4 楼 sonichy 2017-06-27  
Qt5改动很多,要改改了。
3 楼 少卿名宁 2015-06-23  
2 楼 December_Lin 2014-06-20  
如果里面是checkbox控件呢?不用重载paint函数,写完之后发现只有点击的时候才会显示出checkbox控件
1 楼 marsz1990 2012-12-13  
不错 多谢解说

相关推荐

    基于QItemDelegate的例子2 trackeEditorDelegate

    NULL 博文链接:https://qimo601.iteye.com/blog/1536464

    Qt开发的通讯录小程序。

    学习QT时,参照网上的例子完成的小程序,主要是练习之用,不当之处在所难免。希望对初学者有帮助。 关联技术: 1.SQLite数据库的连接,建库,建表 2.QSqlTableModel / QDataWidgetMapper / QItemDelegate等类的使用 ...

    CustomDelegate.rar

    QT中delegate自定义委托的小例子,有注释说明和源码 简单基础部件的委托可以继承QItemDelegate,并使用这些函数的默认实现,委托编辑器可以通过使用小工具来管理编辑过程或直接处理事件来实现。 使用Delegate的原因...

    tableView.zip

    虽然为了这个目的我们设置了一个自定义的基于整数的表模型,我们可以很容易地使用QStandardItemModel来代替,因为自定义委托控制数据输入。我们构造了一个表视图来显示模型的内容,可以使用自定义的委托来进行编辑。

    TableViewDelegate.zip

    《C++ Qt开发:QItemDelegate自定义代理组件》文章课件

    自定义委托

    自定义委托类,重写QItemDelegate中的5个函数并用于模型实例.

    QTableView 中单元格添加控件的实例代码

    QTableView 中单元格添加控件的实例代码

    Delegates.7z

    QT中代理Delegates使用实例代码,可运行,多种编辑方式,有下列框,日期、QSpinBox,界面显示一看就懂!代码无buge,如果对你有帮助请给好评,有问题可以私信联系我

Global site tag (gtag.js) - Google Analytics