1
votes

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.

enter image description here

enter image description here

What am I doing wrong? I want each cells text to be aligned to the left of each column.

1

1 Answers

0
votes

The root of the problem appears to be related to Windows Display settings. My laptop text display setting was set to "Medium - 125%".
When I set it back to "Smaller - 100%" then the problem went away.

I would still like my program to work properly if the user should use 125% text, but at least now I know the cause.

Does anyone know how I to prevent the above problem when using 125% text?

enter image description here