当前位置:网站首页>How to use QTableWidget

How to use QTableWidget

2022-08-11 09:04:00 Mayfly Wings*

行列

添加表头,隐藏/显示 行号

//设置列表头
QStringList  labels;
labels <<" "<< QStringLiteral("编号") << QStringLiteral("速度") << QStringLiteral("操作") << " ";
ui.tableWidget->setColumnCount(5);
ui.tableWidget->setHorizontalHeaderLabels(labels);
 
//显示行号列
QHeaderView* headerView = ui->tableWidget->verticalHeader();
headerView->setHidden(false); //false 显示行号列 true Hide

关于QTableWidget The reason why the header setting is invalid

Invalid headers are generally due to The number of columns is not set;
调用 setColumnCount 后,再调用 setHorizontalHeaderLabels 即可生效;;

当然最好的情况是 setHorizontalHeaderLabels When the number of columns is automatically set according to the size of the parameter; 但QT未支持;

Adjust header size and alignment

  tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);//使列完全填充并平分

  tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);//行自适应宽度 

  tableWidget->resizeColumnsToContents(); //根据内容调整列宽

  tableWidget->resizeColumnToContents(int col);//根据内容自动调整给定列宽

  tableWidget->horizontalHeader()->setResizeMode//把给定列设置为给定模式

  //主要模式有Stretch和Fixed

QTableWidgetColumn width can be set as desired

tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //x先自适应宽度
tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);     //然后设置要根据内容使用宽度的列

QTableWidgetThe column width can be freely set as a reference

Header style reference

Remove the vertical line dividing the header
在这里插入图片描述

 ui.tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{border:none;background-color:rgb(255,255,255);color: black;height: 32px;}");

添加数据

ui.tableWidget->setRowCount(rowNum);
for ( ...)
{
    
	QTableWidgetItem* pRowItem =new QTableWidgetItem(row);
	pRowItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//单元格内容居中对齐
	ui.tableWidget->setItem(row, 1, pRowItem);

	QString strPositionName = item.vecColumnInfo.at(0).strText;
	QTableWidgetItem* pName = new QTableWidgetItem(strPositionName);
	pName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	ui.tableWidget->setItem(row, 2, pName);
}

需要注意:
使用setItemSet the number of rows before inserting data:ui.tableWidget->setRowCount(rowNum);
也可以在forDynamically set the number of rows inside

for(   )
{
    
	...
	ui.tableWidget->setRowCount(row+1);
	ui.tableWidget->setItem(row, ...);
}

Inserted data is not displayed

1、如果是调用insertRowInsert row,再调setRowHeightJust set the row height.
2、如果不是,那可能是rowCount返回的是0,This needs to be adjustedsetRowCountSet the number of lines or tuneinsertRow插入行,再调setRowHeightJust set the row height

QTableWidget中添加QPushButton

 // 创建QPushButton控件
QPushButton *pBtn = new QPushButton();  

// 绑定信号
connect(pBtn, SIGNAL(clicked()), this, SLOT(OnBtnClicked()));  

// 在QTableWidget中添加控件
tableWidget->setCellWidget(0,0,pBtn); 

// The bound response function
void OnBtnClicked(void)  
{
      
    QPushButton *senderObj=qobject_cast<QPushButton*>(sender());  
    if(senderObj == nullptr)  
    {
      
        return;  
    }  
    QModelIndex idx =tableWidget->indexAt(QPoint(senderObj->frameGeometry().x(),senderObj->frameGeometry().y()));  
    int row=idx.row();  
    // Additional response information......
}

样式

去掉网格线

ui.tableWidget->setShowGrid(false);

去掉 QTabWidget 的边框

ui.tableWidget->setStyleSheet("QTableWidget{border: none;}")

例子

Remove the header dividing line,Remove table borders and gridlines,Only the horizontal line remains
在这里插入图片描述

	ui.tableWidget->setShowGrid(false);
	ui.tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{border:none;background-color:rgb(255,255,255);color: black;height: 32px;}");
	ui.tableWidget->setStyleSheet(
		"QTableWidget{border: none;}"
		"QTableWidget::Item{border:0px solid rgb(255,255,255);border-bottom:1px solid rgb(232,232,232);background-color:rgb(255,255,255);color: rgb(89,89,89);}"
		"QTableWidget::Item:selected{background-color:rgb(255,255,255);color: rgb(24,144,255);}"
	);

QTableWidget的用法总结

QTabWidget 样式表

原网站

版权声明
本文为[Mayfly Wings*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110856164459.html