Qt 6.8.0, here how my
QTableView
looks like after calling resizeColumnsToContents()
:
INSTEAD我仍然想调整到内容大小(我的意思是不指定全局最小宽度),但是在每列内容之后留出一些空间,例如:
I really don't want to create my own delegate for each column just to add some space. I'm reading the documentation of QTableView but I can find a suitable property to change.
I tried to set either the margin or the padding using CSS:
qApp->setStyleSheet("QTableView::item \
{ \
border: 0px; \
padding-right: 10px; \
}");
but it adds the padding "outside" the useful width of the cell:
有一种方法可以将列宽度调节到内容物,但增加了一些空间?
据否,做到这一点的“正确”方法可能是用
QStyle
实现自定义sizeFromContents(QStyle::CT_ItemViewItem, ...)
,这就是默认委托的方式确定其默认size.。
我发现的“快速而肮脏”的方式是重新进一步QTableView::sizeHintForColumn()
(重新成像
QAbstractItemView
的版本),并为默认大小添加一些额外的填充: int sizeHintForColumn(int col) const override {
return QTableView::sizeHintForColumn(col) + 4; // "4" is extra padding in pixels
}
hth!