Qt:显示、保存图片

显示

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);
    }
   }

 

本帖转自:http://hi.baidu.com/skyjsq/blog/item/bb3f7c188e2c9c0c35fa414a.html

你可能感兴趣的:(c,数据库,user,buffer,qt,照片)