【QT-3】tableWidget控件

本文介绍了如何在Qt环境中使用QTableWidget控件,包括设置表格样式、交互行为、数据插入、单元格内容居中、错误处理等方面。通过示例代码展示了如何自定义单元格样式、实现整行选中、插入数据、清除和删除表格内容,并解释了QTableWidget的信号与槽机制。此外,还提供了针对'QTableWidget: cannot insert an item that is already owned by another QTableWidget'错误的解决方案。

1、在**.ui文件中拖入tableWidget控件

 2、示例代码

表格样式:

//Table Widget控件
//设置列数
ui->tableWidget->setColumnCount(5);

//表头高度
ui->tableWidget->horizontalHeader()->setMinimumHeight(40);

QString qssTV = "QTableWidget::item:hover{background-color:rgb(92,188,227,200)}"
	"QTableWidget::item:selected{background-color:#1B89A1}"
	"QHeaderView::section,QTableCornerButton: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); }"
"QTableWidget{background-color:rgb(211, 211, 211);border:2px;}";

//ui->tableWidget->setShowGrid(true); //设置显示格子线
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止编辑
//ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//表头等宽
ui->tableWidget->horizontalHeader()->setFont(QFont("song", 12));

//点击表时不对表头行光亮(获取焦点)
ui->tableWidget->horizontalHeader()->setHighlightSections(false);

//设置表头字体加粗
QFont font = ui->tableWidget->horizontalHeader()->font();
font.setBold(true);
ui->tableWidget->horizontalHeader()->setFont(font);

ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:lightblue;}"); //skyblue设置表头背景色
ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色

ui->tableWidget->setStyleSheet(qssTV);

ui->tableWidget->horizontalHeader()->setHighlightSections(false);         //点击表头时不对表头光亮
ui->tableWidget->setSelectionMode(QAbstractItemView:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值