I成功(this exampleから適合)は、このコードを使用して、各項目のテキストの2行を表示するQListWidgetを作成した:カスタマイズされたアイテムの表示にQAbstractItemDelegateを使用するQListWidgetにグローバルQListViewスタイルを適用するにはどうすればよいですか?
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
}
mainapp.cppのコードの呼び出し:
ui.myList->setItemDelegate(new SessionListDelegate(ui.myList));
、私はカスタマイズさListWidgetにこのスタイルを適用したい
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));
}
:
は今、自分のアプリケーションのUIのメインQWidgetの形で、私は、フォームのすべてのQListViewsのスタイルが含まれているスタイルシートを定義していますそれを実現させる方法は考えられません。それはかなり一般的なことでなければならないようですが、私はどこにも例を見つけることができません。
おかげで作業する場合、カスタムデリゲートを実装する場合、または として基本クラスをQStyledItemDelegateを使用することをお勧めします。私は実際に実際にこれを示すhttp://qt-articles.blogspot.com/2010/07/how-to-customize-listview-in-qt-using.htmlであなたの応答を読む直前の例を見つけました。 – PIntag
良い例、共有ありがとう... – pnezis