ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置表格内容不可编辑
ui->tableWidget->verticalHeader()->setHidden(true); //隐藏行号列
ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //设置滚动隐藏
ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Fixed); //设置表头不可拖动
ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection); //设置不可选
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
/*************************************
***
***设置表格横竖内容
***
*************************************/
QStringList header;
header<<"序号"<<"名称"<<"设备ID"<<"设备IP"<<"设备端口"<<"型号"<<"厂家"<<"备注";
ui->tableWidget->setHorizontalHeaderLabels(header);
ui->tableWidget->horizontalHeader()->setStyleSheet(
"QHeaderView::section{background-color:rgb(225,225,225);"
"font:10pt '宋体';color: black;};");
ui->tableWidget->setColumnWidth(0,width * 3);
ui->tableWidget->setColumnWidth(1,width * 2);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidget->horizontalHeader()->setFixedHeight(45);
// 1、设置所有的列
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
// 2、设置某一列,比如第一列
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Fixed);
ui->tabWidget->findChildren<QTabBar*>().at(0)->hide();
如果qt版本是5.0以上,还可以使用如下操作:
ui->tabWidget->tabBar()->hide();
如果还是不行可试试:
ui->tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
ui->tableWidget->setRowCount(regInfo.size()+1);
ui->tableWidget->verticalHeader()->setDefaultSectionSize(30);
ui->tableWidget->verticalHeader()->setHidden(true);
ui->tableWidget->scrollToBottom();
首先获取当前位置行,点击插入后执行执行插入行操作:
int currentRow = ui->tableWidget->currentRow();
if(currentRow == -1)
{
qDebug()<<"未选择插入位置!";
return ;
}
ui->tableWidget->insertRow(currentRow);
ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0, new QTableWidgetItem("data")));
ui->tableWidget->setItem(i, 0, new QTableWidgetItem(info.indexes));
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->item(0,0)->setFont(QFont( "Times", 10, QFont::Black ));
ui->tableWidget->item(0,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
connect(ui->tableWidget, SIGNAL(cellDoubleClicked(int , int )),
this, SLOT(on_clickTabWidget(int , int )));
ui->tableWidget->item(i,4)->setBackgroundColor(RedColor);
如QLabel,给label插入背景图片实现如下效果:
QLabel *status = new QLabel();
status->setPixmap(QPixmap(":/new/prefix1/white.png"));
ui->tableWidget->setCellWidget(i,4,status);
ui->tableWidget->selectRow(0);
ui->tableWidget->selectAll();
ui->tableWidget->clearSelection();
ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
//设置纵向滚动条样式
ui->tableWidget->verticalScrollBar()->setStyleSheet(
"QScrollBar{background-color:rgb(218,222,223); width:10px;}"
"QScrollBar::handle{background-color:rgb(180, 180, 180); border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background-color:rgb(139, 139, 139);}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
//设置横向滚动条样式
ui->tableWidget->horizontalScrollBar()->setStyleSheet(
"QScrollBar{background-color:rgb(218,222,223); height:10px;}"
"QScrollBar::handle{background-color:rgb(180, 180, 180); border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background-color:rgb(139, 139, 139);}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
只设置控件的背景为透明,子控件不设置
QPalette pBuff = ui->tableWidget->palette();
pBuff.setBrush(QPalette::Base,QBrush(QColor(255,255,255,0)));
ui->tableWidget->setPalette(pBuff);
ui->tableWidget->setShowGrid(false);
目前只能修改控件样式实现,且图片显示位置不包含表头,代码如下:
ui->tableWidget->setStyleSheet("border-image: url(:/Resources/Images/backgruod.png)");
去掉背景图片,使用下面任意一行代码就行:
ui->tableWidget->setStyleSheet("");
ui->tableWidget->styleSheet().clear();
更多内容后续持续更新。