/* 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