Image and text in Table View
-
Can you elaborate more on your qn ? What do you mean by records and image in same table ? In one column you can show image and other columns you can show values you want.
-
I try to use the following code to write into the database:
#include <QCoreApplication> #include <QSql> #include <QSqlDatabase> #include <QDebug> #include <QFile> #include <QByteArray> #include <QSqlQuery> #include <QSqlError> #include <QBuffer> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QSqlDatabase db; db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("C:/Programming/Qtsamples/ImageandDB/db.db"); db.open (); QSqlQuery query; if(!db.open ()) { qDebug() << "The database is not open!"; } else { qDebug() << "The database is open!"; } QByteArray inByteArray; QBuffer inBuffer(&inByteArray); inBuffer.open (QIODevice::WriteOnly); QFile file("1.jpg"); inByteArray = file.readAll (); query.prepare ("INSERT INTO Images (ID,Pic) VALUES ('1.jpg',:imageData)"); query.bindValue (":imageData", inByteArray); if(!query.exec()) { qDebug() <<"Error inserting image into the table!\n" << query.lastError (); } return a.exec(); }
The image file is where the main.cpp is.
I keep getting the following error message:
QIODevice::read(QFile, "1.jpg"): device not open.
Please help me to find out what's wrong with it.Thank you.
-
QFile file("1.jpg");
file.open(QIODevice::Readonly)
inByteArray = file.readAll ();- You did not open the file. You need to use open() method as suggested here.
- Also ensure that 1.jpg is in the path.
-
I implemented what you recommended. It writes the first column into the db but not the content of the inByteArray.
The new code:#include <QCoreApplication> #include <QSql> #include <QSqlDatabase> #include <QDebug> #include <QFile> #include <QByteArray> #include <QSqlQuery> #include <QSqlError> #include <QBuffer> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QSqlDatabase db; db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("C:/Programming/Qtsamples/ImageandDB/db.db"); db.open (); QSqlQuery query; if(!db.open ()) { qDebug() << "The database is not open!"; } else { qDebug() << "The database is open!"; } int ID = 1; QByteArray inByteArray; QBuffer inBuffer(&inByteArray); inBuffer.open (QIODevice::WriteOnly); QFile file("C:/Programming/Qtsamples/ImageandDB/1.jpg"); file.open (QIODevice::WriteOnly); inByteArray = file.readAll (); query.prepare ("INSERT INTO Images (ID,Pic) VALUES (:ID,:Pic)"); //query.bindValue (":imageData", inByteArray); query.bindValue (":ID",ID); query.bindValue (":Pic",inByteArray); if(!query.exec()) { qDebug() <<"Error inserting image into the table!\n" << query.lastError (); } return a.exec(); }
The message I get on the command prompt screen:
The database is open!
QIODevice::read(QFile,"C:\Programming\Qtsamples\ImageandDB\1.jpg"): WriteOnly device.Any idea what's wrong? Thank you for your help.
-
Hi,
Two things:
- Your path is wrong, you're not escaping the backslash properly.
Solution: Use forward slashes in your paths. Qt will handle for you whether it needs to be changed for Windows. - You're opening your file as WriteOnly and don't check the return value of it.
Solution: Fix the open mode and add error handling after that call.
- Your path is wrong, you're not escaping the backslash properly.