Qt26代理delegate

news2024/9/25 17:15:05

代理delegate

  • mainwindow
    • mainwindow.h
    • mainwindow.cpp
  • datedelegate
    • datedelegate.h
    • datedelegate.cpp
  • combodelegate
    • combodelegate.h
    • combodelegate.cpp
  • spindelegate
    • spindelegate.h
    • spindelegate.cpp
  • main.cpp
  • 运行图

mainwindow

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItemModel>
#include <QTableView>
#include <QFile>
#include <QTextStream>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public:
    void createAction();
    void createMenu();
    void setupModel();
    void setupView();
    void openFile(QString);
public slots:
    void slotOpen();
private:
    QMenu *fileMenu;
    QAction* openAct;
    QStandardItemModel* model;
    QTableView *table;
    DateDelegate* dataDelegate;
    ComboDelegate* comboDelegate;
    SpinDelegate* spinDelegate;


};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QHeaderView>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QStringList>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createAction();
    createMenu();
    setupModel();
    setupView();
    setWindowTitle(tr("View Example"));
    resize(600,600);
}

MainWindow::~MainWindow() {}

void MainWindow::createAction()
{
    openAct = new QAction(tr("打开"),this);
    connect(openAct,&QAction::triggered,this,&MainWindow::slotOpen);
}

void MainWindow::createMenu()
{
    fileMenu = new QMenu("文件",this);
    fileMenu->addAction(openAct);
    menuBar()->addMenu(fileMenu);
}

void MainWindow::setupModel()
{
    model = new QStandardItemModel(4,4,this);
    model->setHeaderData(0,Qt::Horizontal,tr("姓名"));
    model->setHeaderData(1,Qt::Horizontal,tr("生日"));
    model->setHeaderData(2,Qt::Horizontal,tr("职业"));
    model->setHeaderData(3,Qt::Horizontal,tr("收入"));
}

void MainWindow::setupView()
{
    table = new QTableView;
    table->setModel(model);
    dataDelegate = new DateDelegate;
    comboDelegate = new ComboDelegate;
    spinDelegate  = new SpinDelegate;
    table->setItemDelegateForColumn(1,dataDelegate);
    table->setItemDelegateForColumn(2,comboDelegate);
    table->setItemDelegateForColumn(3,spinDelegate);


    table->horizontalHeader()->setStyleSheet("QHeaderView::section { background-color: #FFCCCC; }");
    setCentralWidget(table);

}

void MainWindow::openFile(QString path)
{
    if(!path.isEmpty())
    {
        QFile file(path);
        if(file.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream stream(&file);
            stream.setCodec("UTF-8");
            QString line;
            model->removeRows(0,model->rowCount(QModelIndex()));//清除所有数据
            int row = 0;
            do
            {
                line = stream.readLine();
                if(!line.isEmpty())
                {
                    model->insertRows(row,1,QModelIndex());
                    QStringList pleces = line.split(",");
                    model->setData(model->index(row,0,QModelIndex()),pleces.value(0));
                    model->setData(model->index(row,1,QModelIndex()),pleces.value(1));
                    model->setData(model->index(row,2,QModelIndex()),pleces.value(2));
                    model->setData(model->index(row,3,QModelIndex()),pleces.value(3));
                    row++;
                }

            }while(!stream.atEnd());
            file.close();
        }
    }

}

void MainWindow::slotOpen()
{
    QString name;
    name  = QFileDialog::getOpenFileName(this,"打开",".","histogram files (*.txt)");
    if(!name.isEmpty())
    {
        openFile(name);
    }
}

