当前位置:首页 > CN2资讯 > 正文内容

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

2天前CN2资讯


效果如下,在表格的单元格中插入控件,用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);

}



    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/24723.html

    分享给朋友:

    “QT代理Delegates使用实例(三种代理控件)” 的相关文章

    全面解析UDP攻击:类型、影响及有效防御策略

    在当今网络环境中,UDP攻击是一个话题无法忽视的安全隐患。它是一种常见的网络攻击形式,通常会给目标服务器带来严重的资源消耗。为了更好地理解这一现象,我们不妨简单回顾一下UDP协议的特性,以及攻击者是如何利用这一协议实施攻击的。 UDP,即用户数据报协议(User Datagram Protocol)...

    香港低价服务器:经济实惠的选择与优势解析

    在如今数字化迅猛发展的时代,香港低价服务器凭借其独特优势,吸引了无数创业者、站长和企业用户的青睐。何为香港低价服务器?这类服务器主要是指在香港地区提供的,价格相对较低的服务器租用服务。由于其经济实惠的特性,许多小型企业和个人用户在选择服务器时,都会优先考虑这种选项。 在选择网络服务时,速度和价格往往...

    最新hudsonvalleyhost优惠码使用技巧与推荐2023

    hudsonvalleyhost 优惠码介绍 hudsonvalleyhost优惠码是一种可以帮助用户在使用hudsonvalleyhost服务时节省费用的代码。这种优惠码一般由字母和数字组成,用户在进行购买或订阅相关服务时输入该代码,就可以享受到折扣或特定的优惠。每个优惠码的有效期和适用范围可能不...

    云服务器购买指南:选择适合你的云服务方案

    在我开始谈论云服务器购买指南时,首先让我给你解释一下“云服务器”是什么。简单来说,云服务器是基于云计算技术构建的虚拟服务器。它通过网络将计算、存储和其他资源结合在一起,提供灵活的计算能力。相比传统的物理服务器,云服务器的优势在于其高度的可扩展性、经济性和便捷性。你可以根据具体需求随时增减资源,而且没...

    香港VPS:灵活高效的网站托管解决方案

    在互联网技术不断发展的今天,VPS(虚拟专用服务器)逐渐成为网站建设和在线应用的热门选择。简单来说,VPS是一种虚拟化的服务器环境,它通过将物理服务器划分成多个独立的虚拟服务器,为用户提供更高的灵活性与控制权。这意味着,用户可以拥有一台独立的服务器环境,但成本却比购买一台专属服务器要低得多。 VPS...

    Servarica:企业数字化转型的理想云解决方案

    Servarica,作为一款先锋的云解决方案平台,近年来逐渐进入了企业和 IT 行业的视野。回顾它的历史,Servarica 在技术演变与市场需求的促进下不断发展壮大。最初,Servarica 旨在为企业提供高效、安全的云存储和计算服务,帮助他们应对日益增长的数据处理需求。随着云计算技术的进步,Se...