Add margins to cells of QTableView

问题描述 投票:0回答:1

Qt 6.8.0, here how my

QTableView
looks like after calling
resizeColumnsToContents()
:

before

INSTEAD我仍然想调整到内容大小(我的意思是不指定全局最小宽度),但是在每列内容之后留出一些空间,例如:

after

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:

css

有一种方法可以将列宽度调节到内容物,但增加了一些空间?

qt qtableview qt6
1个回答
0
投票

据否,做到这一点的“正确”方法可能是用

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!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.