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. Naming columns for QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Naming columns for QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.7k Views
  • 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.
  • S Offline
    S Offline
    Swati777999
    wrote on 21 Dec 2021, 05:45 last edited by
    #1

    Hi All ,

    How to give names for columns of QTableWidge types?
    Below is my attempt but it crashed, I know I am not using myTable->horizontalHeaderItem(1)->setText("Name"); correctly.

    {
        QTableWidget *myTable = new QTableWidget(this);
        myTable ->setRowCount(7);
        myTable->setColumnCount(3);
        myTable->verticalHeader()->hide();
        myTable->horizontalHeader()->hide();
    
       myTable->horizontalHeaderItem(1)->setText("S.No");
       myTable->horizontalHeaderItem(1)->setText("Date");
       myTable->horizontalHeaderItem(1)->setText("Name");
       this->show();
    }
    
    

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    A 1 Reply Last reply 21 Dec 2021, 06:51
    0
    • A Offline
      A Offline
      AxelVienna
      wrote on 22 Dec 2021, 09:13 last edited by AxelVienna
      #9

      Which Qt version are you working with? The line I suggested works just fine on Qt 6.2.2.
      Your experiments create a QStringList object on the heap. Why? Sub-sequentially you treat name as an instance while it is a pointer. This just goes nowhere.
      You will find all constructors and examples here.

      Corrected versions of your experiments are:

      Trial 1:
      QStringList name;
      name << "S.No" << "Date" << "Name";
      myTable->setHorizontalHeaderLabels(name); // no more error:
      
      Trial 2
      const QStringList name = {"S. No","Date","Name"};
      myTable->setHorizontalHeaderLabels(name); //no more error:
      

      C++ and Python walk into a bar. C++ reuses the first glass.

      S 1 Reply Last reply 22 Dec 2021, 09:24
      2
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 21 Dec 2021, 05:57 last edited by Christian Ehrlicher
        #2

        @Swati777999 said in Naming columns for QTableWidget:

        myTable->horizontalHeaderItem(1)->setText("S.No");
        myTable->horizontalHeaderItem(1)->setText("Date");
        myTable->horizontalHeaderItem(1)->setText("Name");

        Please read again... how should this set different header columns?

        Also you should check if myTable->horizontalHeaderItem(1) returns a valid pointers

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply 22 Dec 2021, 02:44
        4
        • S Swati777999
          21 Dec 2021, 05:45

          Hi All ,

          How to give names for columns of QTableWidge types?
          Below is my attempt but it crashed, I know I am not using myTable->horizontalHeaderItem(1)->setText("Name"); correctly.

          {
              QTableWidget *myTable = new QTableWidget(this);
              myTable ->setRowCount(7);
              myTable->setColumnCount(3);
              myTable->verticalHeader()->hide();
              myTable->horizontalHeader()->hide();
          
             myTable->horizontalHeaderItem(1)->setText("S.No");
             myTable->horizontalHeaderItem(1)->setText("Date");
             myTable->horizontalHeaderItem(1)->setText("Name");
             this->show();
          }
          
          
          A Offline
          A Offline
          anil_arise
          wrote on 21 Dec 2021, 06:51 last edited by anil_arise
          #3

          @Swati777999 said in Naming columns for QTableWidget:

          QTableWidget default set QHeaderView not any QTableWidgetItem (~ nullptr). So First set horizontalHeaderItem :
          Example :
          QTableWidget *myTable = new QTableWidget(this);
          myTable ->setRowCount(7);
          myTable->setColumnCount(3);
          QTableWidgetItem *hItem0 = new QTableWidgetItem("S. No");
          myTable->setHorizontalHeaderItem(0,hItem0);
          QTableWidgetItem *hItem1 = new QTableWidgetItem("Date");
          myTable->setHorizontalHeaderItem(1,hItem1);

          another way is set Header Label
          Example:
          QTableWidget *myTable = new QTableWidget(this);
          myTable ->setRowCount(7);
          myTable->setColumnCount(3);
          myTable->setHorizontalHeaderLabels({"S. No","Date","Name"});

          Now you can access Header item for modify
          myTable->horizontalHeaderItem(column_Count)->setText("xyz---");

          S 1 Reply Last reply 22 Dec 2021, 04:16
          3
          • C Christian Ehrlicher
            21 Dec 2021, 05:57

            @Swati777999 said in Naming columns for QTableWidget:

            myTable->horizontalHeaderItem(1)->setText("S.No");
            myTable->horizontalHeaderItem(1)->setText("Date");
            myTable->horizontalHeaderItem(1)->setText("Name");

            Please read again... how should this set different header columns?

            Also you should check if myTable->horizontalHeaderItem(1) returns a valid pointers

            S Offline
            S Offline
            Swati777999
            wrote on 22 Dec 2021, 02:44 last edited by
            #4

            @Christian-Ehrlicher said in Naming columns for QTableWidget:

            @Swati777999 said in Naming columns for QTableWidget:

            myTable->horizontalHeaderItem(1)->setText("S.No");
            myTable->horizontalHeaderItem(1)->setText("Date");
            myTable->horizontalHeaderItem(1)->setText("Name");

            Please read again... how should this set different header columns?

            OOPS.... I forgot to change the indices of these names, actually, I intended to write the following codes-

            myTable->horizontalHeaderItem(0)->setText("S.No");
            myTable->horizontalHeaderItem(1)->setText("Date");
            myTable->horizontalHeaderItem(2)->setText("Name");
            

            Also you should check if myTable->horizontalHeaderItem(1) returns a valid pointers

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            1 Reply Last reply
            0
            • A anil_arise
              21 Dec 2021, 06:51

              @Swati777999 said in Naming columns for QTableWidget:

              QTableWidget default set QHeaderView not any QTableWidgetItem (~ nullptr). So First set horizontalHeaderItem :
              Example :
              QTableWidget *myTable = new QTableWidget(this);
              myTable ->setRowCount(7);
              myTable->setColumnCount(3);
              QTableWidgetItem *hItem0 = new QTableWidgetItem("S. No");
              myTable->setHorizontalHeaderItem(0,hItem0);
              QTableWidgetItem *hItem1 = new QTableWidgetItem("Date");
              myTable->setHorizontalHeaderItem(1,hItem1);

              another way is set Header Label
              Example:
              QTableWidget *myTable = new QTableWidget(this);
              myTable ->setRowCount(7);
              myTable->setColumnCount(3);
              myTable->setHorizontalHeaderLabels({"S. No","Date","Name"});

              Now you can access Header item for modify
              myTable->horizontalHeaderItem(column_Count)->setText("xyz---");

              S Offline
              S Offline
              Swati777999
              wrote on 22 Dec 2021, 04:16 last edited by
              #5

              @anil_arise said in Naming columns for QTableWidget:

              @Swati777999 said in Naming columns for QTableWidget:

              QTableWidget default set QHeaderView not any QTableWidgetItem (~ nullptr). So First set horizontalHeaderItem :
              Example :
              QTableWidget *myTable = new QTableWidget(this);
              myTable ->setRowCount(7);
              myTable->setColumnCount(3);
              QTableWidgetItem *hItem0 = new QTableWidgetItem("S. No");
              myTable->setHorizontalHeaderItem(0,hItem0);
              QTableWidgetItem *hItem1 = new QTableWidgetItem("Date");
              myTable->setHorizontalHeaderItem(1,hItem1);

              another way is set Header Label

              QTableWidget *myTable = new QTableWidget(this);
              myTable ->setRowCount(7);
              myTable->setColumnCount(3);
              myTable->setHorizontalHeaderLabels({"S. No","Date","Name"});
              

              Now you can access Header item for modify
              myTable->horizontalHeaderItem(column_Count)->setText("xyz---");

              As suggested by you,
              METHOD-1

              {
              QTableWidget *myTable = new QTableWidget(7,3,this);
              myTable->horizontalHeader()->hide();
              QTableWidgetItem *hItem0 = new QTableWidgetItem("S. No");
              myTable->setHorizontalHeaderItem(0,hItem0);
              QTableWidgetItem *hItem1 = new QTableWidgetItem("Date");
              myTable->setHorizontalHeaderItem(1,hItem1);
              this->show();
              setWindowTitle(tr("TableWidget Example"));
              }
              

              Result : No headings appeared.
              Prog-1-22.12.PNG

              METHOD-2
              I tried the following code

              QTableWidget *myTable = new QTableWidget(7,3,this);
              myTable->horizontalHeader()->hide();
               myTable->setHorizontalHeaderLabels({"S. No","Date","Name"}); // Error : No matching constructor initialisation
               this->show();
              setWindowTitle(tr("TableWidget Example"));
              

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AxelVienna
                wrote on 22 Dec 2021, 08:46 last edited by
                #6

                Method 1 does not set any headers. It populates the first row with what you want to be your header.
                Method 2 goes in the right direction but it hides the headers you want to display. You need to delete line 2 of your code completely if you want your headers to be shown.

                setHorizontalHeaderLables expects a QStringList as an argument. Your argument is ambiguous, that's why you get an error. Replace your line with:

                myTable->setHorizontalHeaderLabels(QStringList({"S. No","Date","Name"}));
                

                ...and your error disappears.

                BTW: this->show() is strange by itself and even stranger as you call setWindowTitle() afterwards.

                C++ and Python walk into a bar. C++ reuses the first glass.

                S 1 Reply Last reply 22 Dec 2021, 09:02
                2
                • A AxelVienna
                  22 Dec 2021, 08:46

                  Method 1 does not set any headers. It populates the first row with what you want to be your header.
                  Method 2 goes in the right direction but it hides the headers you want to display. You need to delete line 2 of your code completely if you want your headers to be shown.

                  setHorizontalHeaderLables expects a QStringList as an argument. Your argument is ambiguous, that's why you get an error. Replace your line with:

                  myTable->setHorizontalHeaderLabels(QStringList({"S. No","Date","Name"}));
                  

                  ...and your error disappears.

                  BTW: this->show() is strange by itself and even stranger as you call setWindowTitle() afterwards.

                  S Offline
                  S Offline
                  Swati777999
                  wrote on 22 Dec 2021, 09:02 last edited by
                  #7

                  @AxelVienna said in Naming columns for QTableWidget:

                  Method 1 does not set any headers. It populates the first row with what you want to be your header.
                  Method 2 goes in the right direction but it hides the headers you want to display. You need to delete line 2 of your code completely if you want your headers to be shown.

                  setHorizontalHeaderLables expects a QStringList as an argument. Your argument is ambiguous, that's why you get an error. Replace your line with:

                  myTable->setHorizontalHeaderLabels(QStringList({"S. No","Date","Name"}));
                  

                  ...and your error disappears.

                  BTW: this->show() is strange by itself and even stranger as you call setWindowTitle() afterwards.

                  Yes, I had already tried with this line

                  myTable->setHorizontalHeaderLabels(QStringList({"S. No","Date","Name"})); which gives me an error as no matching constructor for initialisation of QStringList.

                  I tried experimenting as below:
                  Trial-1

                  QStringList *name = new QStringList();
                  name << "S.No" << "Date" << "Name";
                  myTable->setHorizontalHeaderLabels(name); // Error:
                  

                  Trial-2

                  QStringList *name = new QStringList();
                  name = {"S. No","Date","Name"};
                  myTable->setHorizontalHeaderLabels(name); //Error:
                  
                  

                  Any suggestion?

                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 22 Dec 2021, 09:04 last edited by
                    #8

                    And again basic c++ knowledge missing. setHorizontalHeaderLabels() takes a QStringList, not a pointer to a QStringList. So why are you creating a pointer to a QStringList and try to pass it to setHorizontalHeaderLabels()?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • A Offline
                      A Offline
                      AxelVienna
                      wrote on 22 Dec 2021, 09:13 last edited by AxelVienna
                      #9

                      Which Qt version are you working with? The line I suggested works just fine on Qt 6.2.2.
                      Your experiments create a QStringList object on the heap. Why? Sub-sequentially you treat name as an instance while it is a pointer. This just goes nowhere.
                      You will find all constructors and examples here.

                      Corrected versions of your experiments are:

                      Trial 1:
                      QStringList name;
                      name << "S.No" << "Date" << "Name";
                      myTable->setHorizontalHeaderLabels(name); // no more error:
                      
                      Trial 2
                      const QStringList name = {"S. No","Date","Name"};
                      myTable->setHorizontalHeaderLabels(name); //no more error:
                      

                      C++ and Python walk into a bar. C++ reuses the first glass.

                      S 1 Reply Last reply 22 Dec 2021, 09:24
                      2
                      • A AxelVienna
                        22 Dec 2021, 09:13

                        Which Qt version are you working with? The line I suggested works just fine on Qt 6.2.2.
                        Your experiments create a QStringList object on the heap. Why? Sub-sequentially you treat name as an instance while it is a pointer. This just goes nowhere.
                        You will find all constructors and examples here.

                        Corrected versions of your experiments are:

                        Trial 1:
                        QStringList name;
                        name << "S.No" << "Date" << "Name";
                        myTable->setHorizontalHeaderLabels(name); // no more error:
                        
                        Trial 2
                        const QStringList name = {"S. No","Date","Name"};
                        myTable->setHorizontalHeaderLabels(name); //no more error:
                        
                        S Offline
                        S Offline
                        Swati777999
                        wrote on 22 Dec 2021, 09:24 last edited by
                        #10

                        @AxelVienna said in Naming columns for QTableWidget:
                        Which Qt version are you working with? The line I suggested works just fine on Qt 6.2.2.
                        I am working with Qt 4.8

                        Your experiments create a QStringList object on the heap. Why? Sub-sequentially you treat name as an instance while it is a pointer. This just goes nowhere.
                        You will find all constructors and examples here.

                        Corrected versions of your experiments are:

                        Trial 1:
                        QStringList name;
                        name << "S.No" << "Date" << "Name";
                        myTable->setHorizontalHeaderLabels(name); // no more error:
                        
                        Trial 2
                        QStringList name = {"S. No","Date","Name"};
                        myTable->setHorizontalHeaderLabels(name); //no more error:
                        

                        In Your Version of codes
                        In Trial 1 : No headings appear ; only the table with rows and column
                        In Trial 2: Error in QStringList name = {"S. No","Date","Name"}; --> Error : No matching constructor for initialisation of 'QStringList'

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          AxelVienna
                          wrote on 22 Dec 2021, 09:33 last edited by
                          #11

                          Trial 1 does not display anything because you hide your headers explicitly. Read my previous post and remove the respective line.
                          Trial 2 fails because you use Qt 4.8.

                          C++ and Python walk into a bar. C++ reuses the first glass.

                          S 1 Reply Last reply 22 Dec 2021, 09:43
                          0
                          • A AxelVienna
                            22 Dec 2021, 09:33

                            Trial 1 does not display anything because you hide your headers explicitly. Read my previous post and remove the respective line.
                            Trial 2 fails because you use Qt 4.8.

                            S Offline
                            S Offline
                            Swati777999
                            wrote on 22 Dec 2021, 09:43 last edited by
                            #12

                            @AxelVienna said in Naming columns for QTableWidget:
                            Trial 1 does not display anything because you hide your headers explicitly. Read my previous post and remove the respective line.
                            I thought myTable->horizontalHeader()->hide(); for hiding numerical headers.

                            Trial 2 fails because you use Qt 4.8.
                            Got it.

                            It works like a charm!

                            “ In order to be irreplaceable, one must always be different” – Coco Chanel

                            1 Reply Last reply
                            0

                            1/12

                            21 Dec 2021, 05:45

                            • Login

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