datedelegate

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 dataStr = index.model()->data(index).toString();
    QDate date = QDate::fromString(dataStr,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

combodelegate.h

#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H

#include <QItemDelegate>
#include <QObject>

class ComboDelegate : public QItemDelegate
{
public:
    explicit ComboDelegate(QObject *parent = nullptr);
    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("工人");
    editor->addItem("农民");
    editor->addItem("医生");
    editor->addItem("律师");
    editor->addItem("军人");
    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

spindelegate.h

#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H

#include <QItemDelegate>
#include <QObject>

class SpinDelegate : public QItemDelegate
{
public:
    explicit SpinDelegate(QObject *parent = nullptr);
    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* sbox = new QSpinBox(parent);
    sbox->setRange(0,80000);
    sbox->installEventFilter(const_cast<SpinDelegate*>(this));
    return sbox;
}

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

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

}

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


main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

运行图

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2110104.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java8新特性-Optional的使用

写在前面 最开始学java的时候&#xff0c;总能听到别人说java8的新特性&#xff0c;比如lambda表达式&#xff0c;stream流等等。但是第一次接触Optional是在公司前辈的代码中看到的。最开始我还以为是公司自己的工具类&#xff0c;也没太注意。后来才知道他也是java8最重要的…

西安十大产业园,哪一个将成为你事业的新起点?

每个人在追求事业成功的过程中&#xff0c;都在寻找那个能够助力自己腾飞的平台&#xff0c;而西安的十大产业园无疑提供了丰富多样的选择&#xff1a; 一、西安国际数字影像产业园 1. 园区概况 西安国际数字影像产业园是西安乃至西北地区在数字影像产业领域的重要基地&#…

基于视觉-语言模型的机器人任务规划:ViLaIn框架解析

目录 一、引言二、ViLaln框架介绍总体框架概述对象估计器初始状态估计器目标估计器纠错重提示机制&#xff08;CR&#xff09; 参考文献 一、引言 随着机器人技术的不断发展&#xff0c;如何通过自然语言指令引导机器人执行任务成为了一个重要的研究方向。自然语言作为人与机器…

mysql连接oceanbase数据库集群+租户

mysql集成的有连接oceanbase数据库的方式&#xff0c;所以只需要对参数进行修改即可。 url: jdbc:mysql://[ip地址]:[端口]/[数据库]?useUnicodetrue&characterEncodingUTF-8&serverTimezoneUTC //其他参数根据需求设置username: [用户名][租户名]#[集群名]password: …

【CTF Web】BUUCTF Upload-Labs-Linux Pass-10 Writeup(文件上传+PHP+扩展名双写绕过)

Upload-Labs-Linux 1 点击部署靶机。 简介 upload-labs是一个使用php语言编写的&#xff0c;专门收集渗透测试和CTF中遇到的各种上传漏洞的靶场。旨在帮助大家对上传漏洞有一个全面的了解。目前一共20关&#xff0c;每一关都包含着不同上传方式。 注意 1.每一关没有固定的…

springboot+vue+mybatisjsp广播剧制作订阅系统+PPT+论文+讲解+售后

随着世界经济信息化、全球化的到来和互联网的飞速发展&#xff0c;推动了各行业的改革。若想达到安全&#xff0c;快捷的目的&#xff0c;就需要拥有信息化的组织和管理模式&#xff0c;建立一套合理、动态的、交互友好的、高效的广播剧制作订阅系统。当前的信息管理存在工作效…

element-ui打包之后图标不显示,woff、ttf加载404

1、bug 起因 昨天在 vue 项目中编写 element-ui 的树形结构的表格&#xff0c;发现项目中无法生效&#xff0c;定位问题之后发现项目使用的 element-ui 的版本是 2.4.11 。看了官方最新版本是 2.15.14&#xff0c;然后得知 2.4.11 版本是不支持表格树形结构的。于是决定升级 el…

Python 在Excel中应用和取消多种不同类型的数据筛选

目录 安装Python Excel处理库 Python 在 Excel 中应用文本筛选 Python 在 Excel 中应用数字筛选 Python 在 Excel 中应用字体颜色、单元格颜色或图标集筛选 Python 在 Excel 中应用日期筛选 Python 在 Excel 中应用动态日期筛选 Python 在 Excel 中筛选空单元格或非空单…

再做leetcode42hard题接雨水——双指针法

再做leetcode42hard题接雨水——双指针法 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 示例 1&#xff1a; 输入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 输出&#xff1a;6 解释&#xff1a…

在jenkins中获取git的修改记录的方法

获取 Jenkins API Token&#xff1a; 首先&#xff0c;登录到你的 Jenkins 服务器。 点击右上角的用户名&#xff0c;然后选择“Configure&#xff08;配置&#xff09;”。 在“API Token”部分&#xff0c;生成一个新的 API Token 或使用已有的 Token。 构建 API 请求 URL&a…

k8s上搭建devops环境

一、gitlab 1.安装gitlab # 下载安装包 wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-15.9.1-ce.0.el7.x86_64.rpm # 安装 rpm -i gitlab-ce-15.9.1-ce.0.el7.x86_64.rpm # 编辑 vi /etc/gitlab/gitlab.rb 文件 # 修改 external_url 访问路径 htt…

网络安全工程师培训费用

在当今这个信息化迅猛发展的时代&#xff0c;网络安全已成为各行各业关注的焦点。作为保障网络信息安全的中坚力量&#xff0c;网络安全工程师的需求量逐年攀升。随之而来的是&#xff0c;越来越多的人对网络安全工程师的培训费用充满了好奇。本文将为您详细解析这一问题&#…

内存卡不小心格式化了有办法恢复数据吗?

在数字时代&#xff0c;内存卡作为便携式存储设备&#xff0c;广泛应用于手机、相机等设备中。然而&#xff0c;由于操作不当或设备故障&#xff0c;内存卡有时会被不小心格式化&#xff0c;导致存储在其中的重要数据丢失。 面对这种情况&#xff0c;许多人可能会感到焦虑&…

C语言求100以内的素数

问题&#xff1a;用C语言求出100以内的素数。 分析&#xff1a;素数&#xff0c;即质数&#xff0c;是指只可以被1和本身整除的数。此时可以考虑用循环的方法来用这个数除以所有1001以内的数&#xff0c;若存在余数为0的情况&#xff0c;则说明该数不是素数&#xff1b;此外&am…

MyBatis 一级缓存原理

优质博文&#xff1a;IT-BLOG-CN 一、一级缓存配置 MyBatis一级缓存默认是开启的。如果需要显示的开启&#xff0c;需要在MyBaits配置文件中<settings>标签中添加如下语句&#xff1a; <settings><setting name"localCacheScope" value"SESSI…

k8s之HPA实践——实现Web服务器的自动伸缩特性

文章目录 在生产环境中&#xff0c;总会有一些意想不到的事情发生&#xff0c;比如公司网站流量突然升高&#xff0c;此时之前创建的Pod已不足以支撑所有的访问&#xff0c;而运维人员也不可能24小时守着业务服务&#xff0c;这时就可以通过配置HPA&#xff0c;实现负载过高的情…

APP黄金流量:如何完成首页入口资源位最大价值

首页资源位的使用很重要 首页资源位&#xff0c;是指用户打开并进入 App 后&#xff0c;所有直接展示给用户内容&#xff0c;即不需要用户滑动页面就看到的内容&#xff0c;都是首页资源位。 移动互联网流量即王道&#xff0c;而 App 首页展示给用户的内容&#xff0c;会直接…

基于CNN卷积神经网络迁移学习的图像识别实现

基于CNN卷积神经网络迁移学习的图像识别实现 基于CNN卷积神经网络迁移学习的图像识别实现写在前面一&#xff0c;原理介绍迁移学习的基本方法1.样本迁移&#xff08;Instance based TL&#xff09;2.特征迁移&#xff08;Feature based TL&#xff09;3.模型迁移&#xff08;Pa…

html+css网页设计 我的家乡5个页面

htmlcss网页设计 我的家乡5个页面 网页作品代码简单&#xff0c;可使用任意HTML辑软件&#xff08;如&#xff1a;Dreamweaver、HBuilder、Vscode 、Sublime 、Webstorm、Text 、Notepad 等任意html编辑软件进行运行及修改编辑等操作&#xff09;。 获取源码 1&#xff0c;访…

第二证券:北交所新股申购和沪深两市有什么区别?

北交所新股申购和沪深新股申购的区别&#xff1a; 1、申购条件不同 深市、沪市申购新股前第22个交易日至申购前第2个交易日的日均持有市值在1万元以上的投资者可参加新股申购。 此外&#xff0c;创业板&#xff08;深市&#xff09;新股申购有必要注册创业板权限。创业板注册…