Ghilas BELHADJ
Ghilas BELHADJ

Reputation: 14096

Add padding to QTreeView Columns

I've added a left and right padding to the header of the QTreeView using this QSS code:

QHeaderView::section{padding:7px 15px}

But the content of the columns is not aligned anymore with the headers.

unaligned QTreeView columns with headers title

How can I add a padding of 15px ( like in the header ) to the columns' content?

Edit: For some reasons I use delegates to draw the content of the QTreeView, that's why styling the QTreeView::item doesn't work ( like @svlasov suggested ).

painter.translate(15, 0) seems to fix this issue, but a weird effect appears when I select a row: the selection is not continuous.

enter image description here

Upvotes: 4

Views: 6616

Answers (2)

Gary
Gary

Reputation: 1

This is rude and crude, but it does what you're asking for in a real simple way for a 3 column QTreeWidget.

// Okay, I want to make sure my columns are wide enough for the contents, but I also
// Don't want them squished together. So use resizeColumnToContents, to make them
// as small as they can be to show the contents, then take those widths and add some 
// spacing and then set the columns to the new widths.
m_tree->resizeColumnToContents (0);
m_tree->resizeColumnToContents (1);
int w0 = m_tree->columnWidth (0) + 20;
int w1 = m_tree->columnWidth (1) + 20;
m_tree->setColumnWidth (0, w0);
m_tree->setColumnWidth (1, w1);

Upvotes: 0

svlasov
svlasov

Reputation: 10456

Something like this:

QTreeView::item {  border: 0px;  padding: 0 15px; }

Upvotes: 3

Related Questions