QTableView 委任用法QStyledItemDelegate

The QStyledItemDelegate class provides display and editing facilities for data items from a model.

委任是提供展示编辑模型数据的一种工具,比如在表格中,你想让表格具有下拉框,spinbox等组件的功能。就可以用委任来实现。

The QStyledItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework. The delegate allows the display and editing of items to be developed independently from the model and view

委任类是模型/视图,以及模型试图框架的一部分,模型/试图允许委任独立的展示编辑以及开发。

实现委任的方法有两大类:来自于他自身的虚函数

virtual QWidget *

createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

setEditorData(QWidget *editor, const QModelIndex &index) const

virtual void

setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const

virtual QSize

sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

1:qt 自身没有自带的组件

If the delegate does not support painting of the data types you need or you want to customize the drawing of items, you need to subclass QStyledItemDelegate, and reimplement paint() and possibly sizeHint(). The paint() function is called individually for each item, and with sizeHint(), you can specify the hint for each of them

如果委任不支持对数据类型的绘画,那末你就需要自己去绘画这些项目。你需要实现paint()可能还有sizehint().

2: qt自身拥有的组件

It is possible for a custom delegate to provide editors without the use of an editor item factory. In this case, the following virtual functions must be reimplemented:

一个定制的委托可以在不使用编辑器项目工厂的情况下提供编辑器。在这种情况下,必须重新实现以下虚拟功能;就是这四种函数。


本章节主要讲解第二部分:先看图:


main.cpp

#include <QApplication>

#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableView>
#include<QHBoxLayout>
#include"delegate.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    [1]
    QStandardItemModel *model=new QStandardItemModel;
    model->setColumnCount(2);
    model->setRowCount(4);
    [2]
    QTableView *view=new QTableView;
    view->verticalHeader()->hide();
    view->setAlternatingRowColors(true);
    view->setStyleSheet("QTableView{background-color: #4682B4;"
            "alternate-background-color: #D8BFD8;}");
    view->setModel(model);
    spinboxdelegate delegate;
    view->setItemDelegateForColumn(1,&delegate);//将表格的第一列进行委任。
    view->horizontalHeader()->setStretchLastSection(true);
/[3]
    QWidget *widget=new QWidget;
    QPalette pal=widget->window()->palette();
    pal.setColor(QPalette::Window, "#B0C4DE");
    widget->setFixedSize(400,500);
    QHBoxLayout *hb=new QHBoxLayout;
    hb->setContentsMargins(50,100,50,200);
    hb->addWidget(view);
    widget->setLayout(hb);
    widget->setPalette(pal);
    widget->show();
    return a.exec();
}
delegate.cpp

#include"delegate.h"

#include<QSpinBox>
#include<QComboBox>
spinboxdelegate::spinboxdelegate(QObject *parent):QStyledItemDelegate(parent)
{
}
QWidget *spinboxdelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *box=new  QComboBox(parent);
    box->addItem("男");
    box->addItem("女");
    box->setFrame(false);
    return box;
}
void spinboxdelegate::setEditorData(QWidget *editor, const QModelIndex &index)const
{
    QString  value=index.model()->data(index,Qt::EditRole).toString();
    QComboBox *box=static_cast<QComboBox*>(editor);
    box->setCurrentText(value);
}
void spinboxdelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
{
    QComboBox *box=static_cast<QComboBox*>(editor);
          box->internalWinId();
          QString  value = box->currentText();
          model->setData(index, value, Qt::EditRole);
}
void spinboxdelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}
delegate.h

#ifndef DELEGATE_H

#define DELEGATE_H
#include<QObject>
#include<QStyledItemDelegate>
 
class spinboxdelegate:public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit spinboxdelegate(QObject *parent=0);
 
      QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
     void setEditorData(QWidget *editor, const QModelIndex &index) const;
     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
};
 
#endif // DELEGATE_H
 



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt 中,QTableView 是一个常用的表格控件,它提供了一种方便的方式来展示和编辑数据。如果需要在 QTableView 中设置某个单元格的样式,可以使用 QStyledItemDelegate 和 QStyleOptionViewItem 类来实现。 具体来说,可以继承 QStyledItemDelegate 类并重写其 createEditor() 和 setEditorData() 函数,以实现在特定单元格中设置样式。代码示例如下: ``` class MyDelegate : public QStyledItemDelegate { public: QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { // 创建一个 QLineEdit 作为 editor QLineEdit* editor = new QLineEdit(parent); // 在第 1 行、第 2 列的单元格中设置样式 if (index.row() == 1 && index.column() == 2) { QPalette p = editor->palette(); p.setColor(QPalette::Base, QColor(Qt::red)); editor->setPalette(p); } return editor; } void setEditorData(QWidget* editor, const QModelIndex& index) const override { // 将单元格中的数据设置到编辑器中 QString value = index.model()->data(index, Qt::EditRole).toString(); QLineEdit* lineEdit = static_cast<QLineEdit*>(editor); lineEdit->setText(value); } }; ``` 然后,在使用 QTableView 的地方,可以设置自定义的委托对象来实现样式设置,代码示例如下: ``` QTableView* tableView = new QTableView; MyDelegate* delegate = new MyDelegate; tableView->setItemDelegate(delegate); ``` 这样,在 QTableView 中,第 1 行、第 2 列的单元格就会被设置为红色背景色。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值