1

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.

4

1 回答 1

0

问题的根源似乎与 Windows 显示设置有关。我的笔记本电脑文本显示设置设置为“中 - 125%”。
当我将其设置回“更小 - 100%”时,问题就消失了。

如果用户应该使用 125% 的文本,我仍然希望我的程序能够正常工作,但至少现在我知道原因了。

有谁知道我在使用 125% 文本时如何防止上述问题?

在此处输入图像描述

于 2015-01-16T15:09:25.503 回答