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 3.0k 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.
  • Swati777999S Swati777999

    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 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---");

    Swati777999S 1 Reply Last reply
    3
    • Christian EhrlicherC Christian Ehrlicher

      @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

      Swati777999S Offline
      Swati777999S Offline
      Swati777999
      wrote on 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

        @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---");

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on 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
        • AxelViennaA Offline
          AxelViennaA Offline
          AxelVienna
          wrote on 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.

          Swati777999S 1 Reply Last reply
          2
          • AxelViennaA AxelVienna

            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.

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on 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
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              • AxelViennaA Offline
                AxelViennaA Offline
                AxelVienna
                wrote on 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.

                Swati777999S 1 Reply Last reply
                2
                • AxelViennaA AxelVienna

                  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:
                  
                  Swati777999S Offline
                  Swati777999S Offline
                  Swati777999
                  wrote on 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
                  • AxelViennaA Offline
                    AxelViennaA Offline
                    AxelVienna
                    wrote on 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.

                    Swati777999S 1 Reply Last reply
                    0
                    • AxelViennaA AxelVienna

                      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.

                      Swati777999S Offline
                      Swati777999S Offline
                      Swati777999
                      wrote on 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

                      • Login

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