【发布时间】:2013-11-16 17:22:07
【问题描述】:
我需要在QTreeView 中显示来自QFileSystemModel 的文件并自定义该树以使用QCheckBox 再显示一列,因此用户可以从该QTreeView 中选择0..N 个文件。
我从 Qt 阅读了文档以了解模型/视图架构,现在我在我的代码中,我有自定义委托 CustomItemDelegatefor 特定列,但实际上我不知道如何在绘制方法中创建 QCheckBox我的自定义委托(更具体地说,我知道如何,但这是 99% 的坏方法)。
customitemdelegate.h
#ifndef CUSTOMITEMDELEGATE_H
#define CUSTOMITEMDELEGATE_H
#include <QStyledItemDelegate>
class CustomItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit CustomItemDelegate(QObject *parent = 0);
void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
signals:
public slots:
};
#endif // CUSTOMITEMDELEGATE_H
customitemdelegate.cpp
#include "customitemdelegate.h"
#include <QCheckBox>
#include <iostream>
#include <QTreeView>
using namespace std;
CustomItemDelegate::CustomItemDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void CustomItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
((QTreeView *)parent())->setIndexWidget(index, new QCheckBox());
}
【问题讨论】: