-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageprovider.h
52 lines (45 loc) · 1.52 KB
/
imageprovider.h
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
#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H
#include <QQuickImageProvider>
#include <QtSql/QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
class ImageProvider : public QQuickImageProvider
{
public:
ImageProvider()
: QQuickImageProvider(QQuickImageProvider::Pixmap)
{
}
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery q(db);
q.prepare("SELECT thumbnail FROM photos WHERE id = (?)");
q.addBindValue(id.toInt());
if (!q.exec()) {
qDebug() << "Failed to execute query: " << q.lastError().text() << " query was: " << q.executedQuery();
}
if (!q.next())
return QImage();
QImage img = QImage::fromData(q.value(0).toByteArray());
return img;
}
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery q(db);
q.prepare("SELECT thumbnail FROM photos WHERE id = (?)");
q.addBindValue(id.toInt());
if (!q.exec()) {
qDebug() << "Failed to execute query: " << q.lastError().text() << " query was: " << q.executedQuery();
}
if (!q.next())
return QPixmap();
QImage img = QImage::fromData(q.value(0).toByteArray());
return QPixmap::fromImage(img);
}
};
#endif // IMAGEPROVIDER_H