Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Columns name replaced with numerals in QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Columns name replaced with numerals in QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 7.5k Views 1 Watching
  • 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 Offline
    A Offline
    ajaxcrypto
    wrote on last edited by
    #1

    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
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        ajaxcrypto
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          ajaxcrypto
          wrote on last edited by
          #4

          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
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            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
            3
            • A Offline
              A Offline
              ajaxcrypto
              wrote on last edited by
              #6

              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
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7
                • 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
                0
                • A Offline
                  A Offline
                  ajaxcrypto
                  wrote on last edited by
                  #8
                  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
                  0
                  • A Offline
                    A Offline
                    ajaxcrypto
                    wrote on last edited by
                    #9

                    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
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      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
                      2
                      • A Offline
                        A Offline
                        ajaxcrypto
                        wrote on last edited by
                        #11

                        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
                        0
                        • C Offline
                          C Offline
                          CNCer
                          wrote on last edited by
                          #12

                          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
                          7

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved