"Reprint" MySQL deposit picture +qt read in the database read the picture

Source: Internet
Author: User

/* time:2017.01.02--2017.01.04

* AUTHOR:WJ

* Function: Connect the database, read the picture from the database and display (succeeded)

*/

"Reference link"

MySQL deposit image +QT read in the database read the picture-lpdpzc column-Blog channel-csdn.net http://blog.csdn.net/lpdpzc/article/details/41915835

"Main Code"

void Mainwindow::on_showimage_clicked ()

{

Qsqldatabase DB;

Connecting to a database

if (Qsqldatabase::contains ("Qt_sql_default_connection"))

db = Qsqldatabase::d atabase ("qt_sql_default_connection");

Else

{

db = Qsqldatabase::adddatabase ("Qmysql");

Db.sethostname ("localhost");

Db.setdatabasename ("Sfood");

Db.setusername ("root");

Db.setpassword ("123456");

}

if (! Db.open ())

{

Qdebug () << "Fail to opened!";

}

Else

{

Qdebug () << "DataBase opened!";

Create a query

Qsqlquery query;

Query.exec ("SELECT * from Sfood"); Query not successful

if (! query.exec ("SELECT * from Sfood"))

{

Qdebug () << "Fail to Selecte!";

}

Else

{

Qdebug () << "Selected success!";

Qpixmap photo;

Qdebug () << "Show Pictures";

if (Query.first ())

{

Photo.loadfromdata (Query.value (5). Tobytearray (), "JPG"); Read the image from the database as binary data, the image format is JPG, and then display in the Qlabel

}

Ui->label->setpixmap (photo);

Ui->label->setscaledcontents (TRUE);

}

}

}

Results

"Reprint" MySQL deposit picture +qt read in the database read the picture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.