如何在QTableWidget上显示数据并使用标题从中读取数据?

时间:2022-10-19 20:51:14

How can I show data on QTableWidget and read data from it with header?

如何在QTableWidget上显示数据并使用标题从中读取数据?

2 个解决方案

#1


22  

1). Create table with this example code:

1)。使用以下示例代码创建表:

filesTable = new QTableWidget(0, 2);
QStringList labels;
labels << tr("File Name") << tr("Size");
filesTable->setHorizontalHeaderLabels(labels);
filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
filesTable->verticalHeader()->hide();
filesTable->setShowGrid(false);

2). Add row:

2)。添加行:

int row = filesTable->rowCount();
filesTable->insertRow(row);
filesTable->setItem(row, 0, fileNameItem);
filesTable->setItem(row, 1, sizeItem);

Enjoy.

请享用。

#2


6  

To create columns:

要创建列:

ui->tableWidget->setColumnCount('the number of columns');

before you can insert rows you need to set the total rows:

在您可以插入行之前,您需要设置总行数:

ui->tableWidget->setRowCount('number of rows');

now loop through rows and columns and set the data in each

现在循环遍历行和列并在每个中设置数据

for (int ridx = 0 ; ridx < 'number of rows' ; ridx++ )
{
    for (int cidx = 0 ; cidx < 'number of columns' ; cidx++)
    {
      QTableWidgetItem* item = new QTableWidgetItem();
      item->setText('text or number to display');
      ui->tableWidget->setItem(ridx,cidx,item);
    }
}

to set the header

设置标题

ui->tableWidget->setHorizontalHeaderLabels('QStringList containing the names');

hope it helps

希望能帮助到你

#1


22  

1). Create table with this example code:

1)。使用以下示例代码创建表:

filesTable = new QTableWidget(0, 2);
QStringList labels;
labels << tr("File Name") << tr("Size");
filesTable->setHorizontalHeaderLabels(labels);
filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
filesTable->verticalHeader()->hide();
filesTable->setShowGrid(false);

2). Add row:

2)。添加行:

int row = filesTable->rowCount();
filesTable->insertRow(row);
filesTable->setItem(row, 0, fileNameItem);
filesTable->setItem(row, 1, sizeItem);

Enjoy.

请享用。

#2


6  

To create columns:

要创建列:

ui->tableWidget->setColumnCount('the number of columns');

before you can insert rows you need to set the total rows:

在您可以插入行之前,您需要设置总行数:

ui->tableWidget->setRowCount('number of rows');

now loop through rows and columns and set the data in each

现在循环遍历行和列并在每个中设置数据

for (int ridx = 0 ; ridx < 'number of rows' ; ridx++ )
{
    for (int cidx = 0 ; cidx < 'number of columns' ; cidx++)
    {
      QTableWidgetItem* item = new QTableWidgetItem();
      item->setText('text or number to display');
      ui->tableWidget->setItem(ridx,cidx,item);
    }
}

to set the header

设置标题

ui->tableWidget->setHorizontalHeaderLabels('QStringList containing the names');

hope it helps

希望能帮助到你