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
Qt 6.11 is out! See what's new in the release blog

Reading image from QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
53 Posts 8 Posters 47.0k 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 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
          • G gabor53

            @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 Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #21

            @gabor53
            2 reasons your csetModelData() isn't called:

            1. the correct signatur for setModelData() would be: void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
            2. setModelData() is used as the name implies to set data in the model. Means when the editor has finished editing and wants to save

            the correct method to reimplement is the delegates paint() and sizeHint() methods. In there you check if the column is your image column and do the work appropriately, else just call the base class implementation.

            --- 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 2 Replies Last reply
            3
            • raven-worxR raven-worx

              @gabor53
              2 reasons your csetModelData() isn't called:

              1. the correct signatur for setModelData() would be: void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
              2. setModelData() is used as the name implies to set data in the model. Means when the editor has finished editing and wants to save

              the correct method to reimplement is the delegates paint() and sizeHint() methods. In there you check if the column is your image column and do the work appropriately, else just call the base class implementation.

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

              @raven-worx
              Thank you. I'm working on it.

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

                @gabor53
                2 reasons your csetModelData() isn't called:

                1. the correct signatur for setModelData() would be: void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
                2. setModelData() is used as the name implies to set data in the model. Means when the editor has finished editing and wants to save

                the correct method to reimplement is the delegates paint() and sizeHint() methods. In there you check if the column is your image column and do the work appropriately, else just call the base class implementation.

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

                @raven-worx
                I believe in the paint() method I need to use

                void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
                

                How can I get the pixmap out of the model?

                artwawA 1 Reply Last reply
                0
                • G gabor53

                  @raven-worx
                  I believe in the paint() method I need to use

                  void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
                  

                  How can I get the pixmap out of the model?

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

                  @gabor53 of course. Model's data() method returns QVariant, you can get QByteArray this way.

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  G 3 Replies Last reply
                  0
                  • artwawA artwaw

                    @gabor53 of course. Model's data() method returns QVariant, you can get QByteArray this way.

                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by gabor53
                    #25
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • artwawA artwaw

                      @gabor53 of course. Model's data() method returns QVariant, you can get QByteArray this way.

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

                      @artwaw
                      The delegate's paint looks like this:

                      void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                      

                      I can get the index of the item from QModelIndex& index, but as there is no model, how can I get the QByteArray from the db (or model)?
                      Thank you.

                      1 Reply Last reply
                      0
                      • artwawA artwaw

                        @gabor53 of course. Model's data() method returns QVariant, you can get QByteArray this way.

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

                        @artwaw
                        As I have no access to the model, is it OK to use a query to get the bytearray from the model?

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

                          Index is your point of access to the model. See QModelIndex::data.

                          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
                          2
                          • SGaistS SGaist

                            Index is your point of access to the model. See QModelIndex::data.

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

                            @SGaist
                            I tried the following:

                            QVariant fixImg;
                            
                              fixImg = index.data (Qt::DisplayRole);
                            
                              QPixmap pixMap2;
                              pixMap2 = fixImg.value<QPixmap>();
                            
                              qDebug() << "pixMap2 size: " << pixMap2.size ();
                              qDebug() << "pixMap2 width: " << pixMap2.width ();
                            
                            

                            No error message, but the size is 0. Did I miss a step?
                            Thank you.

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

                              Do you store a QPixmap in your model for that role ?

                              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
                              0
                              • G Offline
                                G Offline
                                gabor53
                                wrote on last edited by
                                #31

                                It's a QByteArray.

                                jsulmJ 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  Do you store a QPixmap in your model for that role ?

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

                                  @SGaist
                                  I ran

                                   QVariant fixImg(index.data (Qt::EditRole));
                                  
                                    qDebug() << "QVariant type: " << fixImg.type ();
                                  

                                  and the result was
                                  QVariant type: QVariant::QString

                                  1 Reply Last reply
                                  0
                                  • G gabor53

                                    It's a QByteArray.

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #33

                                    @gabor53 said in Reading image from QSqlTableModel:

                                    It's a QByteArray.

                                    QByteArray of what? What and how are you storing in the DB? What is the data type of the corresponding column?

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

                                    G 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @gabor53 said in Reading image from QSqlTableModel:

                                      It's a QByteArray.

                                      QByteArray of what? What and how are you storing in the DB? What is the data type of the corresponding column?

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

                                      @jsulm
                                      QByteArray of the original image. The data type of that column is BLOB.

                                      raven-worxR 1 Reply Last reply
                                      0
                                      • G gabor53

                                        @jsulm
                                        QByteArray of the original image. The data type of that column is BLOB.

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

                                        @gabor53
                                        i asked in a post before:

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

                                        in your delegates paint() method (if really a QByteArray is returned) do the following:

                                        if( index.column() == NUMBER_OF_PIXMAP_COLUMN )
                                        {
                                              QByteArray ba = index.data( ROLE_USED_IN_MODEL ).toByteArray();
                                             QPixmap pix;
                                                pix.loadFromData ( ba );
                                             painter->save();
                                                 painter->translate( option.rect.topLeft() );
                                                 painter->drawPixmap( pix );
                                             painter->restore();
                                        }
                                        

                                        I you should probably post your model code to avoid further guessing.

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

                                        VRoninV 1 Reply Last reply
                                        0
                                        • raven-worxR raven-worx

                                          @gabor53
                                          i asked in a post before:

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

                                          in your delegates paint() method (if really a QByteArray is returned) do the following:

                                          if( index.column() == NUMBER_OF_PIXMAP_COLUMN )
                                          {
                                                QByteArray ba = index.data( ROLE_USED_IN_MODEL ).toByteArray();
                                               QPixmap pix;
                                                  pix.loadFromData ( ba );
                                               painter->save();
                                                   painter->translate( option.rect.topLeft() );
                                                   painter->drawPixmap( pix );
                                               painter->restore();
                                          }
                                          

                                          I you should probably post your model code to avoid further guessing.

                                          VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on last edited by
                                          #36
                                          • I don't like index.column() == NUMBER_OF_PIXMAP_COLUMN as it ties the delegate to a single model so I consider it bad design, view->setItemDelegateForColumn() should be used instead.
                                          • index.data( ROLE_USED_IN_MODEL ) it's a QSqlTableModel so just delete ROLE_USED_IN_MODEL and use the default
                                          • It's a QStyledItemDelegate so let's use style
                                          void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                          QPixmap loadedImage;
                                          loadedImage.loadFromData(index.data().toByteArray());
                                          const QWidget *widget = option.widget;
                                          QStyle *style = widget ? widget->style() : QApplication::style();
                                          style->drawItemPixmap(painter,option.rect,Qt::AlignCenter,loadedImage);
                                          }
                                          

                                          "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
                                          3

                                          • Login

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