QTableWidget清空或删除内容及表头样式内容

今天使用到了QTableWidget的清空和删除,特地整理出。如下:

目录

1、清空表格中所有内容

  1. void QTableWidget::clear() [slot] //清空表格中所有内容(包含表头)
  2. Removes all items in the view. This will also remove all selections and headers. 

 2、清空表格中的内容(不包含表头)

  1. void QTableWidget::clearContents() [slot] //清空表格中的内容(不包含表头)。
  2.  Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.

3、删除表格中的某行中的某列内容

  1. void QTableWidget::removeCellWidget(int row, int column) //删除表格中的某行中的某列内容。
  2.  Removes the widget set on the cell indicated by row and column.

4、删除表格中的某列内容

  1. void QTableWidget::removeColumn(int column) [slot] //删除表格中的某列内容。
  2.  Removes the column column and all its items from the table.

5、删除表格中的某行内容

  1. void QTableWidget::removeRow(int row) [slot] //删除表格中的某行内容。
  2.  Removes the row row and all its items from the table

6、删除表格中的某行和某列

  1. QTableWidgetItem * QTableWidget::takeItem(int row, int column) //删除表格中的某行和某列
  2.  Removes the item at row and column from the table without deleting it.

7、删除表格中的水平标题头

  1. QTableWidgetItem * QTableWidget::takeHorizontalHeaderItem(int column) //删除表格中的水平标题头
  2.  Removes the horizontal header item at column from the header without deleting it.

8、删除表格中的垂直标题头

  1. QTableWidgetItem * QTableWidget::takeVerticalHeaderItem(int row) //删除表格中的垂直标题头
  2.  Removes the vertical header item at row from the header without deleting it.

9、设置该单元格为屏蔽状态,不能编辑和选中

ui->tableWidget->item(0,0)->setFlags(Qt::ItemIsEditable);//设置该单元格为屏蔽状态,不能编辑和选中。 

10、取消表头的在选中单元格时的高亮状态

ui->tableWidget->horizontalHeader()->setHighlightSections(false);//取消表头的在选中单元格时的高亮状态。

 11、QTableWidget设置为整行选中

ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);

12、设置表头的样式

ui->tableWidget->horizontalHeader()->setStyleSheet(“image: url(:/img/a.png);image-position:left; “); //设置表头的样式。 

13、删除QTableWidget自带的行号

  1. QHeaderView* headerView = ui->tableWidget->verticalHeader();//m_ItemTable为QTableWidget
  2. headerView->setHidden(true);//隐藏tablewidget自带行号列

14、设置QTableWidget内容居中

  1. item = new QTableWidgetItem;
  2. item->setText(userInfo.userId);
  3. item->setTextAlignment(Qt::AlignCenter);//设置内容对齐方式为垂直和水平居中
  4. ui->tableWidget->setItem(i, 0, item);

15、QTableWidget设置最后一列自动填充表格

ui->tableWidget->horizontalHeader()->setStretchLastSection(true); 

16、QTableWidget隐藏序号列

  1. QHeaderView* headerView = m_ItemTable->verticalHeader();//隐藏序号列
  2. headerView->setHidden(true);

17、QTableWidget设置最后一列自动填充表格

  1. ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //设置最后一列自动填充表格
  2. ui->tableWidget->horizontalHeader()->setResizeContentsPrecision(QHeaderView::Stretch);

18、QTableWidget设置水平表头

  1. header<<“节目编号”<<“节目名称”<<“开始时间”<<“结束时间”<<“创建者”<<“创建时间”<<“审核”<<“备注”;
  2. ui->tableWidget->setHorizontalHeaderLabels(header); //设置水平表头
  3. ui->tableWidget->setColumnWidth(1, 200);//设置第一列宽度

19、在使用QTableWidget显示图片、文字等信息

在使用QTableWidget显示图片、文字等信息的时候,有时会遇到这样一种情况:图片或者文字没有填满一整行或一整列。余下的几项是空白的单元格,白占着位置,最关键的是这些Item还可以被选中,在选中项被设成高亮显示的时候就十分的难看,显得很不和谐。如何设置这些空白的单元格,使其看起来就像背景的白板一样呢?

    下面提供解决办法:

1)、首先空白的单元格要填入一个QTableWidgetItem,这是必要的,因为我们要针对某一个Item操作,而不是QTableWidget。

2)、通过设置flag,设置Item的属性:使其不可用或者不可选中、不可编辑。

Qt提供了一个函数(setFlags(Qt::ItemFlags))和一些枚举值来设置这些属性。

使用方法如下:

  1. QTableWidgetItem *item = new QTableWidgetItem();
  2. tabWidget->setItem(2,0,item);
  3. item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

上面用到的两个枚举中:~Qt::ItemIsEnabled可以保证单击该Item时不会被选中,但是在启用Ctrl + A时,全选操作会导致Item被选中。~Qt::ItemIsSelectable的使用可以保证全选状态下也不会被选中,但是在单独使用时出现了虚线框,没有真正实现“不存在”的效果。所以必须两个同用。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注