当前位置:网站首页>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
边栏推荐
- 102. Sequence traversal of binary tree
- The ultimate experience, the audio and video technology behind the tiktok
- 2022制冷与空调设备运行操作判断题及答案
- uni-app黑马优购项目学习记录(下)
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
- Add drag and drop function to El dialog
- Leak detection and vacancy filling (VII)
- Future usage details
- HCIP第五次实验
猜你喜欢

EasymodbusTCP之clientexample解析

Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)

2022年茶艺师(初级)考试模拟100题及模拟考试

102. Sequence traversal of binary tree

2022 judgment questions and answers for operation of refrigeration and air conditioning equipment

Chrome浏览器的跨域设置----包含新老版本两种设置

2021 Great Wall Cup WP
![[appium] write scripts by designing Keyword Driven files](/img/05/536701f39dcf8474e90e58738f2094.png)
[appium] write scripts by designing Keyword Driven files

MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】

470. 用 Rand7() 实现 Rand10()
随机推荐
In JS, t, = > Analysis of
极致体验,揭晓抖音背后的音视频技术
Listen for click events other than an element
2022年茶艺师(初级)考试模拟100题及模拟考试
C1 notes [task training part 2]
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
198. Looting - Dynamic Planning
JVM class loading mechanism
JS implementation private attribute
Construction of functions in C language programming
开期货,开户云安全还是相信期货公司的软件?
C1小笔记【任务训练篇一】
On the method of outputting the complete name of typeID from GCC
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
Halo open source project learning (II): entity classes and data tables
Go对文件操作
開期貨,開戶雲安全還是相信期貨公司的軟件?
cartographer_ There is no problem compiling node, but running the bug that hangs directly
Summary of floating point double precision, single precision and half precision knowledge
SystemVerilog (VI) - variable