qt从mysql数据库中读取和显示图片

来源:互联网 发布:软件销售流程 编辑:程序博客网 时间:2024/05/21 13:06

显示

QString g_strCurrentDir;
QString strImage = QFileDialog::getOpenFileName(
this,
"请选择照片文件",
g_strCurrentDir,
"图像文件 (*.png *.jpg *.bmp *.gif)");
if (strImage.isNull())
{
return;
}
g_strCurrentDir = QDir(strImage).absolutePath();
ui.labelPic->setPixmap(QPixmap(strImage).scaled(ui.labelPic->size()));

保存图片到数据库中:

QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
ui.labelPic->pixmap()->save(&buffer, "PNG");


QSqlRecord record =model->record(7);
record.setValue("Picture",(QVariant)bytes);
model->setRecord(0,record);
model->submitAll();

从数据库中读取图片:

QSqlQuery q("SELECT Picture FROM User WHERE Id = 6");
if (q.next())
{
if (q.isNull(0) == false)
{
QPixmap photo;
photo.loadFromData(q.value(0).toByteArray(), "PNG");

ui.UserPicLabel->setPixmap(photo);
}
}


原创粉丝点击