日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

QT代理Delegates使用实例(三种代理控件)

發布時間:2023/12/25 综合教程 21 生活家
生活随笔 收集整理的這篇文章主要介紹了 QT代理Delegates使用实例(三种代理控件) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果如下,在表格的單元格中插入控件,用Delegates方式實現

源代碼如下:

main.cpp文件

#include <QApplication>
#include <QStandardItemModel>
#include <QTableView>
#include <QFile>
#include <QTextStream>
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);

QStandardItemModel model(4,4);
QTableView tableView;
tableView.setModel(&model);

DateDelegate dateDelegate;
tableView.setItemDelegateForColumn(1,&dateDelegate);

ComboDelegate comboDelegate;
tableView.setItemDelegateForColumn(2,&comboDelegate);

SpinDelegate spinDelegate;
tableView.setItemDelegateForColumn(3,&spinDelegate);

model.setHeaderData(0,Qt::Horizontal,QObject::tr("Name"));
model.setHeaderData(1,Qt::Horizontal,QObject::tr("Birthday"));
model.setHeaderData(2,Qt::Horizontal,QObject::tr("Job"));
model.setHeaderData(3,Qt::Horizontal,QObject::tr("Income"));

QFile file("test.tab");
if(file.open(QFile::ReadOnly|QFile::Text))
{
QTextStream stream(&file);
QString line;

model.removeRows(0,model.rowCount(QModelIndex()),QModelIndex());
int row =0;
do{
line = stream.readLine();
if(!line.isEmpty())
{
model.insertRows(row,1,QModelIndex());

QStringList pieces = line.split(",",QString::SkipEmptyParts);
model.setData(model.index(row,0,QModelIndex()),pieces.value(0));
model.setData(model.index(row,1,QModelIndex()),pieces.value(1));
model.setData(model.index(row,2,QModelIndex()),pieces.value(2));
model.setData(model.index(row,3,QModelIndex()),pieces.value(3));
row++;
}
}while(!line.isEmpty());

file.close();
}
tableView.setWindowTitle(QObject::tr("Delegate"));
tableView.show();
return app.exec();
}

datedelegate.h文件

#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H

#include <QItemDelegate>

class DateDelegate : public QItemDelegate
{
Q_OBJECT

public:
DateDelegate(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 // DATEDELEGATE_H

datedelegate.cpp文件

#include "datedelegate.h"
#include <QDateTimeEdit>

DateDelegate::DateDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

QWidget *DateDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("yyyy-MM-dd");
editor->setCalendarPopup(true);
editor->installEventFilter(const_cast<DateDelegate*>(this));

return editor;
}

void DateDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString dateStr= index.model()->data(index).toString();
QDate date = QDate::fromString(dateStr,Qt::ISODate);

QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);
edit->setDate(date);
}

void DateDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);
QDate date = edit->date();
model->setData(index,QVariant(date.toString(Qt::ISODate)));
}

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

combodelegate.h文件

#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H

#include <QItemDelegate>

class ComboDelegate : public QItemDelegate
{
Q_OBJECT

public:
ComboDelegate(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 // COMBODELEGATE_H

combodelegate.cpp文件

#include "combodelegate.h"
#include <QComboBox>

ComboDelegate::ComboDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

QWidget *ComboDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem(QString::fromLocal8Bit("工人"));
editor->addItem(QString::fromLocal8Bit("農民"));
editor->addItem(QString::fromLocal8Bit("醫生"));
editor->addItem(QString::fromLocal8Bit("律師"));
editor->addItem(QString::fromLocal8Bit("軍人"));
editor->installEventFilter(const_cast<ComboDelegate*>(this));

return editor;
}

void ComboDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString str =index.model()->data(index).toString();

QComboBox *box = static_cast<QComboBox*>(editor);
int i=box->findText(str);
box->setCurrentIndex(i);
}

void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *box = static_cast<QComboBox*>(editor);
QString str = box->currentText();

model->setData(index,str);
}
void ComboDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}

spindelegate.h文件

#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H

