Naming columns for QTableWidget
- 
Hi All ,
How to give names for columns of QTableWidge types?
Below is my attempt but it crashed, I know I am not usingmyTable->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(); } - 
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 treatnameas 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: - 
@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
 - 
Hi All ,
How to give names for columns of QTableWidge types?
Below is my attempt but it crashed, I know I am not usingmyTable->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(); }@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---"); - 
@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
@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
 - 
@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---");@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.
METHOD-2
I tried the following codeQTableWidget *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")); - 
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.setHorizontalHeaderLablesexpects 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 callsetWindowTitle()afterwards. - 
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.setHorizontalHeaderLablesexpects 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 callsetWindowTitle()afterwards.@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.setHorizontalHeaderLablesexpects 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 callsetWindowTitle()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-1QStringList *name = new QStringList(); name << "S.No" << "Date" << "Name"; myTable->setHorizontalHeaderLabels(name); // Error:Trial-2QStringList *name = new QStringList(); name = {"S. No","Date","Name"}; myTable->setHorizontalHeaderLabels(name); //Error:Any suggestion?
 - 
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()?
 - 
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 treatnameas 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: - 
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 treatnameas 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:@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.8Your experiments create a QStringList object on the heap. Why? Sub-sequentially you treat
nameas 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 inQStringList name = {"S. No","Date","Name"};--> Error : No matching constructor for initialisation of 'QStringList' - 
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. - 
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.@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 thoughtmyTable->horizontalHeader()->hide();for hiding numerical headers.Trial 2 fails because you use Qt 4.8.
Got it.It works like a charm!