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 67 68 69 70
|
#include <QApplication> #include <QtGui> #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery>
int main(int argc, char *argv[]) { QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "conn1"); db.setDatabaseName("/home/htux/thumbs.sqlite"); if(!db.open()) return 1;
QByteArray baImage;
QString filename = "/home/htux/file2.jpg"; QPixmap pixmapIn(filename);
QBuffer buffer(&baImage); buffer.open(QIODevice::WriteOnly);
QPixmap pixmapSmall; pixmapSmall = pixmapIn.scaled(256,256, Qt::KeepAspectRatio, Qt::SmoothTransformation); pixmapSmall.save(&buffer, "JPG",50); pixmapSmall.save("/home/htux/file2.png", "PNG"); pixmapSmall.save("/home/htux/file2-5.jpg", "JPG", 20); pixmapSmall.save("/home/htux/file2-5.jpg", "JPG", 50); pixmapSmall.save("/home/htux/file2-8.jpg", "JPG", 80);
QSqlQuery query(db);
query.exec("CREATE TABLE thumbs " "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," " filename VCHAR(20) , thumbnail BLOB);");
QString sql = QString("INSERT INTO thumbs VALUES " "( NULL , :filename, :thumbnail );");
query.prepare(sql); query.bindValue(":filename", filename); query.bindValue(":thumbnail", baImage);
query.exec();
query.exec("select * from thumbs;");
if (!query.last()) return 1;
QPixmap pixmapOut; pixmapOut.loadFromData(query.value(2).toByteArray());
db.close(); QLabel label; label.setPixmap(pixmapOut); label.show();
return a.exec(); }
|