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. Reading image from QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Reading image from QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
53 Posts 8 Posters 20.9k Views 4 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 Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm trying to read an image from an SQLite db using QSqlTableModel (and display it in a TableView). In the delegate I have the following code:

    void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const {
    
      Q_UNUSED(editor);
      Q_UNUSED(index);
    
      QPixmap fixPic;
      fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray ());
    
      qDebug() << "Fixmodel pic: " << fixPic;
      qDebug() << "Fixmodel pic size: " << fixPic.size ();
    
    }
    
    

    I keep getting the following message:
    QPixmap::scaled: Pixmap is a null pixmap
    Wgat did I miss? Thank you for your help.

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      Hi, apparently loadFromData fails.
      You have hardcoded fixModel->record(1) - are you sure the data is there? In row number one?
      Rows are numbered in zero-based manner.
      Other problem might be that field "Pic" returns invalid value.

      For more information please re-read.

      Kind Regards,
      Artur

      G 1 Reply Last reply
      1
      • artwawA artwaw

        Hi, apparently loadFromData fails.
        You have hardcoded fixModel->record(1) - are you sure the data is there? In row number one?
        Rows are numbered in zero-based manner.
        Other problem might be that field "Pic" returns invalid value.

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

        Hi @artwaw ,
        The data is there. There is an image in each record in the Pic field. Is it the correct way I'm trying to load the image?
        Thank you.

        artwawA 1 Reply Last reply
        0
        • G gabor53

          Hi @artwaw ,
          The data is there. There is an image in each record in the Pic field. Is it the correct way I'm trying to load the image?
          Thank you.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @gabor53 Actually in the method you posted here there should not be any hardcoded values. I would post rather elaborate answer a bit later in the evening, I am commuting now.

          For more information please re-read.

          Kind Regards,
          Artur

          G 1 Reply Last reply
          0
          • artwawA artwaw

            @gabor53 Actually in the method you posted here there should not be any hardcoded values. I would post rather elaborate answer a bit later in the evening, I am commuting now.

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

            @artwaw
            Thank you. I would appreciate that. Also I assigned the delegate to the view like this:

            
              QSqlTableModel* fixModel = new QSqlTableModel(this);
              fixModel->setTable ("Items");
              fixModel->setEditStrategy (QSqlTableModel::OnFieldChange);
              fixModel->setSort (2, Qt::DescendingOrder);
            
              fixModel->select ();
            
              ui->tableView_Fix->setItemDelegateForColumn(3, new FixViewDelegate (this));
            

            The goal would be to get the delegate draw the image each time.

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

              Hi,

              How did you store the image data in your database ?

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

              G 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                How did you store the image data in your database ?

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

                Hi @SGaist ,
                I store them as a QByteArray:

                QPixmap pix2(fileNameChosen);
                  int w = 0;
                  int h = 0;
                  w = ui->Image_Display_Label->width ();
                  h = ui->Image_Display_Label->height ();
                  ui->Image_Display_Label->setPixmap (pix2.scaled(w, h, Qt::KeepAspectRatio));
                
                  QFile fileReview(fileNameChosen);
                
                  if(fileReview.open (QIODevice::ReadOnly)) {
                    fileByteArray = fileReview.readAll ();
                
                jsulmJ 1 Reply Last reply
                0
                • G gabor53

                  Hi @SGaist ,
                  I store them as a QByteArray:

                  QPixmap pix2(fileNameChosen);
                    int w = 0;
                    int h = 0;
                    w = ui->Image_Display_Label->width ();
                    h = ui->Image_Display_Label->height ();
                    ui->Image_Display_Label->setPixmap (pix2.scaled(w, h, Qt::KeepAspectRatio));
                  
                    QFile fileReview(fileNameChosen);
                  
                    if(fileReview.open (QIODevice::ReadOnly)) {
                      fileByteArray = fileReview.readAll ();
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @gabor53 What @SGaist probably means is: what data type do you use for the pixmap column in the table in the database?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @gabor53 What @SGaist probably means is: what data type do you use for the pixmap column in the table in the database?

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

                    Hi @jsulm ,
                    It's BLOB.

                    1 Reply Last reply
                    0
                    • Vinod KuntojiV Offline
                      Vinod KuntojiV Offline
                      Vinod Kuntoji
                      wrote on last edited by
                      #10

                      @gabor53 ,

                      What is the size of the bytearray?

                      C++, Qt, Qt Quick Developer,
                      PthinkS, Bangalore

                      G 1 Reply Last reply
                      0
                      • Vinod KuntojiV Vinod Kuntoji

                        @gabor53 ,

                        What is the size of the bytearray?

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

                        @Vinod-Kuntoji
                        100 x 100

                        1 Reply Last reply
                        0
                        • Vinod KuntojiV Offline
                          Vinod KuntojiV Offline
                          Vinod Kuntoji
                          wrote on last edited by
                          #12

                          @gabor53 ,
                          The blob you are using, is it a raw bytearray or png, jpeg byterray?

                          C++, Qt, Qt Quick Developer,
                          PthinkS, Bangalore

                          G 1 Reply Last reply
                          0
                          • Vinod KuntojiV Vinod Kuntoji

                            @gabor53 ,
                            The blob you are using, is it a raw bytearray or png, jpeg byterray?

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

                            @Vinod-Kuntoji
                            It is jpeg bytearray.

                            1 Reply Last reply
                            0
                            • Vinod KuntojiV Offline
                              Vinod KuntojiV Offline
                              Vinod Kuntoji
                              wrote on last edited by
                              #14

                              @gabor53 ,

                              Then you should specify "JPG" in loadFromData function.

                              QPixmap fixPic;
                              fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray (),"JPG");

                              C++, Qt, Qt Quick Developer,
                              PthinkS, Bangalore

                              G 1 Reply Last reply
                              0
                              • Vinod KuntojiV Vinod Kuntoji

                                @gabor53 ,

                                Then you should specify "JPG" in loadFromData function.

                                QPixmap fixPic;
                                fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray (),"JPG");

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

                                @Vinod-Kuntoji
                                Thank you. I've noticed an other issue: the program execution enters fixviewdelegate.cpp but never reads the actual function:

                                void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const {
                                
                                  Q_UNUSED(index);
                                  Q_UNUSED(editor);
                                
                                  qDebug() << "Entered fixViewDelegate (2).";
                                
                                  QPixmap fixPic;
                                  fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray (), "JPG");
                                  qDebug() << "Fixmodel pic: " << fixPic;
                                  qDebug() << "Fixmodel pic size: " << fixPic.size ();
                                
                                }
                                
                                

                                What can I do to fix this problem?

                                raven-worxR 1 Reply Last reply
                                0
                                • Vinod KuntojiV Offline
                                  Vinod KuntojiV Offline
                                  Vinod Kuntoji
                                  wrote on last edited by
                                  #16

                                  @gabor53 said in Reading image from QSqlTableModel:

                                  execution

                                  What is that function?

                                  C++, Qt, Qt Quick Developer,
                                  PthinkS, Bangalore

                                  1 Reply Last reply
                                  1
                                  • G gabor53

                                    @Vinod-Kuntoji
                                    Thank you. I've noticed an other issue: the program execution enters fixviewdelegate.cpp but never reads the actual function:

                                    void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const {
                                    
                                      Q_UNUSED(index);
                                      Q_UNUSED(editor);
                                    
                                      qDebug() << "Entered fixViewDelegate (2).";
                                    
                                      QPixmap fixPic;
                                      fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray (), "JPG");
                                      qDebug() << "Fixmodel pic: " << fixPic;
                                      qDebug() << "Fixmodel pic size: " << fixPic.size ();
                                    
                                    }
                                    
                                    

                                    What can I do to fix this problem?

                                    raven-worxR Offline
                                    raven-worxR Offline
                                    raven-worx
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @gabor53
                                    what is output of the following code in the console:

                                    qDebug() << QImageReader::supportedImageFormats();
                                    

                                    Also on what system are you running your application?

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    G 1 Reply Last reply
                                    1
                                    • raven-worxR raven-worx

                                      @gabor53
                                      what is output of the following code in the console:

                                      qDebug() << QImageReader::supportedImageFormats();
                                      

                                      Also on what system are you running your application?

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

                                      Hi @raven-worx ,

                                      The output is

                                      ("bmp", "cur", "dds", "gif", "icns", "ico", "jpeg", "jpg", "pbm", "pgm", "png", "ppm", "svg", "svgz", "tga", "tif", "tiff", "wbmp", "webp", "xbm", "xpm").
                                      I run Qt 5.8 on Windows 10.

                                      raven-worxR 1 Reply Last reply
                                      1
                                      • G gabor53

                                        Hi @raven-worx ,

                                        The output is

                                        ("bmp", "cur", "dds", "gif", "icns", "ico", "jpeg", "jpg", "pbm", "pgm", "png", "ppm", "svg", "svgz", "tga", "tif", "tiff", "wbmp", "webp", "xbm", "xpm").
                                        I run Qt 5.8 on Windows 10.

                                        raven-worxR Offline
                                        raven-worxR Offline
                                        raven-worx
                                        Moderators
                                        wrote on last edited by raven-worx
                                        #19

                                        @gabor53
                                        ok that at least ensures you can read jpg images.
                                        Now the only possibility is that the data you retrieve from the database isn't valid imagedata.

                                        QByteArray ba = fixModel->record (1).value ("Pic").toByteArray ();
                                        qDebug() << ba.size() << ba;
                                        

                                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                        If you have a question please use the forum so others can benefit from the solution in the future

                                        G 1 Reply Last reply
                                        3
                                        • raven-worxR raven-worx

                                          @gabor53
                                          ok that at least ensures you can read jpg images.
                                          Now the only possibility is that the data you retrieve from the database isn't valid imagedata.

                                          QByteArray ba = fixModel->record (1).value ("Pic").toByteArray ();
                                          qDebug() << ba.size() << ba;
                                          
                                          G Offline
                                          G Offline
                                          gabor53
                                          wrote on last edited by
                                          #20

                                          @raven-worx
                                          I added it to the

                                          void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const {
                                          
                                          

                                          function but it doesnt display anything. I know program execution goes from fixdb.cpp to FixViewDelegate, but it does nothing with setModelData. As this delegate supposed to handle displaying the image without clicking on the table I think something is missing. How can I render the image using this delegate each time the tableView is displayed?
                                          The delegate so far looks like this:

                                          #include "fixviewdelegate.h"
                                          
                                          FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
                                            qDebug() << "Entered fixViewDelegate (1).";
                                          }
                                          
                                          void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                          
                                            Q_UNUSED(painter);
                                            Q_UNUSED(option);
                                            Q_UNUSED(index);
                                          }
                                          
                                          QSize FixViewDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                          
                                            Q_UNUSED(option);
                                            Q_UNUSED(index);
                                          
                                          }
                                          
                                          void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
                                          
                                            Q_UNUSED(editor);
                                            Q_UNUSED(index);
                                          
                                          }
                                          
                                          void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const {
                                          
                                            Q_UNUSED(index);
                                            Q_UNUSED(editor);
                                          
                                          
                                            qDebug() << "Entered fixViewDelegate (2).";
                                            QByteArray ba = fixModel->record (1).value ("Pic").toByteArray ();
                                            qDebug() << ba.size() << ba;
                                          
                                          //  qDebug() << "Fixmodel pic: " << fixPic;
                                          //  qDebug() << "Fixmodel pic size: " << fixPic.size ();
                                          
                                          }
                                          
                                          
                                          

                                          Thank you for your help.

                                          raven-worxR 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