QT开发中文网

how to add second delegate to a row in TreeView?

To add a second delegate to a row in QTreeView, you need to create a custom delegate that can handle both types of data and use it for the specific rows where you want to show the second delegate.

Here’s an example:

#include <QtWidgets>

class CustomDelegate : public QStyledItemDelegate {
public:
    CustomDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const override {
        // Check if this is the row where we want to use the second delegate
        if (index.row() == 1 && index.column() == 0) {
            // Use the second delegate for this row/column
            m_delegate2->paint(painter, option, index);
        } else {
            // Use the default delegate for all other cases
            QStyledItemDelegate::paint(painter, option, index);
        }
    }

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override {
        // Check if this is the row where we want to use the second delegate
        if (index.row() == 1 && index.column() == 0) {
            // Use the second delegate for this row/column
            return m_delegate2->createEditor(parent, option, index);
        } else {
            // Use the default delegate for all other cases
            return QStyledItemDelegate::createEditor(parent, option, index);
        }
    }

    void setEditorData(QWidget *editor, const QModelIndex &index) const override {
        // Check if this is the row where we want to use the second delegate
        if (index.row() == 1 && index.column() == 0) {
            // Use the second delegate for this row/column
            m_delegate2->setEditorData(editor, index);
        } else {
            // Use the default delegate for all other cases
            QStyledItemDelegate::setEditorData(editor, index);
        }
    }

    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const override {
        // Check if this is the row where we want to use the second delegate
        if (index.row() == 1 && index.column() == 0) {
            // Use the second delegate for this row/column
            m_delegate2->setModelData(editor, model, index);
        } else {
            // Use the default delegate for all other cases
            QStyledItemDelegate::setModelData(editor, model, index);
        }
    }

    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const override {
        // Check if this is the row where we want to use the second delegate
        if (index.row() == 1 && index.column() == 0) {
            // Use the second delegate for this row/column
            return m_delegate2->sizeHint(option, index);
        } else {
            // Use the default delegate for all other cases
            return QStyledItemDelegate::sizeHint(option, index);
        }
    }

    void initSecondDelegate(QAbstractItemDelegate *delegate) { m_delegate2 = delegate; }

private:
    QAbstractItemDelegate *m_delegate2 = nullptr;
};

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

    QStandardItemModel model(3, 1);

    QTreeView treeView;
    CustomDelegate customDelegate;
    treeView.setItemDelegate(&customDelegate);

    QLineEdit lineEdit;
    customDelegate.initSecondDelegate(lineEdit.lineEdit()->itemDelegate());
    
    treeView.setModel(&model);
    treeView.show();

    return app.exec();
}

In this example, we create a CustomDelegate that checks if the current index is in the row where we want to use the second delegate (row == 1 && column == 0). If it is, we call the corresponding methods on the second delegate; otherwise, we use the default delegate.

To use a custom delegate for a specific row, you need to initialize it with the desired delegate. In our case, we initialize it with a QLineEdit’s item delegate using lineEdit()->itemDelegate().

Note that this example only adds a second delegate to one specific row and column combination (row == 1 && column == 0). If you need to add multiple custom delegates or more complex logic for selecting which delegate to use based on data or other criteria, you will need to modify this code accordingly.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=3055

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?