如何为QTreeView中的特定行创建自定义QItemDelegate?

时间:2018-10-15 09:20:36

标签: c++ qt qt5 qitemdelegate

如何像自定义图片一样制作自定义QItemDelegate。这是QTreeView。我要自定义并添加QItemDelegate enter image description here

的最后一个元素

目前,我只有绿色的separator行,并想在分隔符下方添加一个QCheckBox。如何实现这种行为?

void SeparatorItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
    {
        QPen pen(Qt::green, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
        painter->setPen(pen);
        painter->drawLine(option.rect.left(), option.rect.center().y(), option.rect.right(), option.rect.center().y());
    }
    else
    {
        QItemDelegate::paint(painter, option, index);
    }
}

QSize SeparatorItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
    {
        return QSize(200, 25);
    }
    return QItemDelegate::sizeHint(option, index);
}

void SeparatorItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    editor->setGeometry(option.rect);
}

问题在于: 如何将SeparatorLineQChekBox合并到一个自定义项目中?

1 个答案:

答案 0 :(得分:2)

在这种情况下,联合的想法是重新粉刷,如下所示:

#include <QApplication>
#include <QItemDelegate>
#include <QPainter>
#include <QStandardItemModel>
#include <QTreeView>

class SeparatorItemDelegate: public QItemDelegate
{
public:

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        QItemDelegate::paint(painter, option, index);
        if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
        {

            QPen pen(Qt::green, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
            painter->setPen(pen);
            QLine line(option.rect.topLeft(), option.rect.topRight());
            painter->drawLine(line);
        }
    }

    QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
            return QSize(200, 25);
        return QItemDelegate::sizeHint(option, index);
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTreeView w;
    SeparatorItemDelegate delegate;
    w.setItemDelegate(&delegate);
    QStandardItemModel model;
    for(const QString & root_name: {"Symbols", "Studies", "Drawings", "Unsorted"}){
        QStandardItem *root_item = new QStandardItem(root_name);
        root_item->setData(root_name);
        root_item->setCheckState(Qt::Checked);
        model.appendRow(root_item);
        for(int i=0; i < 3; i++){
            QStandardItem *child_item = new QStandardItem(root_name+QString::number(i));
            root_item->appendRow(child_item);
            child_item->setCheckState(Qt::Checked);
        }
    }
    w.setModel(&model);
    w.expandAll();
    w.resize(240, 480);
    w.show();

    return a.exec();
}

enter image description here