I am writing a C++ plugin for an existing application where I need to display a window which contains a QTableWidget using Qt.
Here is an excerpt of my working code where I am building the QTableWidget and filling the cells.
tableWidget->setRowCount(0); // clear tablewidget
tableWidget->clearContents();
tableWidget->setColumnCount(5);
tableWidget->setRowCount(2);
QStringList header;
header << "Date" << "Owner" << "Type" << "Folder" << "Path";
tableWidget->verticalHeader()->setDefaultSectionSize(20);
tableWidget->setHorizontalHeaderLabels(header);
tableWidget->setItem(0,0,new QTableWidgetItem(QString("dog")));
tableWidget->setItem(0,1,new QTableWidgetItem(QString("cat")));
tableWidget->setItem(0,2,new QTableWidgetItem(QString("frog")));
tableWidget->setItem(1,0,new QTableWidgetItem(QString("shark")));
tableWidget->item(0,0)->setTextAlignment(Qt::AlignLeft);
tableWidget->item(0,1)->setTextAlignment(Qt::AlignLeft);
tableWidget->item(0,2)->setTextAlignment(Qt::AlignLeft);
tableWidget->resizeColumnsToContents();
It works as expected, except the data inside each successive column is moved further right than the previous column. This can be exaggerated when you resize a column and see the data in columns to the right "slide" even further right.
Here are 2 screenshots of the effect (before and after column resize). You can see how the words "cat" and "frog" have moved more to the right than normal.
What am I doing wrong? I want each cells text to be aligned to the left of each column.