Last active
January 3, 2018 20:00
-
-
Save niklasf/6124645 to your computer and use it in GitHub Desktop.
A Qt QItemDelegate that shows a bold title above a description text. Could also be used to render more general rich text in QTableView and co.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "title_and_description_delegate.h" | |
void TitleAndDescriptionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const | |
{ | |
m_paintingIndex = index; | |
QItemDelegate::paint(painter, option, index); | |
} | |
void TitleAndDescriptionDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const | |
{ | |
Q_UNUSED(option); | |
Q_UNUSED(text); | |
Q_ASSERT(m_paintingIndex.isValid()); | |
QTextDocument *doc = document(option, m_paintingIndex); | |
doc->setTextWidth(rect.width()); | |
painter->save(); | |
painter->translate(rect.left(), rect.top()); | |
doc->drawContents(painter, QRect(0, 0, rect.width(), rect.height())); | |
painter->restore(); | |
delete doc; | |
} | |
QTextDocument *TitleAndDescriptionDelegate::document(const QStyleOptionViewItem &option, const QModelIndex& index) const | |
{ | |
QFont fnt = qvariant_cast<QFont>(index.data(Qt::FontRole)).resolve(option.font); | |
QTextDocument *doc = new QTextDocument(); | |
doc->setDefaultFont(fnt); | |
QVariant descriptionText = index.data(TitleAndDescriptionDelegate::DescriptionRole); | |
if (descriptionText.isValid()) { | |
doc->setHtml(QString("<html><strong>%1</strong><br>%2</html>") | |
.arg(Qt::escape(index.data(Qt::DisplayRole).toString())) | |
.arg(Qt::escape(descriptionText.toString()))); | |
} else { | |
doc->setHtml(QString("<html><strong>%1</strong></html>") | |
.arg(Qt::escape(index.data(Qt::DisplayRole).toString()))); | |
} | |
return doc; | |
} | |
QSize TitleAndDescriptionDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex& index) const | |
{ | |
QVariant value = index.data(Qt::SizeHintRole); | |
if (value.isValid()) { | |
return qvariant_cast<QSize>(value); | |
} | |
QTextDocument *displayDocument = document(option, index); | |
displayDocument->adjustSize(); | |
QRect decorationRect = rect(option, index, Qt::DecorationRole); | |
QRect displayRect = QRect(0, 0, displayDocument->size().width(), displayDocument->size().height()); | |
QRect checkRect = rect(option, index, Qt::CheckStateRole); | |
doLayout(option, &checkRect, &decorationRect, &displayRect, true); | |
delete displayDocument; | |
return (decorationRect|displayRect|checkRect).size(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TITLE_AND_DESCRIPTION_DELEGATE_H | |
#define TITLE_AND_DESCRIPTION_DELEGATE_H | |
#include <QtCore> | |
#include <QtGui> | |
class TitleAndDescriptionDelegate : public QItemDelegate | |
{ | |
Q_OBJECT | |
public: | |
static const int DescriptionRole = Qt::UserRole + 1; | |
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; | |
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; | |
protected: | |
QTextDocument *document(const QStyleOptionViewItem &option, const QModelIndex& index) const; | |
void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const; | |
private: | |
mutable QModelIndex m_paintingIndex; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment