Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I apply a global QListView style to a QListWidget that uses a QAbstractItemDelegate for customized item display?
    primarykey
    data
    text
    <p>I have successfully created a QListWidget that displays 2 lines of text for each item using this code (adapted from <a href="http://www.qtcentre.org/threads/27777-Customize-QListWidgetItem-how-to" rel="nofollow">this example</a>):</p> <p>SessionListDelegate.h</p> <pre><code>#ifndef SESSIONLISTDELEGATE_H_ #define SESSIONLISTDELEGATE_H_ #include &lt;QPainter&gt; #include &lt;QAbstractItemDelegate&gt; class SessionListDelegate : public QAbstractItemDelegate { public: SessionListDelegate(QObject *parent = 0, QStyle *style); virtual ~SessionListDelegate(); void paint (QPainter * painter, const QStyleOptionViewItem &amp; option, const QModelIndex &amp; index) const; QSize sizeHint (const QStyleOptionViewItem &amp; option, const QModelIndex &amp; index) const; private: }; #endif /* SESSIONLISTDELEGATE_H_ */ </code></pre> <p>SessionListDelegate.cpp</p> <pre><code>#include "SessionListDelegate.h" SessionListDelegate::SessionListDelegate(QObject *parent) : QAbstractItemDelegate(parent) { this-&gt;_parent = parent; } void SessionListDelegate::paint(QPainter * painter, const QStyleOptionViewItem &amp; option, const QModelIndex &amp; index) const { QRect r = option.rect; QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine); if(option.state &amp; QStyle::State_Selected) { painter-&gt;fillRect(option.rect, option.palette.color(QPalette::Highlight)); } painter-&gt;setPen(fontPen); QString date = index.data(Qt::DisplayRole).toString(); QString description = index.data(Qt::UserRole).toString(); int imageSpace = 10; r = option.rect.adjusted(imageSpace, 0, -10, -30); painter-&gt;setFont(QFont( "Lucida Grande", 24, QFont::Normal)); painter-&gt;drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, date, &amp;r); r = option.rect.adjusted(imageSpace, 30, -10, 0); painter-&gt;setFont(QFont( "Lucida Grande", 18, QFont::Normal)); painter-&gt;drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft, description, &amp;r); } QSize SessionListDelegate::sizeHint(const QStyleOptionViewItem &amp; option, const QModelIndex &amp; index) const { return QSize(200, 60); // very dumb value } SessionListDelegate::~SessionListDelegate() { // TODO Auto-generated destructor stub } </code></pre> <p>Calling code in mainapp.cpp:</p> <pre><code>ui.myList-&gt;setItemDelegate(new SessionListDelegate(ui.myList)); </code></pre> <p>Now, in the main QWidget form of my application UI, I have defined a style sheet which contains a style for all QListViews for the form:</p> <pre><code>QListView::item:selected { color: black; background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(255, 255, 80, 255), stop:1 rgba(255, 255, 255, 255)); } </code></pre> <p>I would like to apply this style to the customized ListWidget, but I can't think of a way to make that happen. It seems like it should be a pretty common thing to do, but I can't find any examples anywhere.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload