Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Columns name replaced with numerals in QTableWidget

    General and Desktop
    3
    12
    5296
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      ajaxcrypto last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • V
        VRonin last edited by

        Could you try:

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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply Reply Quote 0
        • A
          ajaxcrypto last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • A
            ajaxcrypto last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • V
              VRonin last edited by

              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?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply Reply Quote 3
              • A
                ajaxcrypto last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • V
                  VRonin last edited by

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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply Reply Quote 0
                  • A
                    ajaxcrypto last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • A
                      ajaxcrypto last edited by

                      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.

                      1 Reply Last reply Reply Quote 0
                      • V
                        VRonin last edited by

                        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

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply Reply Quote 2
                        • A
                          ajaxcrypto last edited by

                          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.

                          1 Reply Last reply Reply Quote 0
                          • C
                            CNCer last edited by

                            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 ()

                            1 Reply Last reply Reply Quote 7
                            • First post
                              Last post