• Qt的复杂代理使用总结


    Qt的复杂代理使用总结

    代理的基本使用

    一般代理只需要继承QStyledItemDelegate类,然后重写createEditor,setEditorData和setModelData接口即可。

    // COpratorDelegate.h
    class COpratorDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        explicit COpratorDelegate(QObject *parent = nullptr, const ColType &type=STRING);
        QWidget *createEditor(QWidget *parent,  const QStyleOptionViewItem &option,
                                       const QModelIndex &index) const override;
        void setEditorData(QWidget *editor, const QModelIndex &index) const override;
        void setModelData(QWidget *editor,  QAbstractItemModel *model,
                                  const QModelIndex &index) const override;
    private:
        CModel *_pModel = nullptr;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在createEditor中创建控件,然后返回,在setEditorData中从模型中获取数据,然后填充到控件中,setModelData从控件中获取数据设置到model中。

    在view中使用代理,如下:

    COpratorDelegate *pCOD = new COpratorDelegate(_pTableView, COpratorDelegate::MULBTN);
    _pTableView->setItemDelegateForColumn(1, pCOD);
    
    • 1
    • 2

    在代理中使用复杂控件

    在代理中使用复杂控件,比如使用布局添加多个控件,如下:

    // 创建一个多按键的QWidget页面, MulBtnWiget.h
    class MulBtnWiget : public QWidget
    {
        Q_OBJECT
    public:
        explicit MulBtnWiget(const QModelIndex &index, QWidget *parent = nullptr);
    
        void setBtnData(const QVariant &data);
    
    signals:
        void btnColor(const QString &color, const int &row);
    
    private:
        QPushButton *_pButton1;
        QPushButton *_pButton2;
        QPushButton *_pButton3;
        QModelIndex _index;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    此页面包含三个按钮,分别为红色,绿色和重置,用来改变颜色,如下:

    MulBtnWiget::MulBtnWiget(const QModelIndex &index, QWidget *parent/* = nullptr*/)
        : QWidget(parent), _index(index)
    {
        QHBoxLayout *pLayout = new QHBoxLayout();
        _pButton1 = new QPushButton("red");
        pLayout->addWidget(_pButton1);
        _pButton2 = new QPushButton("green");
        pLayout->addWidget(_pButton2);
        _pButton3 = new QPushButton("reset");
        pLayout->addWidget(_pButton3);
        pLayout->setContentsMargins(0, 0, 0, 0);
        this->setLayout(pLayout);
        this->setContentsMargins(0, 0, 0, 0);
        _pButton1->setEnabled(false);
        _pButton2->setEnabled(false);
        _pButton3->setEnabled(false);
        _pButton1->setStyleSheet("color: red;");
        _pButton2->setStyleSheet("color: green;");
        _pButton3->setStyleSheet("color: black;");
    
        connect(_pButton1, &QPushButton::clicked, this, [this](){
            emit btnColor("red", _index.row());
        });
        connect(_pButton2, &QPushButton::clicked, this, [this](){
            emit btnColor("green", _index.row());
        });
        connect(_pButton3, &QPushButton::clicked, this, [this](){
            emit btnColor("black", _index.row());
        });
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    创建了这个页面之后,可以使用这个页面作为代理,使用的方式非常简单,直接new出来,然后返回即可:

    QWidget *COpratorDelegate::createEditor(QWidget *parent,
                                            const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const
    {
        MulBtnWiget *pMulBtnWidget = new MulBtnWiget(index, parent);
        return pMulBtnWidget;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用这种方式既可以使用复杂的控件代理,如果需要使用数据进行设置,只需要定义一个数据类,然后注册,如下:

    struct Person
    {
        int id;
        QString name;
        qint8 age;
     };
     Q_DECLARE_METATYPE(Person)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    但是这种情况只有在双击的时候代理才会显示,如果需要一直显示,或者在标题栏中是无法实现的。

    让代理一直显示

    使用自绘的方式

    使用自绘的方式主要是paint和editorEvent两个接口,自己手动绘制,这种方式最原始的方案,但是面对复杂的布局和多个控件的时候就比较麻烦。如下:

    在这里插入图片描述
    还可以参考Qt的例子: Star Delegate example 的实现,网上大部分添加一直显示的控件就是使用此方式实现的。

    使用View的接口设置一直显示

    针对代理一直显示,Qt的View中三个接口能完成此事,分别为: openPersistentEditor(), closePersistentEditor() 和 isPersistentEditorOpen()方法。使用方法非常简单:

    _pTableView->openPersistentEditor(pModel->index(1, 1));
    
    • 1

    比如需要给某一列进行设置就可以使用循环遍历所有列进行设置即可。但是,在使用排序过滤QSortFilterProxyModel会导致设置的一直显示失效, 主要原因在于通过_pTableView->model()获取的model已经是QSortFilterProxyModel,因此设置的索引不对,排序过滤后导致,需要将真正的数据类模型指针传递到代理中,并且过滤时手动调用openPersistentEditor接口。

    最终实现的结果展示

    在这里插入图片描述至于自定义QHeaderView参考: https://blog.csdn.net/beichen_yx/article/details/105599995

    示例代码

    // CDelegate.h
    #ifndef CDELEGATE_H
    #define CDELEGATE_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    class QWidget;
    class QVariant;
    class QPushButton;
    class CModel;
    
    class MulBtnWiget : public QWidget
    {
        Q_OBJECT
    public:
        explicit MulBtnWiget(const QModelIndex &index, QWidget *parent = nullptr);
    
        void setBtnData(const QVariant &data);
    
    signals:
        void btnColor(const QString &color, const int &row);
    
    private:
        QPushButton *_pButton1;
        QPushButton *_pButton2;
        QPushButton *_pButton3;
        QModelIndex _index;
    };
    
    
    class COpratorDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        enum ColType {
            STRING,
            COMBOBOX,
            PROGRESSBAR,
            MULBTN
        };
    
        explicit COpratorDelegate(QObject *parent = nullptr, const ColType &type=STRING);
    
        QWidget *createEditor(QWidget *parent,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override;
    
        void setEditorData(QWidget *editor, const QModelIndex &index) const override;
        void setModelData(QWidget *editor,
                          QAbstractItemModel *model,
                          const QModelIndex &index) const override;
    
        void setCModel(CModel * const pModel) { _pModel = pModel; }
    
    signals:
        void btnColor(const QString &color, const QModelIndex &index);
    
    private:
        CModel *_pModel = nullptr;
        ColType _colType = STRING;
    };
    
    #endif // CDELEGATE_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    #include "CDelegate.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "CModel/CModel.h"
    
    MulBtnWiget::MulBtnWiget(const QModelIndex &index, QWidget *parent/* = nullptr*/)
        : QWidget(parent), _index(index)
    {
        QHBoxLayout *pLayout = new QHBoxLayout();
        _pButton1 = new QPushButton("red");
        pLayout->addWidget(_pButton1);
        _pButton2 = new QPushButton("green");
        pLayout->addWidget(_pButton2);
        _pButton3 = new QPushButton("reset");
        pLayout->addWidget(_pButton3);
        pLayout->setContentsMargins(0, 0, 0, 0);
        this->setLayout(pLayout);
        this->setContentsMargins(0, 0, 0, 0);
        _pButton1->setEnabled(false);
        _pButton2->setEnabled(false);
        _pButton3->setEnabled(false);
        _pButton1->setStyleSheet("color: red;");
        _pButton2->setStyleSheet("color: green;");
        _pButton3->setStyleSheet("color: black;");
    
        connect(_pButton1, &QPushButton::clicked, this, [this](){
            emit btnColor("red", _index.row());
        });
        connect(_pButton2, &QPushButton::clicked, this, [this](){
            emit btnColor("green", _index.row());
        });
        connect(_pButton3, &QPushButton::clicked, this, [this](){
            emit btnColor("black", _index.row());
        });
    }
    
    void MulBtnWiget::setBtnData(const QVariant &data)
    {
        int type = qvariant_cast(data);
        if (type <= 3) {
            _pButton1->setEnabled(true);
            _pButton2->setEnabled(true);
            _pButton3->setEnabled(true);
            _pButton1->setStyleSheet("color: red;");
            _pButton2->setStyleSheet("color: green;");
        } else if (type >3 && type <=4) {
            _pButton1->setEnabled(false);
            _pButton1->setStyleSheet("color: black;");
            _pButton2->setEnabled(true);
            _pButton3->setEnabled(true);
        } else {
            _pButton1->setEnabled(true);
            _pButton2->setEnabled(false);
            _pButton2->setStyleSheet("color: black;");
            _pButton3->setEnabled(true);
        }
    }
    
    COpratorDelegate::COpratorDelegate(QObject *parent/* = nullptr*/,
                                       const ColType &type/*=STRING*/)
        : QStyledItemDelegate(parent),
          _colType(type)
    {
    }
    
    QWidget *COpratorDelegate::createEditor(QWidget *parent,
                                            const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const
    {
        if (_colType == STRING) {
            return QStyledItemDelegate::createEditor(parent, option, index);
        } else if (_colType == COMBOBOX) {
            auto *pCbx = new QComboBox(parent);
            pCbx->addItems({"test1", "test1", "test2", "test3"});
            return pCbx;
        } else if (_colType == PROGRESSBAR) {
            auto *pProgressbar = new QProgressBar(parent);
            pProgressbar->setRange(0, 10);
            return pProgressbar;
        } else if (_colType == MULBTN) {
            MulBtnWiget *pMulBtnWidget = new MulBtnWiget(index, parent);
            connect(pMulBtnWidget, &MulBtnWiget::btnColor, this,
                    [this](const QString &color, const int &row){
                // index.model()返回的模型不一定是自定义的数据模型,可能是代理模型
                if (_pModel == nullptr) { return; }
                _pModel->setRowColor(row, color);
            });
            return pMulBtnWidget;
        } else {
            return QStyledItemDelegate::createEditor(parent, option, index);
        }
    }
    
    void COpratorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        if (_colType == STRING) {
    
        } else if (_colType == COMBOBOX) {
            QComboBox *pWidget = qobject_cast(editor);
            auto tmp = QString::number(index.data().toInt());
            if (pWidget->findText(tmp) < 0) {
                pWidget->addItem(tmp);
            }
            pWidget->setCurrentText(tmp);
        } else if (_colType == PROGRESSBAR) {
            QProgressBar *pWidget = qobject_cast(editor);
            pWidget->setValue(index.data().toInt());
        } else if (_colType == MULBTN) {
            MulBtnWiget *pWidget = qobject_cast(editor);
            pWidget->setBtnData(index.data());
        } else {
    
        }
    }
    
    void COpratorDelegate::setModelData(QWidget *editor,
                                        QAbstractItemModel *model,
                                        const QModelIndex &index) const
    {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    #include "CDelegate.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "CModel/CModel.h"
    
    MulBtnWiget::MulBtnWiget(const QModelIndex &index, QWidget *parent/* = nullptr*/)
        : QWidget(parent), _index(index)
    {
        QHBoxLayout *pLayout = new QHBoxLayout();
        _pButton1 = new QPushButton("red");
        pLayout->addWidget(_pButton1);
        _pButton2 = new QPushButton("green");
        pLayout->addWidget(_pButton2);
        _pButton3 = new QPushButton("reset");
        pLayout->addWidget(_pButton3);
        pLayout->setContentsMargins(0, 0, 0, 0);
        this->setLayout(pLayout);
        this->setContentsMargins(0, 0, 0, 0);
        _pButton1->setEnabled(false);
        _pButton2->setEnabled(false);
        _pButton3->setEnabled(false);
        _pButton1->setStyleSheet("color: red;");
        _pButton2->setStyleSheet("color: green;");
        _pButton3->setStyleSheet("color: black;");
    
        connect(_pButton1, &QPushButton::clicked, this, [this](){
            emit btnColor("red", _index.row());
        });
        connect(_pButton2, &QPushButton::clicked, this, [this](){
            emit btnColor("green", _index.row());
        });
        connect(_pButton3, &QPushButton::clicked, this, [this](){
            emit btnColor("black", _index.row());
        });
    }
    
    void MulBtnWiget::setBtnData(const QVariant &data)
    {
        int type = qvariant_cast(data);
        if (type <= 3) {
            _pButton1->setEnabled(true);
            _pButton2->setEnabled(true);
            _pButton3->setEnabled(true);
            _pButton1->setStyleSheet("color: red;");
            _pButton2->setStyleSheet("color: green;");
        } else if (type >3 && type <=4) {
            _pButton1->setEnabled(false);
            _pButton1->setStyleSheet("color: black;");
            _pButton2->setEnabled(true);
            _pButton3->setEnabled(true);
        } else {
            _pButton1->setEnabled(true);
            _pButton2->setEnabled(false);
            _pButton2->setStyleSheet("color: black;");
            _pButton3->setEnabled(true);
        }
    }
    
    COpratorDelegate::COpratorDelegate(QObject *parent/* = nullptr*/,
                                       const ColType &type/*=STRING*/)
        : QStyledItemDelegate(parent),
          _colType(type)
    {
    }
    
    QWidget *COpratorDelegate::createEditor(QWidget *parent,
                                            const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const
    {
        if (_colType == STRING) {
            return QStyledItemDelegate::createEditor(parent, option, index);
        } else if (_colType == COMBOBOX) {
            auto *pCbx = new QComboBox(parent);
            pCbx->addItems({"test1", "test1", "test2", "test3"});
            return pCbx;
        } else if (_colType == PROGRESSBAR) {
            auto *pProgressbar = new QProgressBar(parent);
            pProgressbar->setRange(0, 10);
            return pProgressbar;
        } else if (_colType == MULBTN) {
            MulBtnWiget *pMulBtnWidget = new MulBtnWiget(index, parent);
            connect(pMulBtnWidget, &MulBtnWiget::btnColor, this,
                    [this](const QString &color, const int &row){
                // index.model()返回的模型不一定是自定义的数据模型,可能是代理模型
                if (_pModel == nullptr) { return; }
                _pModel->setRowColor(row, color);
            });
            return pMulBtnWidget;
        } else {
            return QStyledItemDelegate::createEditor(parent, option, index);
        }
    }
    
    void COpratorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        if (_colType == STRING) {
    
        } else if (_colType == COMBOBOX) {
            QComboBox *pWidget = qobject_cast(editor);
            auto tmp = QString::number(index.data().toInt());
            if (pWidget->findText(tmp) < 0) {
                pWidget->addItem(tmp);
            }
            pWidget->setCurrentText(tmp);
        } else if (_colType == PROGRESSBAR) {
            QProgressBar *pWidget = qobject_cast(editor);
            pWidget->setValue(index.data().toInt());
        } else if (_colType == MULBTN) {
            MulBtnWiget *pWidget = qobject_cast(editor);
            pWidget->setBtnData(index.data());
        } else {
    
        }
    }
    
    void COpratorDelegate::setModelData(QWidget *editor,
                                        QAbstractItemModel *model,
                                        const QModelIndex &index) const
    {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    #include "CModel/CModel.h"
    #include 
    #include 
    #include 
    
    CModel::CModel(QAbstractItemModel *parent) : QAbstractItemModel(parent)
    {
        for (int i=0; i<10; ++i) {
            m_stocks.append({i, i*100, rand() % 100, rand() % 300, i, i});
            _colors.append(QColor(Qt::black));
        }
    }
    
    void CModel::setRowColor(const int &row, const QString &color)
    {
        QColor txtColor;
        if (color == "red") {
            txtColor = Qt::red;
        } else if (color == "green") {
            txtColor = Qt::green;
        } else {
            txtColor = Qt::black;
        }
        _colors[row] = txtColor;
        auto start = this->index(row, 0);
        auto end = this->index(row, this->columnCount(QModelIndex()) - 1);
        emit dataChanged(start, end);
    }
    
    int CModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return m_stocks.length();
    }
    int CModel::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return m_names.length();
    }
    
    QVariant CModel::data(const QModelIndex &index, int role) const
    {
        if(!index.isValid()){
            qDebug() << "index is valid: " << index.row() << ":" << index.column();
            return QVariant();
        }
        if(index.row() > m_stocks.length() || index.column() > m_names.length()){
            qDebug() << "m_stocks len: " << m_stocks.length() << " " << index.row() << ":" << index.column();
            return QVariant();
        }
        if(role == Qt::DisplayRole){
            auto stock = m_stocks.at(index.row());
            return (index.column() > stock.length() || index.column() < 0) ? QVariant() : stock.at(index.column());
        } else if(role == Qt::EditRole){
            auto stock = m_stocks.at(index.row());
            return (index.column() > stock.length() || index.column() < 0) ? QVariant() : stock.at(index.column());
        } else if (role == Qt::TextColorRole) {
            return QVariant(_colors.at(index.row()));
        } else if (role == Qt::TextAlignmentRole) {
            return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
        }
        return QVariant();
    }
    
    bool CModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if(!index.isValid()){
            qDebug() << "index is valid: " << index.row() << ":" << index.column();
            return false;
        }
        if(role == Qt::EditRole){
            auto stock = m_stocks.at(index.row());
            stock[index.column()] = value;
            emit dataChanged(index, index);
            return true;
        }
        return false;
    }
    
    QVariant CModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    //    if(role != Qt::DisplayRole){
    //        return QVariant();
    //    }
    
    //    if(orientation == Qt::Horizontal){
    //        if(section < 0 || section > m_names.length()){return  QVariant();}
    //        return QVariant(m_names.at(section));
    //    }else {
    //        return QVariant(QString::number(section));
    //    }
        if(orientation == Qt::Horizontal){
            if (role == Qt::DisplayRole) {
                return QVariant(m_names.at(section));
            } else {
               qDebug() << role;
            }
        } else if (orientation == Qt::Vertical) {
            if (role == Qt::DisplayRole) {
                return QVariant(QString::number(section));
            }
        }
        return QVariant();
    }
    bool CModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
    {
        if(role != Qt::EditRole){
            return false;
        }
        if(orientation == Qt::Horizontal){
            m_names.replace(section, value.toString());
            emit headerDataChanged(Qt::Horizontal, section, section);
            return true;
        }else{
            return false;
        }
    }
    
    Qt::ItemFlags CModel::flags(const QModelIndex &index) const
    {
    //    if(index.isValid()){
    //         Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren | Qt::ItemIsEditable;
    //         return flags;
    //    }
    //    return QAbstractItemModel::flags(index);
        if(index.isValid()){
            Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
                    | Qt::ItemNeverHasChildren | Qt::ItemIsEditable;
            return flags | QAbstractItemModel::flags(index);
        }
        return QAbstractItemModel::flags(index);
    }
    
    QModelIndex CModel::index(int row, int column, const QModelIndex &parent) const
    {
        if (row < 0 || column < 0 || column >= columnCount(parent) || column > m_names.length())
            return QModelIndex();
    
        return createIndex(row,column);
    }
    
    QModelIndex CModel::parent(const QModelIndex &child) const
    {
        Q_UNUSED(child);
        return QModelIndex();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "CView/CTableView.h"
    #include "CModel/CModel.h"
    #include "CDelegate/CDelegate.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        _pTableView(new CTableView(this)),
        _pModel(new CModel())
    {
        ui->setupUi(this);
        this->setWindowTitle("Custom MVC");
        this->setCentralWidget(_pTableView);
        this->setContentsMargins(0, 0, 0, 0);
        _pTableView->setModel(_pModel);
    
        this->initUi();
    
        this->resize(1024, 768);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        delete _pModel;
        delete _pTableView;
    }
    
    void MainWindow::initUi()
    {
        int colCount = _pModel->columnCount(_pModel->index(0, 0));
        _pTableView->resizeColumnsToContents();
        for (int i=0; isetColumnWidth(i, 300);
        }
    
        COpratorDelegate *pCOD = new COpratorDelegate(_pTableView, COpratorDelegate::MULBTN);
        pCOD->setCModel(_pModel);
        _pTableView->setItemDelegateForColumn(colCount - 1, pCOD);
        auto *pModel = _pTableView->model();
        int row = pModel->rowCount();
        for (int i=0; iopenPersistentEditor(pModel->index(i, colCount - 1));
        }
        _columns.append(colCount - 1);
    
        COpratorDelegate *pProcessCOD = new COpratorDelegate(_pTableView, COpratorDelegate::PROGRESSBAR);
        pProcessCOD->setCModel(_pModel);
        _pTableView->setItemDelegateForColumn(colCount - 2, pProcessCOD);
        for (int i=0; iopenPersistentEditor(pModel->index(i, colCount - 2));
        }
        _columns.append(colCount - 2);
    
    
        COpratorDelegate *pCbxCOD = new COpratorDelegate(_pTableView, COpratorDelegate::COMBOBOX);
        pCbxCOD->setCModel(_pModel);
        _pTableView->setItemDelegateForColumn(colCount - 3, pCbxCOD);
        for (int i=0; iopenPersistentEditor(pModel->index(i, colCount - 3));
        }
        _columns.append(colCount - 3);
    
        connect(_pTableView, &CTableView::filterUpdate, this,  &MainWindow::openPersistentForColumns );
    }
    
    void MainWindow::openPersistentForColumns()
    {
        auto *pModel = _pTableView->model();
        int row = _pModel->rowCount();
        for (int i=0; iopenPersistentEditor(pModel->index(i, col));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    详细代码参见: https://gitee.com/BeiChen_XQ/custom-delegate.git

  • 相关阅读:
    STM32HAL库CRC学习及测试记录
    C#进阶高级语法之LINQ:深入分析LINQ的查询表达式、延迟执行与PLINQ高级特性
    小学六年级的读者,自学计算机,会爬虫,搞崩过学校网站,还有 Girlfriend.....
    前端怎么debugger排查线上问题
    原型制作神器ProtoPie的使用&Unity与网页跨端交互
    java中的File类(文件操控者)
    04.5. 权重衰减
    Java基于SpringBoot+Vue+nodejs的企业公司人事管理系统 Element
    日更【系统架构设计师知识总结2】指令系统(结合真题)
    [附源码]java毕业设计学生考试成绩分析系统
  • 原文地址:https://blog.csdn.net/beichen_yx/article/details/126564004