Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Image and text in Table View
Forum Updated to NodeBB v4.3 + New Features

Image and text in Table View

Scheduled Pinned Locked Moved Solved General and Desktop
tableviewimages
11 Posts 4 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gabor53

    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.

    RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #2

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

    --Alles ist gut.

    1 Reply Last reply
    1
    • G Offline
      G Offline
      gabor53
      wrote on last edited by
      #3

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

      Thank you.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #4

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        2
        • G Offline
          G Offline
          gabor53
          wrote on last edited by
          #5

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabor53
            wrote on last edited by
            #6

            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.

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #7

              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.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              3
              • G Offline
                G Offline
                gabor53
                wrote on last edited by
                #8

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #9

                  I added the following to the code:

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

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

                  Thank you.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #11

                      Thank you all. It finally worked.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved