#ifndef SQLQT_H
#define SQLQT_H
#include <QtGui>
#include "ui_sqlqt.h"
#include <QtSql>
class sqlqt : public QDialog
{
Q_OBJECT
public:
sqlqt(QWidget *parent = 0, Qt::WFlags flags = 0);
~sqlqt();
private:
Ui::sqlqtClass ui;
QSqlDatabase db;
QSqlQuery * query;
public slots:
void next();
void prev();
void browse();
void add();
};
#endif // SQLQT_H
#include "sqlqt.h"
sqlqt::sqlqt(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("iconDB");
db.open();
/*
QSqlQuery query;
query.exec("create table iconTable (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
query.exec("insert into person values(102, 'Christine', 'Holand')");
query.exec("insert into person values(103, 'Lars', 'Gordon')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");*/
QSqlQuery queryCreate;
queryCreate.exec("create table iconTable(icon blob)");
query = new QSqlQuery("select * from iconTable");
connect(ui.buttonbrowser, SIGNAL(clicked()),this,SLOT(browse()));
connect(ui.buttonadd, SIGNAL(clicked()),this,SLOT(add()));
connect(ui.buttonpresions,SIGNAL(clicked()),this,SLOT(prev()));
connect(ui.buttonnext,SIGNAL(clicked()),this,SLOT(next()));
}
sqlqt::~sqlqt()
{
}
void sqlqt::next()
{
qDebug() <<"##################s:" << __FUNCTION__;
query->next();
QPixmap pixmap;
pixmap.loadFromData(query->record().field("icon").value().toByteArray());
ui.labelPixmap->setPixmap(pixmap);
};//查看后一个
void sqlqt::prev()
{
qDebug() <<"##################s:" << __FUNCTION__;
query->previous();
QPixmap pixmap;
pixmap.loadFromData(query->record().field("icon").value().toByteArray());
ui.labelPixmap->setPixmap(pixmap);
};//查看前一个
void sqlqt::browse()
{
qDebug() <<"##################s:" << __FUNCTION__;
QString filename = QFileDialog::getOpenFileName(this, tr("打开图标"), " ",tr("图标文件(*.*)"));
ui.lblBrowse->setPixmap(QIcon(filename).pixmap(100,300));
};//浏览,打开图标
void sqlqt::add()
{
qDebug() <<"##################s:" << __FUNCTION__;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::ReadWrite);
ui.lblBrowse->pixmap()->save(&buffer,"BMP");
//这里默认传递的是NULL指针,导致save返回false。
QVariant variant(ba);
QSqlQuery qryInsert;
qryInsert.prepare("insert into iconTable(icon) values(?)");
qryInsert.bindValue(0, variant);
qryInsert.exec();
};//添加图标