https://blog.csdn.net/qq_25958023/article/details/78876567
自定义代理
#ifndef DELEGATE_H
#define DELEGATE_H
#include
#include
#include
#include
#include
#include
#include"ScanData/inputagedlg.h"
#include"ui_inputagedlg.h"
#define AGECOLUM 13
class Delegate : public QItemDelegate
{
Q_OBJECT
public:
Delegate(QObject *parent = nullptr);
~Delegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
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;
private:
};
#endif // DELEGATE_H
#include "delegate.h"
#include
#include
# pragma execution_character_set("utf-8")
Delegate::Delegate(QObject *parent): QItemDelegate(parent)
{
}
Delegate::~Delegate()
{
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);
}
QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QItemDelegate::sizeHint(option, index);
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.isValid() && index.column() == AGECOLUM)
{
InputAgeDlg *editor=new InputAgeDlg(parent);
editor->installEventFilter(const_cast(this));
return editor;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.isValid() && index.column() == AGECOLUM)
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
InputAgeDlg *inputdge = qobject_cast(editor);
if(!value.isEmpty())
{
if(value.contains(tr("岁")))
{
inputdge->set_age_uint(tr("岁"));
inputdge->set_Age(value.remove(tr("岁")));
}
if(value.contains(tr("月")))
{
inputdge->set_age_uint(tr("月"));
inputdge->set_Age(value.remove(tr("月")));
}
if(value.contains(tr("天")))
{
inputdge->set_age_uint(tr("天"));
inputdge->set_Age(value.remove(tr("天")));
}
if(value.contains(tr("小时")))
{
inputdge->set_age_uint(tr("小时"));
inputdge->set_Age(value.remove(tr("小时")));
}
}
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.isValid() && index.column() == AGECOLUM)
{
InputAgeDlg *inputdge = qobject_cast(editor);
if(inputdge->age.isEmpty()||inputdge->age==" ")
model->setData(index, "");
else
model->setData(index, inputdge->age+inputdge->age_uint);
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
自定义控件
#ifndef INPUTAGEDLG_H
#define INPUTAGEDLG_H
#include
namespace Ui {
class InputAgeDlg;
}
class InputAgeDlg : public QWidget
{
Q_OBJECT
public:
explicit InputAgeDlg(QWidget *parent = 0);
~InputAgeDlg();
QString getage();
QString getage_unit();
void set_Age(const QString& a);
void set_age_uint(const QString &u);
public:
Ui::InputAgeDlg *ui;
QString age;
QString age_uint;
private slots:
void on_ageedit_editingFinished();
void on_ageunitbox_currentIndexChanged(const QString &arg1);
};
#endif // INPUTAGEDLG_H
#include "inputagedlg.h"
#include "ui_inputagedlg.h"
# pragma execution_character_set("utf-8")
InputAgeDlg::InputAgeDlg(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputAgeDlg)
{
ui->setupUi(this);
QStringList age_unit={tr("岁"),tr("月"),tr("天"),tr("小时")};
ui->ageunitbox->addItems(age_unit);
ui->ageedit->setValidator(new QRegExpValidator(QRegExp("[0-9.]+$"))); //只能输入数字
}
InputAgeDlg::~InputAgeDlg()
{
delete ui;
}
QString InputAgeDlg::getage()
{
return ui->ageedit->text();
}
QString InputAgeDlg::getage_unit()
{
return ui->ageunitbox->currentText();
}
void InputAgeDlg::set_Age(const QString &a)
{
ui->ageedit->setText(a);
}
void InputAgeDlg::set_age_uint(const QString &u)
{
ui->ageunitbox->setCurrentText(u);
}
void InputAgeDlg::on_ageedit_editingFinished()
{
age=ui->ageedit->text();
age_uint=ui->ageunitbox->currentText();
}
void InputAgeDlg::on_ageunitbox_currentIndexChanged(const QString &arg1)
{
age=ui->ageedit->text();
age_uint=arg1;
}