1.初始化
QTableWidget *tabwgt = new QTableWidget();
tabwgt->verticalHeader()->setDefaultSectionSize(20); //列宽度
tabwgt->horizontalHeader()->setFixedHeight(25);
tabwgt->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
tabwgt->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
tabwgt->verticalHeader()->setVisible(false); //隐藏列头
tabwgt->setAlternatingRowColors( true ); //切换颜色
tabwgt->setStyleSheet( "QTableView{alternate-background-color: rgb(141, 163, 215);}" ); 切换颜色
tabwgt->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
tabwgt->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:rgb(141, 163, 215);color: black;border:none;}");
tabwgt->setColumnCount(4);
QStringList header;
header << tr("监控口") << tr("连接端口") << tr("通信状态") << tr("执行次数");
tabwgt->setHorizontalHeaderLabels(header);
2.数据
this->tabwgt->setRowCount(rowCount);
//数据
for(int i=0; i < tabwgt->rowCount(); i++){
tabwgt->setItem(i,0,new QTableWidgetItem("控制口"));
tabwgt->setItem(i,1,new QTableWidgetItem(consoleList[i]->cur_port));
tabwgt->setItem(i,2,new QTableWidgetItem("已连接"));
tabwgt->setItem(i,3,new QTableWidgetItem(QString::number(Global_ExcuteCount)));
tabwgt->item(i,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tabwgt->item(i,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tabwgt->item(i,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tabwgt->item(i,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
3.刷新数据
QTimer *timer_tabData = new QTimer(this);
connect(timer_tabData, SIGNAL(timeout()), this, SLOT(timer_tabData_flush()));
timer_tabData->start(1000);
//数据刷新
void PortStateForm::timer_tabData_flush()
{
for(int i=0; i < tabwgt->rowCount(); i++){
tabwgt->item(i, 1)->setText(StateList[i]->console);
tabwgt->item(i, 2)->setText(StateList[i]->port);
tabwgt->item(i, 3)->setText(StateList[i]->isConn);
tabwgt->item(i, 4)->setText(StateList[i]->excuNum);
}
}
4.清除
tabwgt->clearContents();
QTableWidget *tabwgt = new QTableWidget();
tabwgt->verticalHeader()->setDefaultSectionSize(20); //列宽度
tabwgt->horizontalHeader()->setFixedHeight(25);
tabwgt->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
tabwgt->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
tabwgt->verticalHeader()->setVisible(false); //隐藏列头
tabwgt->setAlternatingRowColors( true ); //切换颜色
tabwgt->setStyleSheet( "QTableView{alternate-background-color: rgb(141, 163, 215);}" ); 切换颜色
tabwgt->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
tabwgt->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:rgb(141, 163, 215);color: black;border:none;}");
tabwgt->setColumnCount(4);
QStringList header;
header << tr("监控口") << tr("连接端口") << tr("通信状态") << tr("执行次数");
tabwgt->setHorizontalHeaderLabels(header);
2.数据
this->tabwgt->setRowCount(rowCount);
//数据
for(int i=0; i < tabwgt->rowCount(); i++){
tabwgt->setItem(i,0,new QTableWidgetItem("控制口"));
tabwgt->setItem(i,1,new QTableWidgetItem(consoleList[i]->cur_port));
tabwgt->setItem(i,2,new QTableWidgetItem("已连接"));
tabwgt->setItem(i,3,new QTableWidgetItem(QString::number(Global_ExcuteCount)));
tabwgt->item(i,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tabwgt->item(i,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tabwgt->item(i,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tabwgt->item(i,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
3.刷新数据
QTimer *timer_tabData = new QTimer(this);
connect(timer_tabData, SIGNAL(timeout()), this, SLOT(timer_tabData_flush()));
timer_tabData->start(1000);
//数据刷新
void PortStateForm::timer_tabData_flush()
{
for(int i=0; i < tabwgt->rowCount(); i++){
tabwgt->item(i, 1)->setText(StateList[i]->console);
tabwgt->item(i, 2)->setText(StateList[i]->port);
tabwgt->item(i, 3)->setText(StateList[i]->isConn);
tabwgt->item(i, 4)->setText(StateList[i]->excuNum);
}
}
4.清除
tabwgt->clearContents();