一、需求
设置QT中的TableWidget样式,初始化表头,行高和颜色,行选中,是否修改,是否自动扩展宽度等。
二、代码
void MainWindow::TimerListInit()
{QStringList InfHeader;int columnNum;columnNum = 7;ui->tableWidgetTabTimer->setColumnCount(columnNum);InfHeader<<tr("序号")<<tr("创建时间")<<tr("修改时间")<<tr("内容")<<tr("定时/周期")<<tr("时间/间隔")<<tr("启用");ui->tableWidgetTabTimer->setHorizontalHeaderLabels(InfHeader);ui->tableWidgetTabTimer->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;}");//表列随着表格变化而自适应变化ui->tableWidgetTabTimer->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//表行随着表格变化而自适应变化//ui->tableWidgetTabNoteList->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);ui->tableWidgetTabTimer->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置为不可修改ui->tableWidgetTabTimer->setSelectionMode(QAbstractItemView::SingleSelection); //设置只能选择一行,不能多行选中ui->tableWidgetTabTimer->setSelectionBehavior(QAbstractItemView::SelectRows);//整行选择//表头属性QTableWidgetItem *columnHeaderItem0 = ui->tableWidgetTabTimer->horizontalHeaderItem(0); //获得水平方向表头的Item对象columnHeaderItem0->setFont(QFont("Helvetica")); //设置字体columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //设置单元格背景颜色columnHeaderItem0->setTextColor(QColor(200,111,30)); //设置文字颜色
}