QtabelView表中添加QComboBox小部件


前言

最近在学习QT5的QTableView,想要在QTableView上做成下拉框的形式,查找资料总结有两种实现方式。
 


一、基于setIndexWidget函数

void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget)

index 索引,这里为QTableView的索引

widget 小部件,可以是QComboBox、QLineEdit

举个例子,代码如下:

QComboBox* box = new QComboBox();、
QStringList list;

list << "id1" << "id2";
cob->addItems(list);

view->setIndexWidget(index, cob);

setIndexWidget函数,小部件会浮在Item上面,会一直显示小部件。

二、基于QItemDelegate

MyItemDelegate.h 代码如下:

class MyItemDelegate : public QItemDelegate
{
public:
    MyItemDelegate ();
    MyItemDelegate (QStringList list, QItemDelegate *parent = nullptr);

    // 创建编辑器
    virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    // 设置编辑器数据
    virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    // 更新编辑器集合属性
    virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    // 设置模型数据
    virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;

private:
    // 存储QComBoBox的数据
    QStringList m_comboBoxList;

};

MyItemDelegate.cpp 代码如下:

MyItemDelegate::MyItemDelegate()
{

}

MyItemDelegate::MyItemDelegate(QStringList list, QItemDelegate *parent) : QItemDelegate(parent)
{
    m_comboBoxList = list;
}

// 创建编辑器
QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    Q_UNUSED(option);
    Q_UNUSED(index);
    // 创建自己需要的控件进行返回
    QComboBox *editor = new QComboBox(parent);

    return editor;
}

// 设置编辑器数据
void MyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    Q_UNUSED(index);
    // 将参数editor转换为对应创建的控件,再进行数据初始设置就行
    QComboBox *cob = static_cast<QComboBox *>(editor);
    cob->addItems(m_comboBoxList);
}

// 更新编辑器集合属性
void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    Q_UNUSED(index);
    // 将编辑器设置为矩形属性
    editor->setGeometry(option.rect);
}

// 设置模型数据
void MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    // 类型转换
    QComboBox *comboBox = static_cast<QComboBox *>(editor);
    // 模型(单元格)显示的数据
    model->setData(index, comboBox->currentText());
}

 具体使用方法,代码如下:

int column = 1;
int row = 1;
QStringList list;
list << "id1" << "id2" << "id3";

// 创建时指定数据传入
MyItemDelegate* m_cBoxDelegate = new MyItemDelegate(list);
QTableView *tableView = new QTableView;

// 将某一列单元格设置为该委托
tableView->setItemDelegateForColumn(column,m_cBoxDelegate);
// 将某一行单元格设置为该委托
tableView->setItemDelegateForRow(row,m_cBoxDelegate);
// 将全部单元格设置为该委托
tableView->setItemDelegate(m_cBoxDelegate);

使用代理QItemDelegate方法,需要双击,才会显示小部件。


总结

以上就是QtabelView窗口小部件的内容,主要介绍了两种实现小部件的方法,两种方法各有千秋,需要根据需求进行选择,个人偏向第二种。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在QTableView表头中添加CheckBox,需要实现自定义QHeaderView并重写其paintSection()和mousePressEvent()方法。 首先,在自定义的QHeaderView的构造函数中设置其为可以接收鼠标事件: ```python class CheckBoxHeader(QtWidgets.QHeaderView): def __init__(self, parent): super().__init__(QtCore.Qt.Horizontal, parent) self.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents) self.setClickable(True) self.setSortIndicatorShown(False) ``` 然后,重写paintSection()方法,在其中绘制CheckBox: ```python class CheckBoxHeader(QtWidgets.QHeaderView): def paintSection(self, painter, rect, logicalIndex): painter.save() super().paintSection(painter, rect, logicalIndex) if logicalIndex == 0: option = QtWidgets.QStyleOptionButton() option.rect = QtCore.QRect(10, 5, 20, 20) option.state = QtWidgets.QStyle.State_Enabled | QtWidgets.QStyle.State_Active if self.isChecked: option.state |= QtWidgets.QStyle.State_On else: option.state |= QtWidgets.QStyle.State_Off self.style().drawControl(QtWidgets.QStyle.CE_CheckBox, option, painter) painter.restore() ``` 最后,重写mousePressEvent()方法,在其中判断是否点击了CheckBox,并根据需要切换其状态: ```python class CheckBoxHeader(QtWidgets.QHeaderView): def mousePressEvent(self, event): if event.button() == QtCore.Qt.LeftButton: if self.logicalIndexAt(event.pos()) == 0: self.isChecked = not self.isChecked self.sectionClicked.emit(0) self.viewport().update() else: super().mousePressEvent(event) ``` 在使用QTableView时,将其表头设置为自定义的CheckBoxHeader即可: ```python tableView = QtWidgets.QTableView() header = CheckBoxHeader(tableView) tableView.setHorizontalHeader(header) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值