#include <QItemDelegate>

class SpinDelegate : public QItemDelegate
{
Q_OBJECT

public:
SpinDelegate(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 // SPINDELEGATE_H

spindelegate.cpp文件

#include "spindelegate.h"
#include <QSpinBox>

SpinDelegate::SpinDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

QWidget *SpinDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setRange(0,10000);
editor->installEventFilter(const_cast<SpinDelegate*>(this));

return editor;
}

void SpinDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value =index.model()->data(index).toInt();

QSpinBox *box = static_cast<QSpinBox*>(editor);
box->setValue(value);
}

void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QSpinBox *box = static_cast<QSpinBox*>(editor);
int value = box->value();

model->setData(index,value);
}
void SpinDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}

http://blog.csdn.net/liuguangzhou123/article/details/7355985

總結

以上是生活随笔為你收集整理的QT代理Delegates使用实例(三种代理控件)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲欧美日韩国产 | 91亚洲网 | 黄色成人影视 | 玩弄丰满少妇xxxxx性多毛 | 日韩精品免费在线 | 一本视频在线 | 姐姐的秘密韩剧免费观看全集中文 | 少妇献身老头系列 | 九色国产精品 | 一级中文片 | 五月婷婷狠狠爱 | 色婷婷久久久亚洲一区二区三区 | 精品中文视频 | 朴银狐电影中文在线看 | 国产精品久久久爽爽爽麻豆色哟哟 | 欧美成视频 | 亚洲一区欧洲二区 | 无码国产精品高潮久久99 | 免费一级特黄3大片视频 | 成人动漫h在线观看 | 成人97| 成人高潮片 | 美女网站免费观看 | 亚洲天堂视频在线播放 | 性猛交富婆╳xxx乱大交麻豆 | 精品无人国产偷自产在线 | 国产丝袜自拍 | 91一区二区三区在线观看 | 手机在线永久免费观看av片 | 亚洲天堂系列 | 91福利一区| 污网站在线播放 | 人人射 | 九九热在线视频播放 | 亚洲综合图 | 亚洲精品综合精品自拍 | 国产91一区二区三区 | 国产人久久人人人人爽 | www.啪啪| 91在线观看成人 | 精品人伦一区二区三电影 | 在线免费观看黄网 | 日本xx视频 | 国产成人啪免费观看软件 | 欧美国产激情 | 日本三级视频在线播放 | 国产熟女一区二区 | wwwxxxx欧美| 亚洲国产视频一区二区三区 | 在线观看免费高清视频 | 一本大道综合伊人精品热热 | 欧美成人日韩 | 欧美激情久久久久久久 | 91精品视频在线播放 | 九色.com| 日本一区二区三区视频在线观看 | 欧美日韩中文 | 男男全肉变态重口高h | 97干干| 国产精品久久久久久亚洲 | 日韩黄色一区 | 靠逼网站| 在线成人一区 | 五月天av影院 | 男女做激情爱呻吟口述全过程 | 伊人资源 | 91日本视频| 青久草视频| 亚洲a级在线观看 | 操操操操操操操操操 | 在线观看视频99 | 五月花婷婷 | 日韩精品中文字幕在线 | 精品亚洲永久免费精品 | 正在播放一区 | 国产精品无码AV无码国产 | 成人av在线影院 | 久久免费黄色网址 | 亚洲激情社区 | 永久免费av网站 | 欧美一区二区激情 | 又黄又爽一区二区三区 | 另类亚洲激情 | 亚洲综合日韩精品欧美综合区 | 无遮挡黄色 | 香蕉视频网站在线观看 | 黄色欧美在线观看 | 性喷潮久久久久久久久 | 亚洲成人不卡 | 色婷婷综合久久久久中文字幕 | 久久久久亚洲av成人网人人软件 | 日本一品道 | 日韩a在线观看 | 在线观看黄网 | 理论片亚洲 | 日本一区二区三区中文字幕 | 国产精品扒开腿做爽爽爽视频 | 老司机久久精品视频 | 色乱码一区二区三在线看 |