[Interest] Item / Views: QLabel delegate

Laszlo Papp lpapp at kde.org
Fri Dec 1 14:06:18 CET 2023


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ó
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20231201/bdd2bbcf/attachment.htm>


More information about the Interest mailing list