Qt 6.8.0, here how my QTableView looks like after calling resizeColumnsToContents():
instead I want to still resize to the content (without specifying a global minimum width, I mean) but leaving some space after the content of each column, like this:
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:
Is there a way to adjust the column width to the contents but adding some space?
Qt 6.8.0, here how my QTableView looks like after calling resizeColumnsToContents():
instead I want to still resize to the content (without specifying a global minimum width, I mean) but leaving some space after the content of each column, like this:
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:
Is there a way to adjust the column width to the contents but adding some space?
Arguably, the "proper" way to do this is probably to implement a custom QStyle with sizeFromContents(QStyle::CT_ItemViewItem, ...), which is how the default delegate gets its default size.
A "quick and dirty" way that I found is to use a custom QTableView and reimplement QTableView::sizeHintForColumn()  (which reimplements QAbstractItemView's version) and add some extra padding to the default size:
    int sizeHintForColumn(int col) const override {
        return QTableView::sizeHintForColumn(col) + 4;  // "4" is extra padding in pixels
    }
HTH!
PS: Here's a version using a custom QProxyStyle (not tested)
class AppStyle : public QProxyStyle {
  Q_OBJECT
public:
    QSize AppStyle::sizeFromContents(ContentsType type, const QStyleOption *o, const QSize &size, const QWidget *w) const override
    {
        QSize ret = QProxyStyle::sizeFromContents(type, o, size, w);
        if (type == CT_ItemViewItem) {
            // Add some extra padding to view column.
            if (!ret.isEmpty())
                ret.rwidth() += 4;
        }
        return ret;
    }
}


initStyleOption()) or the contents (the type, if you're usingdisplayText(), which is also what it does by default). – musicamante Commented Jan 30 at 22:44sizeHint()just returns the default hint augmented by your margin, then callsetItemDelegate(), which will use the delegate for all columns (replacing the default one), and eventually change the behavior depending on the column (as explained above). Alternatively, create a further subclass of that delegate (implementing the desired behavior for the specific column) and also set that new delegate for the specific column. In this way you only need 2 delegates: the default one (used for all columns), and the specialized one. – musicamante Commented Jan 31 at 16:57