Hi Laslo,
In my delegate, I use a QTextDocument instead and I also override sizeHint:
void MessagesDelegate::paint(
QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem optionV4 = option;
initStyleOption(&optionV4, index);
QStyle *style = optionV4.widget ? optionV4.widget->style() :
QApplication::style();
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
// Draw the item without any text:
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &optionV4, painter,
optionV4.widget);
_doc.setTextWidth(-1);
_doc.setHtml("This is a test message");
QAbstractTextDocumentLayout::PaintContext ctx;
ctx.palette = optionV4.palette;
// TODO: Change ctx.palette according to cell being disabled and/or selected.
painter->save();
painter->translate(textRect.topLeft());
painter->setClipRect(textRect.translated(-textRect.topLeft()));
doc.documentLayout()->draw(painter, ctx);
painter->restore();
}
QSize MessagesDelegate::sizeHint(
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
_doc.setTextWidth(-1);
_doc.setHtml("This is a test message");
return QSize(_doc.idealWidth(), _doc.size().height());
}
Sorry I can't release the full code as it does lots of other things like
icon decoration role, clicking on links in the HTML, etc.
Hope that helps,
Tony
On 2/12/2023 12:06 am, Laszlo Papp wrote:
Hi everyone,
I am not able to render a custom widget, like a QLabel in a QTreeView
cell with a custom delegate. The delegate code is this:
*Header*
#ifndef MESSAGESDELEGATE_H
#define MESSAGESDELEGATE_H
#include <QLabel>
#include <QStyledItemDelegate>
class MessagesDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MessagesDelegate(QObject* parent = nullptr);
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
private:
mutable QLabel _label;
};
#endif
*Source*
#include "MessagesDelegate.h"
#include <QLabel>
MessagesDelegate::MessagesDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
void MessagesDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
_label.setText("This is a test message");
_label.render(painter);
}
and then:
_messageDelegate = new MessagesDelegate(this);
_messageTreeView->setItemDelegateForColumn(MessageColumn,
_messageDelegate);
But I am seeing my QTreeView cell empty instead of seeing the above
"This a test message" in there. Do you know what I may be doing wrong?
I took this idea from here:
https://groups.google.com/g/python_inside_maya/c/FXB_V0llUpY/m/mpJOQkyJCgAJ
Thank you in advance.
Kind regards,
László
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest