Как сделать пользовательский QItemDelegate для конкретной строки в QTreeView? - PullRequest
0 голосов
/ 15 октября 2018

Как сделать кастом 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);
}

ПРОБЛЕМА ЕСТЬ: Как объединить SeparatorLine и QChekBox в один пользовательский элемент?

1 Ответ

0 голосов
/ 15 октября 2018

Идея объединения в этом случае состоит в том, чтобы закрасить, как показано ниже:

#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

...