QTableWidget自定义单元格

news2024/10/5 19:19:20

一 自定义QTableWidget

创建一个Widget项目,注释掉其中的ui->setupUi(this);使用自定义的布局。

#include "widget.h"
#include "ui_widget.h"
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    v->addWidget(table);
    this->setLayout(v);

}

当前的执行效果如下所示:

二 自定义QLineEdit单元格

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    //新加代码
    table->setRowCount(1);
    table->setCellWidget(0,0,new QLineEdit());
    //结束

    v->addWidget(table);
    this->setLayout(v);

}

效果如下所示 

 三 自定义QComboBox单元格

#include "ui_widget.h"
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QComboBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    //新加代码
    table->setRowCount(1);
    QComboBox * comboBox = new QComboBox();
    QStringList itemList;
    itemList.append("item1");
    itemList.append("item2");
    itemList.append("item3");
    comboBox->addItems(itemList);
    table->setCellWidget(0,0,comboBox);
    //结束

    v->addWidget(table);
    this->setLayout(v);

}

 效果:

   四 自定义QCheckBox 单元格

#include "widget.h"
#include "ui_widget.h"
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QComboBox>
#include <QCheckBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    //新加代码
    QCheckBox *checkBox = new QCheckBox();
    table->setRowCount(1);
    table->setCellWidget(0,0,checkBox);
    //结束

    v->addWidget(table);
    this->setLayout(v);

}

运行效果:

   五 自定义QCheckBox 单元格

#include "widget.h"
#include "ui_widget.h"
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QComboBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QFrame>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    //新加代码
    table->setRowCount(1);
    QFrame * frame = new QFrame();
    QHBoxLayout *h = new QHBoxLayout();
    QRadioButton *radioButton_1 = new QRadioButton("boy");
    radioButton_1->setChecked(true);
    QRadioButton *radioButton_2 = new QRadioButton("girl");
    h->addWidget(radioButton_1);
    h->addWidget(radioButton_2);
    frame->setLayout(h);
    table->setCellWidget(0,0,frame);
    //结束

    v->addWidget(table);
    this->setLayout(v);

}

运行效果:当前的单选按钮显示的效果看起来像两个小括号,而不是一个圆圈,如何优化呢?

 解决办法:

QTableWidget可以通过设置自适应大小来自动调整表格的大小以适应内容。可以使用setSizePolicy()方法来设置表格的大小策略,例如:

tableWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

这将使表格在水平和垂直方向上都自适应大小。另外,还可以使用resizeColumnsToContents()和resizeRowsToContents()方法来自动调整列和行的大小以适应内容。

办法1:添加如下代码,在我的电脑上,这个没达到预期的效果

table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

办法2,添加如下代码:

    table->resizeRowToContents(0);
    table->resizeColumnToContents(0);

添加后的执行效果:不错,单选按钮终于变成圆圈了。

六 自定义QDateTimeEdit单元格

#include "widget.h"
#include "ui_widget.h"
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QComboBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QFrame>
#include <QDateTimeEdit>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    //新加代码

    table->setRowCount(3);
    QDateTime now = QDateTime::currentDateTime();
    QDateTimeEdit *dateTimeEdit = new QDateTimeEdit(now);
    table->setCellWidget(0,0,dateTimeEdit);
    QDateEdit *dateEdit = new QDateEdit(now.date());
    QTimeEdit *timeEdit = new QTimeEdit(now.time());
    table->setCellWidget(0,0,dateTimeEdit);
    table->setCellWidget(1,0,dateEdit);
    table->setCellWidget(2,0,timeEdit);
    //结束
    for(int i = 0;i < table->rowCount();i++){
        table->resizeRowToContents(i);
        table->resizeColumnToContents(0);
    }
    v->addWidget(table);
    this->setLayout(v);
}

执行效果:

 七 自定义QFrame+QDial+QSpinBox+QLabel 单元格

为了将QDial的valueChanged(int)信号传递给QLabel的槽setText(const QString &),自定一个一个QLabel的子类MyLabel。如下所示:

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QObject>
#include <QWidget>
#include <QLabel>

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    MyLabel(QWidget *parent):QLabel(parent){
    }
    MyLabel(const QString &text,QWidget *parent):QLabel(text,parent){
    }
    MyLabel(){}
public slots:
    void setValue(int value){
        this->setText(QString("%1").arg(value));
    }
};

#endif // MYLABEL_H

在下面这个实例中,通过信号槽机制,实现了QDIal空间和QSpinBox空间值的联动。 

