Displaying an image from an SQLite database in Tableview
Solved
General and Desktop
-
Hi,
I'm adding an image to a database then I would like to display it in tableview. The following code works,
but when I run it, it doesn't display the image just a few characters. What should I do to see the actual picture?QSqlDatabase db; db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("C:/Programming/Qtsamples/ImageUI/db.db"); db.open (); QSqlQuery query; if(!db.open ()) { qDebug() << "The database is not open!"; } else { qDebug() << "The database is open!"; } QByteArray ByteArray; QFile file("C:/Programming/Qtsamples/ImageUI/1.jpg"); if(file.open(QIODevice::ReadOnly)) { qDebug() << "The File 1.jpg is open!"; ByteArray = file.readAll (); } else { qDebug() << "The file 1.jpg is NOT open!"; } qDebug() << "Original data " <<ByteArray.size() << "bytes."; qDebug() <<"Byte size: " << ByteArray.size(); int ID = 1; query.prepare ("INSERT INTO Items (ID,Pic) VALUES (:ID,:Pic)"); //query.bindValue (":imageData", ByteArray); query.bindValue (":ID",ID); query.bindValue (":Pic",ByteArray); if(!query.exec()) { qDebug() <<"Error inserting image into the table!\n" << query.lastError (); } file.close (); db.close (); //getting image from database and displaying in tableview db.setDatabaseName ("C:/Programming/Qtsamples/ImageUI/db.db"); db.open (); model = new QSqlTableModel(this); model->setTable ("Items"); model->select (); // model-> ui->tableView->setModel (model); db.close (); }
Thank you.
-
@gabor53
Hi, Still your in the same Exercise!!!!Ok. There is no relation between reading/writing from the database and presenting the data. These two are two different stages. Now your are in second stage, so you've to create your own custom Model for display the custom data. Please refer the below discussion.