#include <QItemDelegate>
{
Q_OBJECT
public:
explicit ItemDelegate
(QObject *parent
= 0);
signals:
public slots:
};
#include <QItemDelegate>
class ItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ItemDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
signals:
public slots:
};
To copy to clipboard, switch view to plain text mode
#include "itemdelegate.h"
#include <QPainter>
#include <QApplication>
ItemDelegate
::ItemDelegate(QObject *parent
) :{
}
{
if(option.
state & QStyle::State_Selected) painter
->fillRect
(r,
QColor::fromRgb(176,
222,
241,
100));
bool checked = index.data(Qt::CheckStateRole).toBool();
QString text
= index.
data(Qt
::DisplayRole).
toString();
QIcon icon
= QIcon(qvariant_cast<QPixmap>
(index.
data(Qt
::DecorationRole)));
r.setHeight(r.height() - 20);
icon.paint(painter, r, Qt::AlignHCenter | Qt::AlignBottom);
textRect.setTop(r.bottom());
textRect.setHeight(19);
textRect.setWidth(r.width() - 20);
textRect.setX(r.x() + 20);
painter
->setPen
(QPen(Qt
::black));
painter->drawText(textRect.left(), textRect.top(), textRect.width(), textRect.height(),
Qt::AlignBottom | Qt::TextWordWrap, text, &r);
QRect checkRect
= textRect;
checkRect.setX(checkRect.x() - 20);
checkRect.setWidth(20);
checkRect.setY(checkRect.y() + 6);
this->drawCheck(painter, option, checkRect, (checked) ? Qt::Checked : Qt::Unchecked);
r = option.rect;
painter
->setPen
(QPen(Qt
::gray));
painter->drawRect(option.rect);
}
{
if(!rect.isValid())
return;
opt.rect = rect;
if(state == Qt::Unchecked)
opt.
state |
= QStyle::State_Off;
else
opt.
state |
= QStyle::State_On;
style
->drawPrimitive
(QStyle::PE_IndicatorCheckBox,
&opt, painter
);
}
{
Q_UNUSED(option);
Q_UNUSED(index);
}
#include "itemdelegate.h"
#include <QPainter>
#include <QApplication>
ItemDelegate::ItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QRect r = option.rect;
if(option.state & QStyle::State_Selected)
painter->fillRect(r, QColor::fromRgb(176, 222, 241, 100));
bool checked = index.data(Qt::CheckStateRole).toBool();
QString text = index.data(Qt::DisplayRole).toString();
QIcon icon = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
r.setHeight(r.height() - 20);
icon.paint(painter, r, Qt::AlignHCenter | Qt::AlignBottom);
QRect textRect = r;
textRect.setTop(r.bottom());
textRect.setHeight(19);
textRect.setWidth(r.width() - 20);
textRect.setX(r.x() + 20);
painter->setPen(QPen(Qt::black));
painter->drawText(textRect.left(), textRect.top(), textRect.width(), textRect.height(),
Qt::AlignBottom | Qt::TextWordWrap, text, &r);
QRect checkRect = textRect;
checkRect.setX(checkRect.x() - 20);
checkRect.setWidth(20);
checkRect.setY(checkRect.y() + 6);
this->drawCheck(painter, option, checkRect, (checked) ? Qt::Checked : Qt::Unchecked);
r = option.rect;
painter->setPen(QPen(Qt::gray));
painter->drawRect(option.rect);
}
void ItemDelegate::drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
{
if(!rect.isValid())
return;
QStyleOptionViewItem opt(option);
opt.rect = rect;
if(state == Qt::Unchecked)
opt.state |= QStyle::State_Off;
else
opt.state |= QStyle::State_On;
QStyle *style = QApplication::style();
style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, painter);
}
QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
return QSize(130, 145);
}
To copy to clipboard, switch view to plain text mode
for(int i = 0; i < photosList.count(); ++i) {
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Unchecked);
QImageReader reader
(photosDir.
absolutePath() + "/" + photosList.
at(i
));
QSize size
= reader.
size();
reader.setQuality(reader.quality() / 2);
reader.setScaledSize(size);
item->setData(Qt::DisplayRole, photosList.at(i));
item
->setData
(Qt
::DecorationRole,
QPixmap::fromImage(reader.
read()));
ui->previewsList->addItem(item);
qApp->processEvents();
}
for(int i = 0; i < photosList.count(); ++i) {
item = new QListWidgetItem();
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Unchecked);
QImageReader reader(photosDir.absolutePath() + "/" + photosList.at(i));
QSize size = reader.size();
reader.setQuality(reader.quality() / 2);
reader.setScaledSize(size);
item->setData(Qt::DisplayRole, photosList.at(i));
item->setData(Qt::DecorationRole, QPixmap::fromImage(reader.read()));
ui->previewsList->addItem(item);
qApp->processEvents();
}
To copy to clipboard, switch view to plain text mode
Bookmarks