Qt小例子学习76 - QStyledItemDelegate 一行显示两小部件

Qt小例子学习76 - QStyledItemDelegate 一行显示两小部件

#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLineEdit>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTreeView>

enum CustomRoles { SelectRole = Qt::UserRole };

class EditorWidget : public QWidget
{
    Q_OBJECT
public:
    EditorWidget(QWidget *parent = nullptr)
        : QWidget(parent), checkBox(new QCheckBox), lineBox(new QLineEdit),
          comboBox(new QComboBox)
    {
        QHBoxLayout *layout = new QHBoxLayout(this);
        comboBox->addItems({"item1", "item2", "item3"});
        layout->addWidget(checkBox);
        layout->addWidget(lineBox);
        layout->addWidget(comboBox);
    }

    int currentIndex() { return comboBox->currentIndex(); }
    void setCurrentIndex(int index) { comboBox->setCurrentIndex(index); }
    QString text() const { return lineBox->text(); }
    void setText(const QString &text) { lineBox->setText(text); }
    Qt::CheckState checkState() const { return checkBox->checkState(); }
    void setCheckState(Qt::CheckState state) { checkBox->setCheckState(state); }

private:
    QCheckBox *checkBox;
    QLineEdit *lineBox;
    QComboBox *comboBox;
};

class Delegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const
    {
        Q_UNUSED(painter)
        Q_UNUSED(option)
        Q_UNUSED(index)
    }
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
    {
        Q_UNUSED(option)
        Q_UNUSED(index)
        EditorWidget *editor = new EditorWidget(parent);
        return editor;
    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        EditorWidget *widget = static_cast<EditorWidget *>(editor);
        widget->blockSignals(true);
        widget->setText(index.data(Qt::DisplayRole).toString());
        Qt::CheckState state =
            static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
        widget->setCheckState(state);
        widget->setCurrentIndex(index.data(CustomRoles::SelectRole).toInt());
        widget->blockSignals(false);
    }
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const
    {
        EditorWidget *widget = static_cast<EditorWidget *>(editor);
        model->setData(index, widget->text(), Qt::DisplayRole);
        model->setData(index, widget->checkState(), Qt::CheckStateRole);
        model->setData(index, widget->sizeHint(), Qt::SizeHintRole);
        model->setData(index, widget->currentIndex(), CustomRoles::SelectRole);
    }
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const
    {
        Q_UNUSED(index)
        editor->setGeometry(option.rect);
    }
    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
    {
        QSize s = index.data(Qt::SizeHintRole).toSize();
        return s.isValid() ? s : QStyledItemDelegate::sizeHint(option, index);
    }
};

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

    QTreeView treeView;
    QStandardItemModel model(4, 2);
    treeView.setModel(&model);

    Delegate delegate;
    treeView.setItemDelegate(&delegate);

    treeView.header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    treeView.setRootIsDecorated(false);
    treeView.setHeaderHidden(true);
    treeView.setIndentation(20);
    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)));
            treeView.openPersistentEditor(index);
        }
    }
    treeView.resize(treeView.sizeHint());
    treeView.show();

    return a.exec();
}

#include "main.moc"

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值