7

How would I go about using the setHorizontalHeaderLabels property of my tableWidget to specify names for my columns as opposed to numbers? I want to keep my rows as numbers, but change my columns to names I have collected into a QList.

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

This is how I am setting the values currently which just involves 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 some guidance on how to properly take the values from my QList and set the columns as those values for my tableWidget. The columns that appear in my tableWidget are 1, 2, 3, 4, 5, 6, 7 which comes from the number of items being passed to it in setColumnCount instead of test1, test2, test3.

THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64
  • 1
    I'm confused... `setHorizontalHeaderLabels` does exactly what you are asking for... it sets the headers to QStrings in a QStringList. – Anthony May 30 '12 at 15:58
  • Well, I am parsing a text file and getting the number of lines for the number of rows. The first line of the text file is commented and that contains the column names that I want to use. However, I only get the number of columns right now and don't know how to pass along the actual column names since it keeps getting overridden by the number of columns. In the code sample above, 'row' and 'column' are integer values. – THE DOCTOR May 30 '12 at 17:28
  • "I only get the number of columns right now and don't know how to pass along the actual column names since it keeps getting overridden by the number of columns" Edit your question to make it clear what you mean by this. – cmannett85 May 30 '12 at 17:30
  • I added more to my description of the problem as well as more of the code I'm working on. – THE DOCTOR May 30 '12 at 17:50

2 Answers2

11

In your example, you set setHorizontalHeaderLabels to an empty list. Be sure to fill it before setting the headers. Also, set the headers after setting the column count.

This is the sort of 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 );
cgmb
  • 4,284
  • 3
  • 33
  • 60
  • Right you are. Silly me...I figured it had something to do with me doing things out of order. Thanks! – THE DOCTOR May 30 '12 at 18:42
  • Thanks for this answer, solved my problem! :-) RE 'Also, set the headers before setting the column count.': Doesn't your code do the opposite ? Did you mean 'set the headers **after** setting the column count.' ? That at least seems to be the only way it works here on Qt 5.4. – ssc Mar 26 '15 at 16:09
8

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

Consider ui->tableWidget_inputPreview->clearContents() to keep the labels.

  • Oh my gosh! That was hard to find. Thank you for pointing it out. I had no idea that clearing the table would also remove the labels. – BSD Mar 30 '18 at 21:44