-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookdelegate.cpp
66 lines (62 loc) · 2.21 KB
/
bookdelegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "bookdelegate.h"
#include <QPixmap>
#include <QString>
#include <QStyle>
#include <QColor>
#include <QLabel>
#include <QByteArray>
#include <QBuffer>
BookDelegate::BookDelegate(QObject *parent): QSqlRelationalDelegate(parent)
{
}
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.column() == 1) {
// 1 là cột bìa sách
QByteArray imageByteArray = index.data().toByteArray();
QPixmap image;
image.loadFromData(imageByteArray);
int x = option.rect.x();
int y = option.rect.y();
int w = 30;
int h = option.rect.height();
drawBackground(painter, option, index);
drawFocus(painter, option, option.rect);
painter->drawPixmap(x + option.rect.width() / 2 - w / 2, y, w, h, image);
} else if (index.column() == 7) {
// 7 là cột trạng thái
drawBackground(painter, option, index);
drawFocus(painter, option, option.rect);
if (index.data() == 1)
drawDisplay(painter, option, option.rect, "Khả dụng");
else drawDisplay(painter, option, option.rect, "Không khả dụng");
}
else {
QSqlRelationalDelegate::paint(painter, option, index);
}
}
void BookDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
if (index.column() != 1) {
return QSqlRelationalDelegate::setEditorData(editor, index);
}
QLabel *label = qobject_cast<QLabel*>(editor);
if (label) {
QByteArray imageByteArray = index.data(Qt::EditRole).toByteArray();
QPixmap pixmap;
if (pixmap.loadFromData(imageByteArray)) {
label->setPixmap(pixmap);
}
}
}
void BookDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const {
if (index.column() != 1) {
return QSqlRelationalDelegate::setModelData(editor, model, index);
}
QLabel *label = qobject_cast<QLabel*>(editor);
if (label) {
QBuffer inBuffer;
inBuffer.open(QIODevice::WriteOnly);
if (label->pixmap()->save(&inBuffer, "PNG")) {
model->setData(index, inBuffer.data(), Qt::EditRole);
}
}
}