Нужно чтобы в QTreeView каждый итем отображался примерно так:
Т.е, нужно чтобы за первой иконкой был расположен текст, если текст не вмещается в оставшуюся длину итема, то нужно ставить многоточие, сразу после текста должно влезть второе изображение.
Я сделал так:
void TreeDelegate::paint(QPainter* painter, const QStyleOptionViewItem& a_option, const QModelIndex& index) const
{
QStyleOptionViewItem option = a_option;
QString displayText = index.model()->data(index, Qt::DisplayRole).toString();
QPixmap icon;
QPixmap mark;
...
painter->save();
int itemWidth = option.rect.width();
int itemHeight = option.rect.height();
int itemX = option.rect.x();
int itemY = option.rect.y();
int iconWidth = icon.rect().width();
int iconHeight = icon.rect().height();
int markWidth = mark.rect().width();
int markHeight = mark.rect().height();
//icon
{
int targetX = itemX + 2;
int targetY = itemY + (itemHeight - iconHeight) / 2;
QRect targetRect( targetX, targetY, iconWidth, iconHeight );
drawDecoration( painter, option, targetRect, icon );
}
//text
{
int targetX = (itemX + 2) + iconWidth + (2);
int targetY = option.rect.y();
int width = 100; //Длинна текста
int height = option.rect.height();
QRect targetRect( targetX, targetY, width, height );
drawDisplay( painter, option, targetRect, "fasdfsdfsdfsadfsdfasd");
}
//mark
{
int targetX = (itemX + 2) + iconWidth + (2) + 100 + 2;
qreal targetY = itemY + (itemHeight - markHeight) / 2;
QRect targetRect( targetX, targetY, markWidth, markHeight );
drawDecoration( painter, option, targetRect, mark );
}
drawFocus( painter, option, option.rect );
}
Проблема в том, что у меня размер прямоугольника, в котором располагается текст, - фиксирован. А нужно его как-то рассчитать чтобы использовать всю длину итема.
Второй момент: TreeDelegate наследуется от QItemDelegate, а если будет нужно наследоваться от QStyledItemDelegate. Там функций drawDecoration и drawDisplay нет. Или нужно все вручную делать с помощью QPainter?
Нельзя ли этот элемент в дизайнере нарисовать, а в коде только текст и иконки задавать?
Сборка персонального компьютера от Artline: умный выбор для современных пользователей