当前位置: 代码迷 >> 综合 >> QTableWidget使用方法详细介绍
  详细解决方案

QTableWidget使用方法详细介绍

热度:0   发布时间:2024-02-09 06:41:26.0

该类主要是用列表的方式来显示数据。

1.添加控件

ui->tableWidget->setCellWidget(int row,int column,QWidget *widget);

2.设置列尺寸的比例

ui->tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents);//根据内容来定义宽度

ui->tableWidget->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Stretch);//该列根据布局和其它列的根据内容定义的列的宽度自适应宽度

3.设置第一列隐藏

ui->tableWidget->verticalHeader()->hide();

4.获取文本内容

QString text = ui->tableWidget->item(int row,int column)->text();

5.设置不可编辑

ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

6、设置清空

void QTableWidget::clear() [slot]  

Removes all items in the view. This will also remove all selections and headers.

//清空表格中所有内容(包含表头)。

7、清空但不包含表头

void QTableWidget::clearContents() [slot]

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

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

void QTableWidget::removeCellWidget(int row, int column)
Removes the widget set on the cell indicated by row and column.

9、void QTableWidget::removeColumn(int column) [slot] 

//删除表格中的某列内容。
Removes the column column and all its items from the table.

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

void QTableWidget::removeRow(int row) [slot] 。
Removes the row row and all its items from the table

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

QTableWidgetItem * QTableWidget::takeItem(int row, int column)
Removes the item at row and column from the table without deleting it.

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

QTableWidgetItem * QTableWidget::takeHorizontalHeaderItem(int column)

 Removes the horizontal header item at column from the header without deleting it.

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

QTableWidgetItem * QTableWidget::takeVerticalHeaderItem(int row) 

 Removes the vertical header item at row from the header without deleting it.

14、添加多个控件,如何响应相似的操作

for循环,按照1添加控件;

for(int i= - ;i<5;i++)
{     //创建一个QPushButton控件QPushButton *pushButton_ = new QPushButton();//设置按键显示文字pushButton_->setText("点击");//建立信号槽connect(pushButton_, SIGNAL(clicked(bool)), this, SLOT(clickButton()));//将QPushButton控件设置到QTableWidget中ui->tableWidget->setCellWidget(i, 1 ,pushButton_);
}
 
void MyTableWidget::clickButton()
{QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());if(NULL == pushButton_){return;}int x = pushButton_->frameGeometry().x();int y = pushButton_->frameGeometry().y();QModelIndex index = ui->tableWidget->indexAt(QPoint(x, y));int row = index.row();int column = index.column();qDebug() << "选择第几行:" << row << "选择第几列:" << column;//接下来自定义要处理的内容//。。。。。。
}

 

  相关解决方案