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

五迷三道 提交于 2020-01-22 10:56:52

问题


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


回答1:


1). Create table with this example code:

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:

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

Enjoy.




回答2:


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



来源:https://stackoverflow.com/questions/2779274/how-can-i-show-data-on-qtablewidget-and-read-data-from-it-with-header

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!