现在的位置: 首页 > 综合 > 正文

Qt:显示、保存图片

2013年10月21日 ⁄ 综合 ⁄ 共 839字 ⁄ 字号 评论关闭

显示

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

【上篇】
【下篇】

抱歉!评论已关闭.