QStyledItemDelegate 是QItemDelegate 的子类,提供了更现代、更易用的接口。 QAbstractItemDelegate 是 QItemDelegate 的基类,而 QItemDelegate 则是 QStyledItemDelegate 的基类。 include "comboxdelegate.h"#include <QComboBox>QWComboBoxDelegate::QWComboBoxDelegate(QObject *parent):QItemDelegate
QTableWidget没有这样功能,所以需要通过委托来实现,再所以就需要继承QItemDelegate,之后重新实现一些函数,和Qt 重新封装的OpenGL又有些类似。 继承QItemDelegate,我们可以使用一些默认的功能,如果继承QAbstractItemDelegate,那就要从头开始实现更多的东西了。 1.自定义委托 //create ComboBoxDelegate #public QItemDelegate class ComboBoxDelegate : public QItemDelegate QStyleOptionViewItem &option, const QModelIndex &index) const { //Use class QComboBox, proxy use through QItemDelegate editor->setCurrentIndex(0); return editor; } else { return QItemDelegate
设置当前委托对象 QAbstractItemDelegate类是所有委托的父类,用来 负责提供通用接口 在模型视图中,会默认提供一个QStyledItemDelegate类,供用户编辑数据 也可以通过继承QItemDelegate QCustomizedDelegate.h: #ifndef QCUSTOMIZEDDELEGATE_H #define QCUSTOMIZEDDELEGATE_H #include <QItemDelegate > #include <QtGui> class QCustomizedDelegate : public QItemDelegate { Q_OBJECT public: : #include "QCustomizedDelegate.h" QCustomizedDelegate::QCustomizedDelegate(QObject *parent) : QItemDelegate > #include <QtGui> #include "ProgressBar.h" class QCustomizedDelegate : public QItemDelegate { Q_OBJECT
记录一下QTableView添加进度条 例子很小,仅供学习 使用QItemDelegate做的实现 有自动更新进度 要在.pro文件里添加 CONFIG += c++11 ProgressBarDelegate 类 #ifndef PROGRESSBARDELEGATE_H #define PROGRESSBARDELEGATE_H #include <QItemDelegate> class ProgressBarDelegate : public QItemDelegate { Q_OBJECT public: explicit ProgressBarDelegate(QObject *parent = 0); <QPainter> #include <QApplication> ProgressBarDelegate::ProgressBarDelegate(QObject *parent) : QItemDelegate CE_ProgressBar, &progressBarOption, painter); painter->restore(); } else { return QItemDelegate
State_HasFocus; } QItemDelegate ); } } using a QTreeView and a QItemDelegate I quickly found out that our default delegate, QItemDelegate, doesn’t use QStyle the way it is supposed To let that sink in – all our views now delegate painting to QStyledItemDelegate instead of QItemDelegate If you had written your own delegate using QItemDelegate, it is completely unaffected by this change.
这里我想要实现的是双击单元格时,通过combox进行选择,原本的TableWidget没有这样的功能,所以需要通过委托来实现,再所以就需要继承QItemDelegate,之后重新实现一些函数,和Qt 重新封装的 继承 QItemDelegate,我们可以使用一些默认的功能,如果继承QAbstractItemDelegate,那就要从头开始实现更多的东西了。 class ComboBoxDelegate : public QItemDelegate { public: ComboBoxDelegate(); public: QWidget setCurrentIndex(0); return editor; } else { return QItemDelegate
关联文章:PyQt QTableView嵌入QCheckBox 在Qt里,在QTableView中嵌入QComboBox真的很方便,只要实现一个QItemDelegate就可以了;QComboBox python #coding=utf-8 from PyQt4.QtGui import * from PyQt4.QtCore import * class DBComboBoxDelegate(QItemDelegate ): def __init__(self, comboModel, parent=None): QItemDelegate.
本例源代码:QtTowButtons.rar 看一下列的效果 看一下添加两个按钮的效果点击第一个按钮弹出 but1 +当前列 点击第二个按钮弹出but2 + 当前行 下面是主要实现 继承自 QItemDelegate 用来处理点击事件,在点击时我们算一下鼠标的坐标在哪个按钮下, 再处理相应的点击事件 #ifndef BUTTONDELEGATE_H #define BUTTONDELEGATE_H #include <QItemDelegate > class ButtonDelegate : public QItemDelegate { Q_OBJECT public: explicit ButtonDelegate(QObject include <QStyleOption> #include <QDesktopWidget> ButtonDelegate::ButtonDelegate(QObject *parent) : QItemDelegate
看一下ButtonDelegate的代码 #ifndef BUTTONDELEGATE_H #define BUTTONDELEGATE_H #include <QItemDelegate> class ButtonDelegate : public QItemDelegate { Q_OBJECT public: explicit ButtonDelegate(QObject *parent include <QStyleOption> #include <QDesktopWidget> ButtonDelegate::ButtonDelegate(QObject *parent) : QItemDelegate
QAbstractItemDelegate是委托类的抽象基类,Qt默认的委托实现由QStyledItemDelegate类提供,这也被用作Qt标准视图的默认委托,选择 QStyledItemDelegate或QItemDelegate include "spinboxdelexgate.h" #include<QSpinBox> SpinBoxDelexgate::SpinBoxDelexgate(QObject* parent):QItemDelegate
include "comboxdelegate.h"#include <QComboBox>QWComboBoxDelegate::QWComboBoxDelegate(QObject *parent):QItemDelegate
如果需要对用户输入做限制,比如只能在指定的字段输入指定的数据类型,可以通过QItemDelegate来实现。
include "comboxdelegate.h" #include <QComboBox> QWComboBoxDelegate::QWComboBoxDelegate(QObject *parent):QItemDelegate
注,QItemDelegate使用设置和访问函数色设置widget的USER属性。 (11)CONSTANT的出现表明属性是一个常量值。对于给点的对象实例,每一次READ函数的调用都应该返回相同的值。
在QTableView控件中,如果需要自定义的列按钮、复选框、下拉框等其他模式显示,可以采用自定义委托QItemDelegate来实现,如果需要禁用某列,则在自定义委托的重载createEditor函数返回