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 image in QSqlTableModel
Forum Update on Monday, May 27th 2025

Displaying image in QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.4k 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 last edited by
    #1

    Hi,
    I have a QSqlTableModel and I want to use it to display an image from a databasecin a TableView. I have the following code so far:

    #include "fixdb.h"
    #include "ui_fixdb.h"
    #include <QDebug>
    
    FixDb::FixDb(QWidget* parent) :
      QDialog(parent),
      ui(new Ui::FixDb) {
      ui->setupUi(this);
    
      correctFriend ();
    }
    
    FixDb::~FixDb() {
      delete ui;
    }
    
    void FixDb::correctFriend() {
      QSqlQuery query_fix;
    
    
    
      int recNum = 0;
      query_fix.prepare ("SELECT COUNT(*) FROM Items");
      query_fix.exec ();
    
      if(query_fix.next ()) {
        recNum = query_fix.value (0).toInt ();
      };
    
      qDebug() << "Record count in fixdb: " << recNum;
    
    
    
      QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE");
      db.setDatabaseName ("C:/Programming/Projects/Folkfriends_bzr/trunk/db.db");
      if(!db.open ())
        QMessageBox::information (this, "Error", "Couldn't open database.");
    
    
      QSqlTableModel* fixModel = new QSqlTableModel(this);
      fixModel->setTable ("Items");
      fixModel->setEditStrategy (QSqlTableModel::OnFieldChange);
      fixModel->setSort (1, Qt::DescendingOrder);
    
      fixModel->select ();
    
      ui->tableView_Fix->setModel (fixModel);
    
      QSqlQuery fixQueryDisp;
      fixQueryDisp.prepare("SELECT * FROM Items");
      fixQueryDisp.exec ();
    
      for(int row1 = 0; row1 < recNum; row1++) {
        ui->tableView_Fix->setRowHeight (row1, 100);
    
        while (fixQueryDisp.next ()) {
          fixPixmap.loadFromData (fixQueryDisp.value (2).toByteArray ());
          fixPixmap = fixPixmap.scaled (100, 100, Qt::KeepAspectRatio);
          fixModel->setData (fixModel->index (row1, 3), fixPixmap, Qt::DecorationRole);
        }
      };
    
      ui->tableView_Fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold; text-decoration: underline;}");
      ui->tableView_Fix->verticalHeader ()->setVisible (false);
      ui->tableView_Fix->setAlternatingRowColors (true);
      ui->tableView_Fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial; ");
    
      fixModel->setHeaderData (0, Qt::Horizontal, QObject::tr ("ID"));
      fixModel->setHeaderData (1, Qt::Horizontal, QObject::tr ("Name"));
      fixModel->setHeaderData (3, Qt::Horizontal, QObject::tr ("What"));
      fixModel->setHeaderData (2, Qt::Horizontal, QObject::tr ("Image"));
      fixModel->setHeaderData (4, Qt::Horizontal, QObject::tr ("Material"));
      fixModel->setHeaderData (5, Qt::Horizontal, QObject::tr ("Color"));
      fixModel->setHeaderData (6, Qt::Horizontal, QObject::tr ("Description"));
      fixModel->setHeaderData (7, Qt::Horizontal, QObject::tr ("Adoption Date"));
      fixModel->setHeaderData (8, Qt::Horizontal, QObject::tr ("Signed by"));
      fixModel->setHeaderData (9, Qt::Horizontal, QObject::tr ("History"));
      fixModel->setHeaderData (10, Qt::Horizontal, QObject::tr ("Age"));
      fixModel->setHeaderData (11, Qt::Horizontal, QObject::tr ("Notes"));
    
    
      ui->tableView_Fix->setColumnWidth (0, 60);
      ui->tableView_Fix->setColumnWidth (1, 120);
      ui->tableView_Fix->setColumnWidth (2, 150);
      ui->tableView_Fix->setColumnWidth (3, 100);
      ui->tableView_Fix->setColumnWidth (4, 130);
      ui->tableView_Fix->setColumnWidth (5, 120);
      ui->tableView_Fix->setColumnWidth (6, 400);
      ui->tableView_Fix->setColumnWidth (7, 150);
      ui->tableView_Fix->setColumnWidth (8, 200);
      ui->tableView_Fix->setColumnWidth (9, 400);
      ui->tableView_Fix->setColumnWidth (10, 130);
      ui->tableView_Fix->setColumnWidth (11, 300);
    
      ui->tableView_Fix->setWordWrap (true);
    
    }//end of correctFriend
    
    

    There is no error message, but the images are not displayed. The image column is #3. Please help me find what I missed.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Don't set the data, QSqlTableModel is read only for any role other than Qt::EditRole, subclass QStyledItemDelegate, reimplement paint() to take the QByteArray and paint the QPixmap, then just set the delegate to the last column

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      G 2 Replies Last reply
      1
      • VRoninV VRonin

        Don't set the data, QSqlTableModel is read only for any role other than Qt::EditRole, subclass QStyledItemDelegate, reimplement paint() to take the QByteArray and paint the QPixmap, then just set the delegate to the last column

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @VRonin
        Would you please show an example? Thank you.

        1 Reply Last reply
        0
        • VRoninV VRonin

          Don't set the data, QSqlTableModel is read only for any role other than Qt::EditRole, subclass QStyledItemDelegate, reimplement paint() to take the QByteArray and paint the QPixmap, then just set the delegate to the last column

          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          @VRonin
          I tried to implement the code at the link, so I created

           bool QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role);
          

          in fixdb.h and

          bool FixDb::QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
          
          }
          

          in fixdb.cpp. I got he following error message:
          C:\Programming\Projects\Folkfriends_bzr\checkout\fixdb.h:39: error: cannot declare member function 'QSqlTableModel::setData' within 'FixDb'
          bool QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role);
          ^
          How can I avoid this error?

          mrjjM 1 Reply Last reply
          0
          • G gabor53

            @VRonin
            I tried to implement the code at the link, so I created

             bool QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role);
            

            in fixdb.h and

            bool FixDb::QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
            
            }
            

            in fixdb.cpp. I got he following error message:
            C:\Programming\Projects\Folkfriends_bzr\checkout\fixdb.h:39: error: cannot declare member function 'QSqlTableModel::setData' within 'FixDb'
            bool QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role);
            ^
            How can I avoid this error?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @gabor53
            Hi
            As it says. You cannot create a member function for one class in another class
            FixDb::QSqlTableModel::setData

            To override setData the class must be a child of QSqlTableModel. so not sure what you try to to do.

            As @VRonin says, you need to use a QStyledItemDelegate to draw the image
            this one is good example
            http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html

            1 Reply Last reply
            2

            • Login

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