当前位置:网站首页>QTableWidget使用讲解
QTableWidget使用讲解
2022-04-23 17:52:00 【liu_jie_bin】
QTableWidget简介
QTableWidget类提供了一个带有默认模型的基于项的表视图。Table部件为应用程序提供标准的显示工具。QTableWidget中的项由QTableWidgetItem提供。
效果
.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_btnAdd_clicked();
void on_btnClear_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
.ccp文件
#include "widget.h"
#include "ui_widget.h"
#include<QSpinBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->comboBox->addItem(QStringLiteral("男"));
ui->comboBox->addItem(QStringLiteral("女"));
//设置列数
ui->tableWidget->setColumnCount(3);
//设置行数
//ui->tableWidget->setRowCount(15);
//设置垂直表头不可见
ui->tableWidget->verticalHeader()->setVisible(false);
//表头标题用QStringList来表示
QStringList headerText;
headerText<<QStringLiteral("名字")<<QStringLiteral("性别")<<QStringLiteral("年龄");
ui->tableWidget->setHorizontalHeaderLabels(headerText);
//设置不可编辑
ui->tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
//设置选中整行模式
ui->tableWidget->setSelectionBehavior(QTableWidget::SelectRows);
//设置单选模式
ui->tableWidget->setSelectionMode(QTableWidget::SingleSelection);
//开启交替行背景色
ui->tableWidget->setAlternatingRowColors(true);
//设置内容自适应宽度
//ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
//设置最后一栏自适应长度
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
//设置表头风格
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{padding:3px; margin:0px; color:#DCDCDC; border:1px solid #242424; \ border-left-width:0px; border-right-width:1px; border-top-width:0px; border-bottom-width:1px; \ background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #646464,stop:1 #525252);}");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btnAdd_clicked()
{
//获取行数
int rowCount = ui->tableWidget->rowCount();
//插入行
ui->tableWidget->insertRow(rowCount);
//添加子项QSpinBox
QSpinBox *ages = new QSpinBox();
ages->setValue(24);
ui->tableWidget->setCellWidget(rowCount,2, ages);
QString strName = ui->lineEdit->text();
//添加名字子项
QTableWidgetItem *nameItem = new QTableWidgetItem(strName);
//设置单个item属性
nameItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QFont font = nameItem->font();
font.setBold(true);
nameItem->setTextColor(QColor(0,0,255));
//添加
ui->tableWidget->setItem(rowCount,0,nameItem);
//添加性别子项
QString strSex = ui->comboBox->currentText();
//设置单个item属性
QTableWidgetItem *sexItem = new QTableWidgetItem(strSex);
sexItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
sexItem->setTextColor(QColor(255,0,0));
//添加
ui->tableWidget->setItem(rowCount,1,sexItem);
}
void Widget::on_btnClear_clicked()
{
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
}
ui布局
版权声明
本文为[liu_jie_bin]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44901043/article/details/124087285
边栏推荐
- Chrome浏览器的跨域设置----包含新老版本两种设置
- 圆环回原点问题-字节跳动高频题
- [appium] write scripts by designing Keyword Driven files
- Kubernetes 服务发现 监控Endpoints
- Special effects case collection: mouse planet small tail
- Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
- Utilisation de la liste - Ajouter, supprimer et modifier la requête
- 2022 tea artist (primary) examination simulated 100 questions and simulated examination
- 土地覆盖/利用数据产品下载
- C1 notes [task training chapter I]
猜你喜欢
Gaode map search, drag and drop query address
JVM class loading mechanism
Halo open source project learning (II): entity classes and data tables
01 - get to know the advantages of sketch sketch
Allowed latency and side output
Kubernetes service discovery monitoring endpoints
Leak detection and vacancy filling (6)
Halo open source project learning (II): entity classes and data tables
92. 反转链表 II-字节跳动高频题
Applet learning notes (I)
随机推荐
Summary of floating point double precision, single precision and half precision knowledge
Future 用法详解
92. 反转链表 II-字节跳动高频题
列錶的使用-增删改查
440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
干货 | 快速抽取缩略图是怎么练成的?
Ring back to origin problem - byte jumping high frequency problem
470. 用 Rand7() 实现 Rand10()
HCIP第五次实验
Sword finger offer 03 Duplicate number in array
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
Commonly used functions -- spineros:: and spineros::)
198. 打家劫舍-动态规划
Add animation to the picture under V-for timing
Timestamp to formatted date
Go对文件操作
41. The first missing positive number
Hcip fifth experiment
Listen for click events other than an element
【Appium】通过设计关键字驱动文件来编写脚本