qt - Add margins to cells of QTableView - Stack Overflow

admin2025-04-18  3

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?

Share Improve this question asked Jan 30 at 16:17 MarkMark 5,22511 gold badges73 silver badges152 bronze badges 3
  • "I really don't want to create my own delegate for each column": you don't need to. Based on your other question, you're already using a delegate, just use that delegate for the whole view, simply change its behavior depending on the column (if you're using initStyleOption()) or the contents (the type, if you're using displayText(), which is also what it does by default). – musicamante Commented Jan 30 at 22:44
  • The other delegate is just for a specific column. Using that approach I will need one for each kind of column, won't I? – Mark Commented Jan 31 at 7:05
  • 1 No, you don't. Just use a custom delegate that in sizeHint() just returns the default hint augmented by your margin, then call setItemDelegate(), 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
Add a comment  | 

1 Answer 1

Reset to default 3

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;
    }

}
转载请注明原文地址:http://anycun.com/QandA/1744906724a89301.html