一、表头操作
QStringList header;
pTable->setRowCount(0);
pTable->setColumnCount(header.size());
pTable->setHorizontalHeaderLabels(header);
for(int i = 0; i < headerInfo.size(); i++)
pTable->setColumnWidth(i, width); //设置列宽
二、插入操作
ui->table_prj->blockSignals(true); //阻塞消息
ui->table_prj->clearTable();
// 更新表格
for (int i=0; i< data.size(); i++)
{
int iRow = ui->table_prj->rowCount();
ui->table_prj->insertRow(iRow);
ui->table_prj->setRowHeight(iRow, 50); //设置行高
//QColor color = getReagStateColor(reag);
int nColumn = ui->table_prj->columnCount();
for(int col = 0; col < nColumn; col++)
{
QTableWidgetItem *pItem = new QTableWidgetItem;
if(0 == col)
pItem->setData(Qt::UserRole + 1, itemRole); //设置自定义数据
QString strShowText = itemRole.value(strHeadKey).toString();
pItem->setText(strShowText);
//pItem->setBackgroundColor(color); //设置背景色
ui->table_prj->setItem(iRow, col, pItem);
}
}
ui->table_prj->blockSignals(false);
三、单选模式下设置多行选中
bool setTableSelectRow(const QList<int> rows)
{
ui->table_prj->blockSignals(true);
ui->table_prj->clearSelection();
ui->table_prj->setSelectionMode(QAbstractItemView::MultiSelection); //设置模式
bool bFlag = false;
for (int i=0; i<ui->table_prj->rowCount(); i++)
{
if(!rows.contains(i))
continue;
ui->table_prj->selectRow(i);
bFlag = true;
}
ui->table_prj->blockSignals(false);
ui->table_prj->setSelectionMode(QAbstractItemView::ExtendedSelection); //恢复单选模式
return bFlag;
}
四、获取选中行文章来源:https://www.toymoban.com/news/detail-724121.html
QSet<int> rows;
const QList<QTableWidgetItem*> items = ui->table_prj->selectedItems();
for(QTableWidgetItem* item : items)
{
if(!rows.contains( item->row()))
{
rows.insert(item->row());
}
}
五、常用QSS文章来源地址https://www.toymoban.com/news/detail-724121.html
/*tabelwidget*/
QTableWidget{
color:#000000;
font-size: 16px;
font-weight: normal;
background:#FFFFFF;
border:0px solid #FFFFFF;
alternate-background-color:#F9F9F9;/*交错颜色*/
gridline-color:#FFFFFF;
height: 50px;
}
/*选中item*/
QTableWidget::item:selected{
color:#000000;
font-size: 16px;
font-weight: bold;
background:#DBFFE4;
}
/*表头*/
QHeaderView::section{
text-align:center;
background:#E6F1EB;
padding:3px;
margin:0px;
right : 10px;
color:#000000;
font-size: 16px;
font-weight: bold;
border:0px solid #FFFFFF;
height: 50px;
}
到了这里,关于QTablewidget 常规操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!