每个单元格设置成不同的控件类型(案例) combox

核心思想是每行设置成一个单独的model

相当于数据还是那个数据,只是在点击单元格的时候变成combox了

 

 1 #pragma once
 2 
 3 #include <QStyledItemDelegate>
 4 
 5 #include <QComboBox>
 6 #include <QSpinBox>
 7 #include <QLineEdit>
 8 #include <UIControlBase.h>
 9 
10 class DynamicDelegate  : public QStyledItemDelegate
11 {
12 //    Q_OBJECT
13 //
14 //public:
15 //    DynamicDelegate(QObject *parent);
16 //    ~DynamicDelegate();
17     Q_OBJECT
18 public:
19     explicit DynamicDelegate(QObject* parent = nullptr);
20  /*   QList<UIControlBase> List_Item;*/
21     // 重写必要的函数
22     QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
23    /* void setEditorData(QWidget* editor, const QModelIndex& index) const override;
24     void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
25     void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;*/
26 
27 public:
28     QList<UIControlBase*> List_Item;
29    /* QList<int> List_Item;*/
30 };
//#include "DynamicDelegate.h"
//
//DynamicDelegate::DynamicDelegate(QObject *parent)
//    : QStyledItemDelegate(parent)
//{}
//
//DynamicDelegate::~DynamicDelegate()
//{}
#include "DynamicDelegate.h"
#include <ComboxUIControl.h>

DynamicDelegate::DynamicDelegate(QObject* parent)
    : QStyledItemDelegate(parent)
{

}
QWidget* DynamicDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    // 根据行和列索引决定创建哪种编辑器
    int row = index.row();
    int column = index.column();

    UIControlBase* Item = List_Item[column];

    ComboxUIControl* comboBox = dynamic_cast<ComboxUIControl*>(Item);
    
    if (comboBox != nullptr)
    {
        QComboBox* editor = new QComboBox(parent);
        editor->addItems(comboBox->qStringList);
        return editor;

    }
  
  
}


//void DynamicDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
//{
//    // 根据编辑器的类型,从模型中获取数据并设置到编辑器中
//    if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
//        QString currentText = index.data(Qt::EditRole).toString();
//        int tindex = comboBox->findText(currentText);
//        if (tindex >= 0) 
//        {
//            comboBox->setCurrentIndex(tindex);
//        }
//    }
//    else if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor))
//    {
//        int value = index.data(Qt::EditRole).toInt();
//        spinBox->setValue(value);
//    }
//    else if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) 
//    {
//        QString text = index.data(Qt::EditRole).toString();
//        lineEdit->setText(text);
//    }
//    else 
//    {
//        // 对于其他编辑器,调用基类方法
//        QStyledItemDelegate::setEditorData(editor, index);
//    }
//}
//
//void DynamicDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
//{
//    // 根据编辑器的类型,获取数据并设置回模型
//    if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
//        QString text = comboBox->currentText();
//        model->setData(index, text, Qt::EditRole);
//    }
//    else if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
//        spinBox->interpretText(); // 确保获取最新的文本输入值
//        int value = spinBox->value();
//        model->setData(index, value, Qt::EditRole);
//    }
//    else if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
//        QString text = lineEdit->text();
//        model->setData(index, text, Qt::EditRole);
//    }
//    else {
//        // 对于其他编辑器,调用基类方法
//        QStyledItemDelegate::setModelData(editor, model, index);
//    }
//}
//
//void DynamicDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
//{
//    // 设置编辑器的大小和位置,使其充满单元格
//    editor->setGeometry(option.rect);
//}
 1 #pragma once
 2 
 3 #include <QObject>
 4 
 5 class UIControlBase  : public QObject
 6 {
 7     Q_OBJECT
 8 
 9 public:
10     UIControlBase(const UIControlBase&);
11     UIControlBase(QObject *parent);
12     UIControlBase();
13     ~UIControlBase();
14 };
 1 #include "UIControlBase.h"
 2 
 3 UIControlBase::UIControlBase(const UIControlBase&)
 4 {
 5 }
 6 
 7 UIControlBase::UIControlBase(QObject *parent)
 8     : QObject(parent)
 9 {}
10 UIControlBase::UIControlBase()
11     : QObject()
12 {
13 }
14 UIControlBase::~UIControlBase()
15 {}

使用

 1 DynamicDelegate* delegate = new DynamicDelegate();
 2 ComboxUIControl* ComboxUIControl_Temp=new ComboxUIControl();
 3 ComboxUIControl_Temp->qStringList << u8"" << u8"" << u8"其他123";
 4 delegate->List_Item.append(ComboxUIControl_Temp);
 5   
 6 /*DynamicComboBoxDelegate* delegate = new DynamicComboBoxDelegate(
 7         { u8"男", u8"女", u8"其他" }, ui.tableView);*/
 8 
 9 QStandardItemModel* model = new QStandardItemModel(5, 3, ui.tableView);
10 
11 ui.tableView->setItemDelegateForRow(0, delegate);
12 
13 
14 {
15     DynamicDelegate* delegate1 = new DynamicDelegate();
16     ComboxUIControl* ComboxUIControl_Temp1 = new ComboxUIControl();
17     ComboxUIControl_Temp1->qStringList << u8"男1" << u8"女1" << u8"其他1231";
18     delegate1->List_Item.append(ComboxUIControl_Temp1);
19     ui.tableView->setItemDelegateForRow(1, delegate1);
20 }

 

posted @ 2025-12-11 18:27  家煜宝宝  阅读(1)  评论(0)    收藏  举报