Columns name replaced with numerals in QTableWidget



  • I have a QTableWidget with fixed number of columns (5 to be precise). I specify the columns by QTableWidget::setColumnCount and then use QTableWidget::setHorizontalHeaderLabels to assign the header labels. But they are instead shown as numerals (1,2,3,4,5 instead of names).
    So, I tried writing a loop, insert columns (QTableWidget::insertColumn(int)), create item and set name and set the header item for column (QTableWidget::setHorizontalHeaderItem(int, QTreeWidgetItem*)), I still get numerals. Any idea as to how should I display header labels?

    Info: Windows 10, Qt 5.9, MSVC 15.



  • Could you try:

    for(int i=0;i<tableWidget->columnCount();++i)
    tableWidget->model()->setHeaderData(i,Qt::Horizontal,QStringLiteral("Column %1").arg(i+1));
    


  • I tried doing that, debugging the issue, it seems like the headers items are all NULL, i.e. in QTableModel::setHeaderData, the horizontalHeaderItems.size() is 5, but all of them are nullptr.



  • I have tried doing the following thing:

    for (auto col = 0; col < headers.size(); ++col) {
            auto hitem = new QTableWidgetItem;
            hitem->setText(headers[col]);
            table->setHorizontalHeaderItem(col, hitem);
            table->model()->setHeaderData(col, Qt::Horizontal, headers[col], Qt::DisplayRole);
    }
    

    When I loop and print by QTableWidgetItem::text, I get the correct labels, but it still displays 1, 2, 3, 4, 5 in GUI.



  • Can't reproduce, this works for me:

    #include <QApplication>
    #include <QTableWidget>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTableWidget wid;
        wid.setRowCount(10);
        wid.setColumnCount(5);
        for(int i=0;i<wid.columnCount();++i){
            wid.setHorizontalHeaderItem(i, new QTableWidgetItem);
            Q_ASSUME(wid.model()->setHeaderData(i,Qt::Horizontal,QStringLiteral("Column %1").arg(i+1)));
        }
        wid.show();
        return a.exec();
    }
    

    Are you setting the column count to 0 anywhere in your code?



  • I just did a column count, I get 5, not 0. So, column count is ok. Even the rows I add are ok, the trouble is with the headers, no matter what I do, I am always seeing numerals only.



    • Did you try my minimal example above?
    • do you, ever, in your code call table->setColumnCount(0) or remove columns?


    1. The minimal example works.
    2. No, I dont. In the sample application, I have other tables, whose headers do show correctly. All tables are initialized by the same routine.

    With VS debugger, I drilled down, it seems the data is indeed there, I can see the horizontal header names, but they dont get displayed.



  • Ok, found the issue, but do not know what causes it. So the table is added to a stacked layout. When I actually navigate there, the QTableModel::horizontalHeaderItems has 5 NULL items. They are not there. The column count is still 5 however.



  • Yeah, the behaviour of Qt models in that sense is quite inconsistent. If you used QStandardItemModel+QTableView, for example, you would not need to worry about this issue. It's always usefull to have methods that are assumed to work wrapped in Q_ASSUME as above. It would assert in debug mode if the setHeaderData failed



  • I cannot (also don't wish to) switch to Model/View for this as I would merely be displaying a bunch of information in table, no interaction as such. Other QTableWidget are working fine in the application. I will try to track down the issue. Also, setHeaderData does not fail either.



  • I know that I am too late but I had the same problem and managed to solve it. I am posting my solution to help other people in the future.
    In my case, adding the QtableWidget to a stacked layout had nothing to do with the problem. In my code I used QTableWidget::clear() this clears the table including the header. To clear the content only and leave the header as is use QTableWidget:: clearContents ()


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.