#include <QVBoxLayout>
#include <QComboBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QFrame>
#include <QDateTimeEdit>
#include <QDial>
#include <QLabel>
#include "mylabel.h"
#include <QSpinBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QVBoxLayout *v = new QVBoxLayout(this);
    //ui->setupUi(this);

    QTableWidget * table = new QTableWidget(this);
    table->setRowCount(0);
    table->setColumnCount(1);
    QTableWidgetItem *head_item1 = new QTableWidgetItem(QString::fromLocal8Bit("列1"));

    table->setHorizontalHeaderItem(0,head_item1);

    //新加代码

    table->setRowCount(1);
    QFrame *frame1 = new QFrame();
    QVBoxLayout *v1 = new QVBoxLayout();
    QDial *dial = new QDial();
    dial->setMinimum(1);
    dial->setMaximum(10);

    dial->setNotchesVisible(true);
    //dial->setWrapping(true);

    MyLabel *label1 = new MyLabel();
    connect(dial,&QDial::valueChanged,label1,&MyLabel::setValue);

    QSpinBox *spinBox_1 = new QSpinBox();
    spinBox_1->setRange(1,10);

    connect(dial,&QDial::valueChanged,spinBox_1,&QSpinBox::setValue);
    connect(dial,&QDial::valueChanged,label1,&MyLabel::setValue);
    connect(spinBox_1,SIGNAL(valueChanged(int)),dial,SLOT(setValue(int)));

    v1->addWidget(dial);
    v1->addWidget(label1);
    v1->addWidget(spinBox_1);
    frame1->setLayout(v1);
    emit dial->setValue(5);

    table->setCellWidget(0,0,frame1);
    //结束
    for(int i = 0;i < table->rowCount();i++){
        table->resizeRowToContents(i);
        table->resizeColumnToContents(0);
    }
    v->addWidget(table);
    this->resize(300,400);
    this->setLayout(v);
}

 效果图:

小结

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

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

相关文章

Vue.js中的provide和inject方法是什么,有什么区别

Vue.js中的provide和inject方法 在Vue.js中&#xff0c;provide和inject是用于父组件向子组件传递数据的一种技术。通过使用provide和inject&#xff0c;我们可以在组件树中任意层次的组件之间进行数据的传递和共享&#xff0c;从而实现复杂的数据交互和状态管理的需求。本文将…

FANUC机器人MODBUS TCP通信配置方法(示教器实物演示)

FANUC机器人MODBUS TCP通信配置方法(示教器实物演示) 机器人一侧的配置: 如下图所示,示教器上找到设置—主机通讯, 如下图所示,选择第一项TCP/IP,点击详细进入配置界面, 如下图所示,设置机器人端口1#的IP地址为192.168.1.10,子网掩码:255.255.255.0 如下图所示…

【深入理解Linux内核锁】一、内核锁的由来

我的圈子&#xff1a; 高级工程师聚集地 我是董哥&#xff0c;高级嵌入式软件开发工程师&#xff0c;从事嵌入式Linux驱动开发和系统开发&#xff0c;曾就职于世界500强公司&#xff01; 创作理念&#xff1a;专注分享高质量嵌入式文章&#xff0c;让大家读有所得&#xff01; …

Java Web——使用Filter实现用户登录

实验名称&#xff1a; 使用Filter实现用户登录 实验目的&#xff1a; &#xff08;1&#xff09;了解什么是Filter。 &#xff08;2&#xff09;熟悉Filter的拦截过程和接口中的方法。 &#xff08;3&#xff09;掌握第一个Filter程序的编写方法。 &#xff08;4&#xf…

华为荣获上海市技术发明一等奖!基于CANN的视频增强平台以AI技术修复历史视频

2023年5月26日&#xff0c;上海市科学技术奖励大会隆重召开&#xff0c;由上海交通大学牵头&#xff0c;中国科学院深圳先进技术研究院、咪咕视讯科技有限公司、华为技术有限公司、上海云视科技股份有限公司、上海人工智能创新中心、上海媒智科技有限公司、上海数字电视国家工程…

Oracle免费云设置Multi-factor Authentication

申请Oracle免费云账号的时候系统就强迫用户设置Multi-factor Authentication&#xff0c;我选择了OracleMobileAuthenticator作为2次认证工具。刚开始用还顺利&#xff0c;但用了一段时间后Oracle登录页面迟迟未向OracleMobileAuthenticator发送通知&#xff0c;要等非常久App才…

大模型 LLM 综述, A Survey of Large Language Models

大模型 LLM 综述, A Survey of Large Language Models 一、概述 一般认为NLP领域的大模型>10 Billion参数(也有人认为是6B、7B, 工业界用, 开始展现涌现能力); 经典大模型有GPT-3、BLOOM、Flan-T5、GPT-NeoX、OPT、GLM-130B、PaLM、LaMDA、LLaMA等; 大模型时间线, 图来自…

chatgpt赋能python:Python绘图教程:将画笔移动到绝对位置的方法

