首先我在ui界面加入了一个Table Widget,这个Table Widget我设置了6列
首先在.H文件中加入头文件:#include <QTableWidgetItem>
一、在初始化时加入下面代码
ui->tableWidget->viewport()->installEventFilter(this); //注册事件过滤器
ui->tableWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);//设置滚动条平滑一点
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);//设置充满表宽度
/* 设置各列的宽度 */
ui->tableWidget->setColumnWidth(0, 45);
ui->tableWidget->setColumnWidth(1, 45);
ui->tableWidget->setColumnWidth(2,150);
ui->tableWidget->setColumnWidth(3,150);
ui->tableWidget->setColumnWidth(4,150);
ui->tableWidget->setColumnWidth(5,180);
ui->tableWidget->verticalHeader()->hide(); //设置垂直头不可见
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
//table_widget->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、 Ctrl+A都可以)
table_widget->horizontalHeader()->resizeSection(0,150);//设置表头第一列的宽度为150
table_widget->horizontalHeader()->setFixedHeight(25);//设置表头的高度
table_widget->setStyleSheet("selection-background-color:lightblue;");//设置选中背景色
table_widget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");//设置表头背景色
//设置水平、垂直滚动条样式
table_widget->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent;height:10px;}"
"QScrollBar::handle{background:lightgray;border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
table_widget->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent;width: 10px;}"
"QScrollBar::handle{background:lightgray;border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
ui->tableWidget->show();
二、多选并获取所选行
this->setSelectionMode(QAbstractItemView::ExtendedSelection); //设置多选(可以Ctral+A全选Ctral+Shift多选)获取所选行号:
bool TableWidget::getSelectedRow(QSet&set_row)
{
QList items =this->selectedItems();
int item_count =items.count();
if(item_count <= 0)
{
return false;
}
for(int i=0; i<item_count ;i++)
{
//获取选中的行
int item_row = this->row(items.at(i));
set_row.insert(item_row);
}
return true;
}
三、操作表单(添加、删除行等)
(1)更新整个表格,插入行
ui->tableWidget->clearContents();//清除表格,在析构中也要调用
ui->tableWidget->setRowCount(SearchList.size());//设置行数,每次更新tablewidget都要调用这两行
for(int i=0;i<SearchList.size();i++)
{
//这一行中的每一列都加入各自的数据
ui->tableWidget->setItem(i,0,new QTableWidgetItem(tr("%1").arg(i+1)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(tr("%1").arg(" ")));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(tr("%1").arg(SearchList.at(i).name)));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(SearchList.at(i).startTime.toString("yyyy-MM-dd hh:mm:ss")));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(SearchList.at(i).endTime.toString("yyyy-MM-dd hh:mm:ss")));
ui->tableWidget->setItem(i,5,new QTableWidgetItem(tr("%1").arg(q_time)));
//设置文本为中间对齐
ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(i,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(i,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(i,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(i,4)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(i,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//设置行高
ui->tableWidget->setRowHeight(i,60);
}
(2)设置指定行,这里将这一行最前面的设置一个*号,然后设置整行为灰色
for(int i=0;i<this->SearchList.size();i++)
{
tabrow_nuamber.append(i);
for(int j =0;j<6;j++)
{
if(j == 1)//设置该行第一列
{
ui->tableWidget->setItem(i,j,new QTableWidgetItem(tr("%1").arg("*")));
ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
ui->tableWidget->item(i,j)->setBackgroundColor(QColor(100,100,100));//设置整行的颜色为灰色
}
}
(3)双击某一行
void record::on_tableWidget_cellDoubleClicked(int row, int column)
{
QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
if(!items.isEmpty())
{
int count = items.count();//获取该行的成员数,列数
int row = ui->tableWidget->row(items.at(0));// 获取该行的行号
for(int i = 0; i < count; i++) //获取该行所有成员的数据,
{
int row = ui->tableWidget->row(items.at(i));
QTableWidgetItem *item = items.at(i);
QString text = item->text(); //获取内容
}
/*获取选中行第二列的数据*/
QTableWidgetItem* item = ui->tableWidget_right->item(row,2);
qDebug() << item->Text();
}
}
单击和上面的双击差不多
同样也可以获取选中的行和列
ui->tableWidget->clearContents();
清除tableWidget,防止内存泄漏
record::~record()
{
ui->tableWidget->clearContents();
delete ui;
}