首先创建数据库
QSqlQuery query;
query->exec(QObject::tr("create table order_select (icon BLOB)"));
接下来,取图片并存入数据库中
QDir dir(":/images/");
if ( !dir.exists() )
qWarning( "Cannot find the example directory" );
picFile = dir.entryList(QDir::Files/*,QDir::Name*/);
QString temp;
for(int i = 0;i <= picFile.count() - 1 ; ++i)
{
temp = picFile.at(i);
temp = temp.remove(0,temp.lastIndexOf('.') + 1);
if(temp == "png")
{
path << QObject::tr(":/images/%1").arg(picFile.at(i));
}
}
for(int i=0;i<path.count();i++)
{
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::ReadWrite);
QPixmap(path[i]).save(&buffer,"PNG");
//这里默认传递的是NULL指针,导致save返回false。
QVariant variant(ba);
QSqlQuery query1;
query1.exec(QObject::tr("insert into order_select values (?)"));///存入数据库
query1.bindValue(0, variant);
query1.exec();
}