Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:32:14+00:00 2026-05-26T16:32:14+00:00

I have successfully created a QListWidget that displays 2 lines of text for each

  • 0

I have successfully created a QListWidget that displays 2 lines of text for each item using this code (adapted from this example):

SessionListDelegate.h

#ifndef SESSIONLISTDELEGATE_H_
#define SESSIONLISTDELEGATE_H_

#include <QPainter>
#include <QAbstractItemDelegate>

class SessionListDelegate : public QAbstractItemDelegate
{
public:
    SessionListDelegate(QObject *parent = 0, QStyle *style);
    virtual ~SessionListDelegate();

    void paint (QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    QSize sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index) const;
private:
};

#endif /* SESSIONLISTDELEGATE_H_ */

SessionListDelegate.cpp

#include "SessionListDelegate.h"

SessionListDelegate::SessionListDelegate(QObject *parent)
: QAbstractItemDelegate(parent)
{
    this->_parent = parent;
}

void SessionListDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    QRect r = option.rect;

    QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine);

    if(option.state & QStyle::State_Selected)
    {
        painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    }
    painter->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->setFont(QFont( "Lucida Grande", 24, QFont::Normal));
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, date, &r);

    r = option.rect.adjusted(imageSpace, 30, -10, 0);
    painter->setFont(QFont( "Lucida Grande", 18, QFont::Normal));
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft, description, &r);

}

QSize SessionListDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
   return QSize(200, 60); // very dumb value
}

SessionListDelegate::~SessionListDelegate()
{
    // TODO Auto-generated destructor stub
}

Calling code in mainapp.cpp:

ui.myList->setItemDelegate(new SessionListDelegate(ui.myList));

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:

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));
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:32:15+00:00Added an answer on May 26, 2026 at 4:32 pm

    I think you should inherit from QStyledItemDelegate instead of QAbstractItemDelegate. From the Qt documentation:

    Since Qt 4.4, there are two delegate classes: QItemDelegate and
    QStyledItemDelegate. However, the default delegate is
    QStyledItemDelegate. These two classes are independent alternatives to
    painting and providing editors for items in views. The difference
    between them is that QStyledItemDelegate uses the current style to
    paint its items. We therefore recommend using QStyledItemDelegate as
    the base class when implementing custom delegates or when working with
    Qt style sheet

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully created video from images using the following code -(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path
I have successfully created an app that reads from a bundled .plist file and
I have successfully created Web Services from MySQL Database using Netbeans tutorial . However,
I have successfully created (and manually edited) an Item Template that will add a
I have successfully created a WS client that works correctly when NOT using authentication.
I have successfully created a textbox that displays / collapses an error message depending
I have successfully created an HTML form that gets a file from a user
I have successfully created video from uiimages using AVAssetWriter. But as soon as the
I am using the jquery-ui-map plugin (http://code.google.com/p/jquery-ui-map/). I have successfully created a map using
I have successfully created a feature in sharepoint that modifies the existing edit dialog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.