I Want to have some long text in one of my columns of the qtreeview (not the first column). This text has to be shown completely but it does not expand. There is a method called SetFirstColumnSpanned. But it only works for the first column. Any ideas of how to span the other columns?
Asked
Active
Viewed 2,021 times
2 Answers
0
You can subclass QItemDelegate and implement painter() to accomplish this. You must set alternatingRowColors to false for it to work (else you have to subclass QTreeView to provide implementations of how alternating colors should behave); In the example below, I had no data in the rootnodes except column 1 and 5 and I needed column 5 to be spanned through column 5 to 7:
#include <QItemDelegate>
class deleg: public QItemDelegate
{
Q_OBJECT
public:
deleg(QObject *p = 0):QItemDelegate(p)
{
}
void setView(QTreeView *tree){view = tree;}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(!index.parent().isValid()){
if(index.column() == 5) //This the column spanned
{
QString fulname = index.data().toString();
QRect vRect = option.rect;
qint64 lng = vRect.width();
lng += (view->columnWidth(6)+view->columnWidth(7));
vRect.setWidth(lng);
painter->drawText(vRect, Qt::AlignLeft, fulname);
}
// Disable painting on columns that you are not interested to show
else if(index.column() == 6|| index.column() == 7)return;
}
else QItemDelegate::paint(painter,option,index);
}
private:
QTreeView *view;
};
You can then set the delegate to your treeview
deleg*trDeleg = new deleg(this);
trDeleg->setView(ui->treeView);
ui->treeView->setItemDelegate(trDeleg);

Kyef
- 69
- 7
-1
Try to use QHeaderView::setResizeMode ( int logicalIndex, ResizeMode mode )
For example:
tableView->horizontalHeader()->setResizeMode(column_number, QHeaderView::ResizeToContents);

Yuriy
- 22
- 2