Image and text in Table View



  • Hi,
    I'd like to show records from a database in tableview and display an image in the first column for each record. Is there a way to do that?
    Thank you for your help.



  • @gabor53
    You may refer this to show record and for image it is shown here.



  • Is it possible to display both the records and the images in the same table?

    Thank you.



  • 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.



  • How can I save an image into the database (and how can I display it)?
    I want to store an image in one of the columns and text data in the others.
    Than you.



  • 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 ();

    1. You did not open the file. You need to use open() method as suggested here.
    2. 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.



  • I added the following to the code:

    qDebug() <<"Byte size: " << inByteArray.size();
    

    It says
    Byte size: 0.
    Why is it empty?

    Thank you.


  • Lifetime Qt Champion

    Hi,

    Two things:

    1. 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.
    2. 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.


  • Thank you all. It finally worked.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.