当前位置:网站首页>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
边栏推荐
- 油猴网站地址
- 386. Dictionary order (medium) - iteration - full arrangement
- 48. 旋转图像
- JS interview question: FN call. call. call. Call (FN2) parsing
- Detailed deployment of flask project
- 41. 缺失的第一个正数
- Hcip fifth experiment
- On the method of outputting the complete name of typeID from GCC
- 开源按键组件Multi_Button的使用,含测试工程
- SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
猜你喜欢

uni-app黑马优购项目学习记录(下)

102. 二叉树的层序遍历

Index: teach you index from zero basis to proficient use

QT modification UI does not take effect

Gaode map search, drag and drop query address

高德地图搜索、拖拽 查询地址

Halo open source project learning (II): entity classes and data tables

48. 旋转图像

470. Rand10() is implemented with rand7()

C1小笔记【任务训练篇一】
随机推荐
Gaode map search, drag and drop query address
402. Remove K digits - greedy
2021 Great Wall Cup WP
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
Remember using Ali Font Icon Library for the first time
C1 notes [task training part 2]
QT modification UI does not take effect
MySQL advanced index [classification, performance analysis, use, design principles]
开期货,开户云安全还是相信期货公司的软件?
Hcip fifth experiment
JS interview question: FN call. call. call. Call (FN2) parsing
2022 tea artist (primary) examination simulated 100 questions and simulated examination
122. The best time to buy and sell stocks II - one-time traversal
On the method of outputting the complete name of typeID from GCC
239. Maximum value of sliding window (difficult) - one-way queue, large top heap - byte skipping high frequency problem
[二叉数] 二叉树的最大深度+N叉树的最大深度
Element calculation distance and event object
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
Add animation to the picture under V-for timing
Learning record of uni app dark horse yougou project (Part 2)