QTabelView使用代理自定义,第一列为QLabel第二列为下拉框

news2024/10/5 18:30:54

预览界面

你好! 这是你第一次使用 **Markdown编辑器** 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

代理源文件

CustomParamViewDelegate.cpp

#include "CustomParamViewDelegate.h"

CustomParamViewDelegate::CustomParamViewDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{}

CustomParamViewDelegate::~CustomParamViewDelegate()
{}

QWidget* CustomParamViewDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    int type = index.data(Qt::UserRole).toInt();

    switch (type) {
    case (CustomParamViewDelegate::Type_ComboBox): {
        return new QComboBox(parent);
    }
    case (CustomParamViewDelegate::Type_Label): {
        return new QLabel(parent);
    }
    case (CustomParamViewDelegate::Type_LinEdit): {
        return new QLineEdit(parent);
    }
    default:
        break;
    }
    return nullptr;
}

void CustomParamViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
    int type = index.data(Qt::UserRole).toInt();
    //QString data = index.data().toString();
    QString data = index.data(Qt::UserRole + 1).toString();

    switch (type) {
    case (CustomParamViewDelegate::Type_ComboBox): {
        QComboBox* cob = static_cast<QComboBox*>(editor);
        QStringList strList = data.split(",");
        cob->addItems(strList);
        cob->setCurrentIndex(cob->findText(data));
        break;
    }
    case (CustomParamViewDelegate::Type_Label): {
        QLabel *label = static_cast<QLabel*>(editor);
        label->setText(data);
        break;
    }
    case (CustomParamViewDelegate::Type_LinEdit): {

        break;
    }
    default:
        break;
    }
}

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

void CustomParamViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
    int type = index.data(Qt::UserRole).toInt();
    QString value;
    switch (type) {
    case (CustomParamViewDelegate::Type_ComboBox): {
        QComboBox* cob = static_cast<QComboBox*>(editor);
        value = cob->currentText();
        break;
    }
    case (CustomParamViewDelegate::Type_Label): {
        QLabel* cob = static_cast<QLabel*>(editor);
        value = cob->text();
        break;
    }
    case (CustomParamViewDelegate::Type_LinEdit): {
        break;
    }
    default:
        break;
    }
    bool ret = model->setData(index, value, Qt::EditRole);//, Qt::UserRole + 1);
}

代理头文件

CustomParamViewDelegate.h

#pragma once

#include <QItemDelegate>
#include <QStyledItemDelegate>
#include <QComboBox>
#include <QApplication>
#include <QLabel>
#include <QLineEdit>

class CustomParamViewDelegate  : public QStyledItemDelegate
{
    Q_OBJECT
public:
    enum ItemDelegate {
        Type_ComboBox = 1,
        Type_Label,
        Type_LinEdit
    };

    CustomParamViewDelegate(QObject *parent);
    ~CustomParamViewDelegate();

    // 创建编辑器
    virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    // 设置编辑器数据
    virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override;
    // 更新编辑器集合属性
    virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    // 设置模型数据
    virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
};

使用源文件和头文件文件

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QStandardItemModel>
#include  "CustomParamViewDelegate.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    QStandardItemModel *m_paramViewMode;
    CustomParamViewDelegate *m_delegateParam;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //tableView是ui上的tabelview插件
    m_paramViewMode = new QStandardItemModel;
    m_paramViewMode = new QStandardItemModel( ui->tableView);
    m_paramViewMode->setHorizontalHeaderLabels(QStringList() << "1" << "2");
    m_delegateParam = new CustomParamViewDelegate( ui->tableView);
    ui->tableView->setItemDelegate(m_delegateParam);
    ui->tableView->horizontalHeader()->setStretchLastSection(true);
    ui->tableView->setModel(m_paramViewMode);

    QStandardItem* itemLabel = new QStandardItem;
    itemLabel->setData(CustomParamViewDelegate::Type_Label, Qt::UserRole);
    itemLabel->setData(QString("%1").arg(0), Qt::UserRole + 1);

    QStandardItem* itemBox = new QStandardItem;
    itemBox->setData(CustomParamViewDelegate::Type_ComboBox, Qt::UserRole);
    itemBox->setData("1,2,3,4,5", Qt::UserRole + 1);

    m_paramViewMode->setItem(0, 0, itemLabel);
    m_paramViewMode->setItem(0, 1, itemBox);

    //默认显示
    ui->tableView->openPersistentEditor(m_paramViewMode->index(0, 0));
    ui->tableView->openPersistentEditor(m_paramViewMode->index(1, 1));
}

