PDA

View Full Version : [Qtreeview] How to let the child content always top display



lovelyld
29th March 2019, 12:11
See from the left part picture, the child content is hided, I need adjust the column width to show out the child content.

So I want to know how to show the whole content of child without adjust the column width.

13068

anda_skoa
29th March 2019, 16:59
Have you tried QHeaderView::setSectionResizeMode()?

Cheers,
_

lovelyld
30th March 2019, 08:18
See from the left part picture, the child content is hided, I need adjust the column width to show out the child content.

So I want to know how to show the whole content of child without adjust the column width.

13068

Hi, thanks for your reply. I used the setSectionResizeMode() in the follow 13069way. The first column width will adjust automatically. But what I want is, the column width of the parent item not change, just change the child item column width. Like this picture show. The child content length is longer than the parent column width. How can I achieve this?

t->header()->setSectionResizeMode(QHeaderView::ResizeToContents );

anda_skoa
30th March 2019, 08:51
Ah, you don't want to change the width of a column, you want certain cells to span more than a column.

Might be possible with a custom delegate on the first column.
When it is handling a top level entry it would do normal drawing, when handling a sub item it would ignore the cell rectangle's width.

Cheers,
_

lovelyld
1st April 2019, 15:57
Ah, you don't want to change the width of a column, you want certain cells to span more than a column.

Might be possible with a custom delegate on the first column.
When it is handling a top level entry it would do normal drawing, when handling a sub item it would ignore the cell rectangle's width.

Cheers,
_

Hi, As your suggestion, I implement a delegate in follow way.



void ColumnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.parent().isValid())
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.rect.adjust(0,0,500,0);
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
QApplication::style()->drawItemText ( painter, myOption.rect , myOption.displayAlignment, QApplication::palette(), true,text );
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}

this solve my previous problem, but I meet another problem, when I adjust second column width it will hide the content of child item. how to solve this problem, see the attachment.

13071

lovelyld
2nd April 2019, 03:00
Ah, you don't want to change the width of a column, you want certain cells to span more than a column.

Might be possible with a custom delegate on the first column.
When it is handling a top level entry it would do normal drawing, when handling a sub item it would ignore the cell rectangle's width.

Cheers,
_

solve this problem by using the section resize signals of Qheaderview, in the slot I hide the first line and show it again to trigger the first column repaint()