Python绘图教程&#xff1a;将画笔移动到绝对位置的方法 Python作为一门高级编程语言&#xff0c;设计初衷是让编程变得简单、易学、易用&#xff0c;且支持多种编程范式&#xff0c;其中产生了让人惊艳的绘图模块——Turtle&#xff08;海龟&#xff09;。 在这篇教程中&…

Mybatis-plus代码生成器

官网&#xff1a;MyBatis-Plus (baomidou.com) 顾名思义&#xff0c;就是它为你生成代码&#xff0c;这里可以为你生成Entity、Mapper、MapperXML、Service、controller等各个模块的代码&#xff0c;极大的提升开发效率 环境&#xff1a;mysql8.0.24、mybatis-plus3.5.3.1、spr…

<<Linux多线程服务端编程>>学习之栏1————线程安全的对象生命期管理

线程安全的对象生命期管理 此章节开头的前两句话&#xff0c;把我点醒&#xff0c;原来思考功力可以这么深厚&#xff01;如下&#xff1a; 第一句话&#xff1a; 编写线程安全的类不是难事&#xff0c; 用同步原语保护内部状态即可&#xff1b; 第二句话&#xff1a; 但是对…

【论文阅读】ControlNet

简介 目标&#xff1a;加入额外的条件&#xff08;例如边缘图像&#xff0c;深度图像&#xff09;控制生成的图像 现有挑战 特定领域上的数据较少&#xff0c;而预训练模型很大&#xff0c;很容易出现过拟合的情况。在资源有限的情况下&#xff0c;只能选择pretrain- finetun…

传统工业制造企业如何实现数字化转型?

传统工业制造企业如何实现数字化转型&#xff0c;以数字驱动、实现高价值管理&#xff1f; 传统企业实现数字化转型是一条很漫长但不得不走的道路&#xff0c;看到这个问题下有很多专业人士对传统企业如何做数字化转型都提出了专业的见解&#xff0c;所以这篇就以传统制造业为…

用ChatGPT来写高考作文,看看效果!

又是一年高考日&#xff0c;今天高考作文题目一出来&#xff0c;很多人第一时间就用AI进行写作&#xff0c;我这边也用gpt3和4分别生成了一篇文章&#xff0c;没有给他投喂范文&#xff0c;把要求和题目的prompt给它&#xff0c;让它分析和写作&#xff0c;来看看效果吧。 GPT4…

学习态度记录JRebel本地验证

学习态度记录JRebel本地验证 网上有两种方式。 1、直接使用其他同学提供好的远程服务器验证地址(可自行搜索) 2、设置本地反向代理&#xff0c;激活JRebel ps&#xff1a;我的IDEA升级到2023.1.2后无法使用第一种方式了。搜了半天网上都是基于windows环境的教程解说&#xff0…

html 原生js手写树 仿照antd 样式

效果如图 <!doctype html> <html><head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"widthdevice-width, initial-scale1.0"…

百度Apollo视频学习笔记

APOLLO视频学习笔记 一、总览 无人驾驶车的运作方式 五个核心部件&#xff1a; 计算机视觉&#xff1a;弄清楚周围的世界是怎样的传感器融合&#xff1a; 合并来自其他传感器的数据&#xff0c;如激光和雷达&#xff0c;更加深入了了解我们周围的环境定位&#xff1a;精确地…

一级建造师执业资格考试--工程法规--速学36记--联想法

第一记&#xff1a;法的效力层级 第二记&#xff1a;法人的分类 【速记方法】口诀&#xff1a;赚钱营利和特别 关键词&#xff1a;营利、特别 【速记内容】 1、营利法人:以取得利润并分配给股东等出资人为目的成立的法人,为营利法人; 如有限责任公司、股份有限公司。经依法登记…

什么牌子电容笔性价比高?iPad触屏笔推荐

电容笔已经成为日常生活中不可或缺的一部分。它可以用于书写&#xff0c;绘画&#xff0c;甚至玩游戏。使用电容笔可以代替传统无纸化书写&#xff0c;提高工作效率。市面上有许多不同价格的电容笔品牌可供选择。本文将介绍四款性价比高的平替电容笔&#xff0c;有需要入手的小…

常用的三种拖拽方法(内置方法 + 接口 + Event Trigger组件)

前言 在Unity中实现拖拽的方法有多种&#xff0c;以下是几种常见的方法和它们的优缺点&#xff1a; Input.GetMouseButtonDown Input.GetMouseButtonDown 方法可以监测用户鼠标按键的点击事件&#xff0c;通过检测鼠标按钮的状态来实现拖拽效果。用户通过鼠标进行拖拽操作。…

Web基本概念

一、前言 World Wide Web的简称&#xff0c;是一个由许多互相链接的超文本组成的系统&#xff0c;通过互联网访问 &#xff08;为用户提供信息&#xff09; 静态网页 仅适用于不能经常更改内容的网页&#xff1b; 动态网页 网络编程技术创建的页面&#xff1b;通过在传统的静态…