Widget::~Widget()
{
    delete ui;
}


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

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

相关文章

postgresql16 物理复制与逻辑复制的实现和对比

本文面向想要练习 PostgreSQL 中数据库复制基础知识但可能无法访问远程服务器的初学者。我认为学习新技术时&#xff0c;在自己的机器上运行示例以巩固概念是至关重要的。对于副本来说&#xff0c;这可能很困难&#xff0c;因为许多可用的资源假设用户具有一定的 PostgreSQL 经…

快乐学Python,如何使用爬虫从网页中提取感兴趣的内容?

前面的内容&#xff0c;我们了解了使用urllib3和selenium来下载网页&#xff0c;但下载下来的是整个网页的内容&#xff0c;那我们又怎么从下载下来的网页中提取我们自己感兴趣的内容呢&#xff1f;这里就需要Python的另一个库来实现-BeautifulSoup。 BeautifulSoup 是一个 Py…

【Py/Java/C++三种语言详解】LeetCode每日一题240115【链表】LeetCode82、删除排序链表中的重复节点II

文章目录 题目链接题目描述解题思路代码pythonJavaC时空复杂度 华为OD算法/大厂面试高频题算法练习冲刺训练 题目链接 LeetCode82、删除排序链表中的重复节点II 题目描述 给定一个已排序的链表的头 head &#xff0c; 删除原始链表中所有重复数字的节点&#xff0c;只留下不…

Hotspot源码解析-第十九章-ClassLoaderData、符号表、字符串表的初始化

第十九章-ClassLoaderData初始化 讲解本章先从一张图开始 众所周知&#xff0c;Java类的相关信息都是存储在元空间中的&#xff0c;但是是怎么存储的&#xff0c;相信很多读者是不清楚的&#xff0c;这里就不得不涉及到ClassLoaderDataGraph、classLoader、classLoaderData&…

容器化postgres备份策略

文章目录 1. 策略和背景1.1 背景1.2 备份策略 2. docker-compose的修改2.1 挂载备份目录2.2 备份脚本3.3 重启容器 3. 定时任务 1. 策略和背景 1.1 背景 使用docker-compose管理的postgres数据库需要备份工作目录在 /data/postgres下 1.2 备份策略 要备份的库 shu_han 库 每…

vue3中组合式api的常用方法

vue3中组合式api的常用方法 记录一下vue3中常用的组合式api&#xff0c;包括计算属性computed、监听器watch及watchEffective 一、computed 作用&#xff1a;根据已有数据计算出新数据&#xff08;和Vue2中的computed作用一致&#xff09;。 <template><div class&…

JavaScript 异步编程解决方案-中篇

天下事有难易乎&#xff1f; 为之&#xff0c;则难者亦易矣&#xff1b;不为&#xff0c; 则易者亦难矣。人之为学有难易乎&#xff1f; 学之&#xff0c;则难者亦易矣&#xff1b;不学&#xff0c;则易者亦难矣。 async 函数 和promise then的规则一样 async function fun() …

HarmonyOS4.0——ArkUI应用说明

一、ArkUI框架简介 ArkUI开发框架是方舟开发框架的简称&#xff0c;它是一套构建 HarmonyOS / OpenHarmony 应用界面的声明式UI开发框架&#xff0c;它使用极简的UI信息语法、丰富的UI组件以及实时界面语言工具&#xff0c;帮助开发者提升应用界面开发效率 30%&#xff0c;开发…

element + table 每两行对比相同值列合并

在开始之前先要明确几个概念&#xff1a; 保持不变&#xff1a;{ rowspan: 1, colspan: 1 } 删除一个单元格&#xff1a;{ rowspan: 0, colspan: 0 } 合并一个单元格&#xff1a;{ rowspan: 2, colspan: 1 } <template><div><el-table:data"tableData&quo…

二叉树的遍历 Java

