使用 QStyledItemDelegate 子类在 QTableView 中创建按钮 [英] Create PushButtons in QTableView with QStyledItemDelegate subclass

查看:206
本文介绍了使用 QStyledItemDelegate 子类在 QTableView 中创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有完全相同的问题,但我将使用 QTableView 小部件.我读了 this 并想知道我是否可以覆盖 createEditor使用例如 QFileDialog 获取新数据的函数.

I have the exact same problem, but I will use the QTableView widget. I read this and was wondering if I can override the createEditor function to use for instance QFileDialog to get the new data.

如果可能的话,谁能给我提供一个例子来实现 QItemDelegate 的子类.

If this is possible, can anyone provide me with an example to implement such a subclass to QItemDelegate.

如果没有,谁能给我一个例子来实现 QItemDelegate 的子类,女巫可以在 QLineEdit 旁边画一个按钮来获得功能 此处.

And if not, can anyone provide me with an example to implement a subclass to QItemDelegate, witch can draw a button next to a QLineEdit to get the functionality here.

也许这个问题真的很愚蠢,我没有意识到,因为我离开了这个项目大约半年.

Maybe this question is really stupid and i dont realize, because I left the project for around half a year.

第二:从 Qt 5.7 升级到 5.8 安全吗?

Second: Is it safe to update from Qt 5.7 to 5.8?

推荐答案

  1. 使用 QStyledItemDelegate,而不是 QItemDelegate.
  2. 阅读 Qt 手册

QStyledItemDelegate 类

Spinboxdelegate 示例

  1. 子类 QStyledItemDelegate 的代码示例(精简):

头文件

#ifndef MYITEMDELEGATE_H
#define MYITEMDELEGATE_H

#include <QStyledItemDelegate>

class KontaktForm;

class MyItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

    mutable SubscriberForm *subscriberForm;

public:
    explicit MyItemDelegate(QObject *parent = 0);
    ~MyItemDelegate();


////////!Methods - You don't need all of them
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);

};

#endif // MYITEMDELEGATE_H

源文件

#include "myitemdelegate.h"

#include "mytreeview.h"
#include <QModelIndex>
#include <QSize>

MyItemDelegate::MyItemDelegate(QObject *parent) : QStyledItemDelegate(parent),
    subscriberForm(Q_NULLPTR),
{
}

QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //// return the QSize of the item in Your view
}

void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    ////optional : implement custom painting - text, images, drawings, and such
}


QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    subscriberForm = new SubscriberForm(parent);
    ////optional additional settings for Your editor
    return subscriberForm;      
}

void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

void MyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    ////setup the editor - your data are in index.data(Qt::DataRoles) - stored in a QVariant;
    QString value = index.model()->data(index,Qt::EditRole).toString();
    SubscriberForm *subscriberForm =  static_cast<SubscriberForm*>(editor);
}

void MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    ////optional - if needed - return changed data, from editor to the model in a custom matter

    SubscriberForm *subscriberForm =  static_cast<SubscriberForm*>(editor);
    model->setData(index,QVariant(subscriberForm->getData()),Qt::EditRole);

}

这篇关于使用 QStyledItemDelegate 子类在 QTableView 中创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