Qt setHorizontalHeaderLabels for tableWidget

How can I use the setHorizontalHeaderLabels property of my tableWidget to specify names for my columns, not numbers? I want my rows to be numbers, but change my columns to the names I put together in a QList .

Right now, I have values ​​for the row and column given as integers. When I try to use setHorizontalHeaderLabels , it seems that the integer values ​​for the columns override the column names that I'm trying to specify, and I don't know how to fix it.

Here's how I set the values ​​at the moment, which only include integer values ​​for my rows and columns:

QList< QStringList > columnHeaderList; //--- create the horizontal (column) headers QStringList horzHeaders; ui->tableWidget_inputPreview->setHorizontalHeaderLabels( horzHeaders ); horzHeaders << "test1" << "test2" << "test3"; ui->tableWidget_inputPreview->setRowCount( rowList.size() - 1 ); ui->tableWidget_inputPreview->setColumnCount( columnHeaderList[0].size() ); for ( int row = 0; row < rowList.size(); ++row ) { for ( int column = 0; column < rowList[row].size(); ++column ) { ui->tableWidget_inputPreview->setItem(row, column, new QTableWidgetItem(rowList[row][column])); } } 

I need to be guided by how to correctly accept values ​​from my QList and set columns as values ​​for my tableWidget . The columns that appear in my TableWidget are 1, 2, 3, 4, 5, 6, 7, which come from the number of elements passed to it in setColumnCount instead of test1, test2, test3 .

+4
source share
2 answers

In your example, you set setHorizontalHeaderLabels to an empty list. Be sure to fill it out before setting the headers. Also, set the headers after setting the number of columns.

This is the order you want:

 //--- create the horizontal (column) headers QStringList horzHeaders; horzHeaders << "test1" << "test2" << "test3"; ui->tableWidget_inputPreview->setRowCount( rowList.size() - 1 ); ui->tableWidget_inputPreview->setColumnCount( columnHeaderList[0].size() ); ui->tableWidget_inputPreview->setHorizontalHeaderLabels( horzHeaders ); 
+7
source

Also understand that calling ui->tableWidget_inputPreview->clear() will remove the labels.

Consider ui->tableWidget_inputPreview->clearContents() to save labels.

+2
source

Source: https://habr.com/ru/post/1415203/


All Articles