二叉树的遍历 递归法前序遍历中序遍历后序遍历改进 迭代法前序、后序遍历中序遍历 Java 中 null、NULL、nullptr 区别 public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) { this.val val; }TreeNode(int val, TreeNode left, Tree…

LLM推理部署(七):FireAttention——通过无损量化比vLLM快4倍

Mixtral作为第一个在数万亿tokens上训练的OSS模型&#xff0c;最近在人工智能社区掀起了波澜&#xff0c;它支持“混合专家”&#xff08;MoE&#xff09;&#xff0c;并且训练和推理速度非常快。 Fireworks AI是第一个托管Mixtral的平台&#xff0c;在Mixtral公开发布之前就托…

小程序中使用微信同声传译插件实现语音识别、语音合成、文本翻译功能----语音识别(一)

官方文档链接&#xff1a;https://mp.weixin.qq.com/wxopen/plugindevdoc?appidwx069ba97219f66d99&token370941954&langzh_CN#- 要使用插件需要先在小程序管理后台的设置->第三方设置->插件管理中添加插件&#xff0c;目前该插件仅认证后的小程序。 语音识别…

༺༽༾ཊ—游戏-01_2D-开发—ཏ༿༼༻

首先利用安装好的Unity Hub创建一个unity 2D&#xff08;URP渲染管线&#xff09;项目 选择个人喜欢的操作格局&#xff08;这里采用2 by 3&#xff09; 在Project项目管理中将双栏改为单栏模式&#xff08;个人喜好&#xff09; 找到首选项&#xff08;Preferences&#xff09…

2024 解决matplotlib中文字体问题

第一种代码&#xff08;失败代码&#xff09; import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.font_manager import FontPropertiesfont_path /Users/huangbaixi/Desktop/SimHei.ttfdef plot_demo():#print(mpl.get_cachedir())# 绘制折线图font…

【记忆化搜索】

欢迎来到Cefler的博客&#x1f601; &#x1f54c;博客主页&#xff1a;那个传说中的man的主页 &#x1f3e0;个人专栏&#xff1a;题目解析 &#x1f30e;推荐文章&#xff1a;【LeetCode】winter vacation training 前言 记忆化搜索是一种优化搜索算法的方法&#xff0c;它可…

Apache StringUtils:Java字符串处理工具类

简介 在我们的代码中经常需要对字符串判空&#xff0c;截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。如果只用 String 类提供的那些方法&#xff0c;我们需要手写大量的额外代码&#xff0c;不然容易出现各种异常。现在有…

GL Logger和CANFDLog-OTL-128两款记录仪都是如何实现高效的报文录制的?

GL Logger是Vector推出的记录CAN/CAN FD、LIN、FlexRay和MOST数据通信的工具。以GL2400为例带着大家一步步地实现路试过程中通过整车OBD口进行CAN/CANFD报文的录制。 Step1 设备配置 设备配置即设备录制方式、录制内容、设备休眠唤醒策略等。 ▷ 打开Vector Logger Configurat…

调试ad5245的总结

调试ad5245的总结 这个ad5245是通过IIC与FPGA进行通信的&#xff0c;首先要理解IIC协议。 经验总结&#xff1a; 1、SCL和SDA端的要有上拉电阻&#xff0c;且上拉电阻能正常工作&#xff1b; 2、要往SDA数据线上写三个字节才能调节ad5245的电阻值&#xff0c;第三个字节就是…

html中的flex是什么?——弹性布局

在HTML中&#xff0c;flex是一种布局方式&#xff0c;用于处理容器中的子元素的布局。它是CSS3的一部分&#xff0c;也被称为弹性布局。 通过使用flex布局&#xff0c;可以将容器中的子元素进行灵活的定位和扩展&#xff0c;以适应不同的屏幕尺寸和设备。它提供了一种简单而强…

flink 1.18 sql gateway /sql gateway jdbc

一 sql gateway 注意 之所以直接启动gateway 能知道yarn session 主要还是隐藏的配置文件&#xff0c;但是配置文件可以被覆盖&#xff0c;多个session 保留最新的applicationid 1 安装flink &#xff08;略&#xff09; 2 启动sql-gatway(sql-gateway 通过官网介绍只能运行…