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. Displaying an image from an SQLite database in Tableview
Forum Update on Monday, May 27th 2025

Displaying an image from an SQLite database in Tableview

Scheduled Pinned Locked Moved Solved General and Desktop
sqlitetableviewimage displaydatabase
3 Posts 2 Posters 2.5k Views
  • 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 Offline
    G Offline
    gabor53
    wrote on 24 Dec 2015, 05:06 last edited by
    #1

    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.

    B 1 Reply Last reply 24 Dec 2015, 11:21
    0
    • G gabor53
      24 Dec 2015, 05:06

      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.

      B Offline
      B Offline
      Bagavathi
      wrote on 24 Dec 2015, 11:21 last edited by
      #2

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

      How do I display a picture on a QTableView cell?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gabor53
        wrote on 25 Dec 2015, 05:12 last edited by
        #3

        Thank you.

        1 Reply Last reply
        0

        1/3

        24 Dec 2015, 05:06

        • Login

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