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. QStiledItemDelegate setModelData doesn't display image
Forum Updated to NodeBB v4.3 + New Features

QStiledItemDelegate setModelData doesn't display image

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 3 Posters 5.4k Views 2 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 gabor53
    #1

    Hi,
    My fixviewdelegate.cpp looks like this:

    #include "fixviewdelegate.h"
    
    FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
      qDebug() << "Entered fixViewDelegate (1).";
    }
    
    QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    
      qDebug() << "Entered createEditor";
      Q_UNUSED(option);
      Q_UNUSED(index);
    
    
      return new ImagePickButton(parent);
    
    
    }
    
    void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    
      QPixmap loadedImage;
    
      loadedImage.loadFromData (index.data ().toByteArray ()) ;
      loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio);
    
      const QWidget* widget = option.widget;
    
    
      QStyle* style = widget ? widget->style() : QApplication::style();
      style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage);
    
    }
    
    void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
      Q_UNUSED(index);
      editor->setGeometry(option.rect);
    }
    
    void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
    
      qDebug() << "Entered setEditorData.";
      ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
      Q_ASSERT(imgPick);
      imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
    
    }
    
    void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
      qDebug() << "Entered setModelData. ";
    
      ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
      Q_ASSERT(imgPick);
    
      QString value;
      value = imgPick->selectedFile ();
    
      QPixmap newImage(value);
      newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
      qDebug() << "newImage size: " << newImage.size ();
    
      qDebug()  << "Index: " << index;
    
      fixModel->setData(index, newImage, Qt::DecorationRole);
    
    }
    

    The setModelData has the newImage pixmap (qDebug displays the size correctly) and the index is correct and valid. The new image appears on the editor, but it is not written back to the model (the model is not updated). Is it because

    • I am using a wrong image format,

    • I am not supposed to do this in setModelData,

    • something is missing,

    • or something else is incorrect?
      Thank you for your help.

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

      Hi,

      Does your model handle properly the Qt::DecorationRole ?

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

        Hi,

        Does your model handle properly the Qt::DecorationRole ?

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

        @SGaist
        How can I find out? What does it look like when the model handles it correctly and when it doesn't?

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

          What type of model does your view use ?

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

            What type of model does your view use ?

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

            @SGaist
            QSqlTableModel

            I modified setModelData like this:

              if(fixModel->setData(index, newImage, Qt::EditRole) == true) {
                qDebug() << "Data IS set. ";
              } else {
                qDebug() << "Data is NOT set. ";
              }
            

            It gave the Data IS set message, but the new image still doesn't show in the tableView managed by the model.

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

              Except that you changed the role used between your different answers so it doesn't make things any clearer.

              Can you post the complete code you are actually using ?

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

                Except that you changed the role used between your different answers so it doesn't make things any clearer.

                Can you post the complete code you are actually using ?

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

                @SGaist
                Sorry for the confusion.

                void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                  qDebug() << "Entered setModelData. ";
                
                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                  Q_ASSERT(imgPick);
                
                  QString value;
                  value = imgPick->selectedFile ();
                
                  QPixmap newImage(value);
                  newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                  qDebug() << "newImage size: " << newImage.size ();
                
                  qDebug()  << "Index: " << index;
                
                  if(fixModel->setData(index, newImage, Qt::EditRole) == true) {
                    qDebug() << "Data IS set. ";
                  } else {
                    qDebug() << "Data is NOT set. ";
                  }
                
                
                }
                

                DecorationRole gives Data is NOT set.

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

                  I feel like you are a bit confused about roles.

                  • in FixViewDelegate::paint, loadedImage.loadFromData (index.data ().toByteArray ()) ; takes the data from Qt::DisplayRole (default argument to data()) as a byte-array as it was an encoded pixmap
                  • in FixViewDelegate::setEditorData, imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); takes the path to the image from Qt::UserRole
                  • in FixViewDelegate::setModelData you directly save the QPixmap in fixModel->setData(index, newImage, Qt::DecorationRole);

                  So the answer to your question is chose 1 way to save and load your image and use it across all your methods.

                  This is what I'd do to fix your code:

                  • add fixModel->setData(index, value, Qt::UserRole); as the last line of FixViewDelegate::setModelData
                  • in FixViewDelegate::paint remove loadedImage.loadFromData (index.data ().toByteArray ()) ;
                  • in FixViewDelegate::paint change QPixmap loadedImage; to QPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>();

                  Optional but encouraged:

                  • add a check for value being empty and/or newImage being null in FixViewDelegate::setModelData

                  "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
                  2
                  • VRoninV VRonin

                    I feel like you are a bit confused about roles.

                    • in FixViewDelegate::paint, loadedImage.loadFromData (index.data ().toByteArray ()) ; takes the data from Qt::DisplayRole (default argument to data()) as a byte-array as it was an encoded pixmap
                    • in FixViewDelegate::setEditorData, imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); takes the path to the image from Qt::UserRole
                    • in FixViewDelegate::setModelData you directly save the QPixmap in fixModel->setData(index, newImage, Qt::DecorationRole);

                    So the answer to your question is chose 1 way to save and load your image and use it across all your methods.

                    This is what I'd do to fix your code:

                    • add fixModel->setData(index, value, Qt::UserRole); as the last line of FixViewDelegate::setModelData
                    • in FixViewDelegate::paint remove loadedImage.loadFromData (index.data ().toByteArray ()) ;
                    • in FixViewDelegate::paint change QPixmap loadedImage; to QPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>();

                    Optional but encouraged:

                    • add a check for value being empty and/or newImage being null in FixViewDelegate::setModelData
                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    @VRonin
                    I made the following changes as you recommended:

                    #include "fixviewdelegate.h"
                    
                    FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
                      qDebug() << "Entered fixViewDelegate (1).";
                    }
                    
                    QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                    
                      qDebug() << "Entered createEditor";
                      Q_UNUSED(option);
                      Q_UNUSED(index);
                    
                    
                      return new ImagePickButton(parent);
                    
                    
                    }
                    
                    void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                    
                      QPixmap loadedImage;
                    
                    
                    
                      loadedImage = index.data (Qt::DecorationRole).value<QPixmap>();
                      loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio);
                      const QWidget* widget = option.widget;
                    
                    
                      QStyle* style = widget ? widget->style() : QApplication::style();
                      style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage);
                    
                    }
                    
                    void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                      Q_UNUSED(index);
                      editor->setGeometry(option.rect);
                    }
                    
                    void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
                    
                      qDebug() << "Entered setEditorData.";
                      ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                      Q_ASSERT(imgPick);
                      imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
                    
                    }
                    
                    void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                      qDebug() << "Entered setModelData. ";
                    
                      ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                      Q_ASSERT(imgPick);
                    
                      QString value;
                      value = imgPick->selectedFile ();
                    
                      if(value == "") {
                        qDebug() << "value is empty.";
                      } else {
                        qDebug() << "value is NOT empty.";
                      }
                    
                      QPixmap newImage(value);
                      newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                      qDebug() << "newImage size: " << newImage.size ();
                    
                      qDebug()  << "Index: " << index;
                    
                      if(fixModel->setData(index, value, Qt::UserRole) == true) {
                        qDebug() << "Data IS set. ";
                      } else {
                        qDebug() << "Data is NOT set. ";
                      }
                    }
                    

                    paint stopped working. There is no image in the tableView's image column.

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

                      Sorry, my comments were based on your original post, not the subsequent changes. put fixModel->setData(index, newImage, Qt::DecorationRole); back in setModelData

                      "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

                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        I feel like you are a bit confused about roles.

                        • in FixViewDelegate::paint, loadedImage.loadFromData (index.data ().toByteArray ()) ; takes the data from Qt::DisplayRole (default argument to data()) as a byte-array as it was an encoded pixmap
                        • in FixViewDelegate::setEditorData, imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); takes the path to the image from Qt::UserRole
                        • in FixViewDelegate::setModelData you directly save the QPixmap in fixModel->setData(index, newImage, Qt::DecorationRole);

                        So the answer to your question is chose 1 way to save and load your image and use it across all your methods.

                        This is what I'd do to fix your code:

                        • add fixModel->setData(index, value, Qt::UserRole); as the last line of FixViewDelegate::setModelData
                        • in FixViewDelegate::paint remove loadedImage.loadFromData (index.data ().toByteArray ()) ;
                        • in FixViewDelegate::paint change QPixmap loadedImage; to QPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>();

                        Optional but encouraged:

                        • add a check for value being empty and/or newImage being null in FixViewDelegate::setModelData
                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #11

                        @VRonin
                        I have the following code (hopefully I made all the changes correctly):

                        #include "fixviewdelegate.h"
                        
                        FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
                          qDebug() << "Entered fixViewDelegate (1).";
                        }
                        
                        QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                        
                          qDebug() << "Entered createEditor";
                          Q_UNUSED(option);
                          Q_UNUSED(index);
                        
                        
                          return new ImagePickButton(parent);
                        
                        
                        }
                        
                        void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                        
                        
                        
                        
                          QPixmap loadedImage = index.data (Qt::DecorationRole).value<QPixmap>();
                          loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio);
                          const QWidget* widget = option.widget;
                        
                        
                          QStyle* style = widget ? widget->style() : QApplication::style();
                          style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage);
                        
                        }
                        
                        void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                          Q_UNUSED(index);
                          editor->setGeometry(option.rect);
                        }
                        
                        void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
                        
                          qDebug() << "Entered setEditorData.";
                          ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                          Q_ASSERT(imgPick);
                          imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
                        
                        }
                        
                        void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                          qDebug() << "Entered setModelData. ";
                        
                          ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                          Q_ASSERT(imgPick);
                        
                          QString value;
                          value = imgPick->selectedFile ();
                        
                          if(value == "") {
                            qDebug() << "value is empty.";
                          } else {
                            qDebug() << "value is NOT empty.";
                          }
                        
                          QPixmap newImage(value);
                          newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                          qDebug() << "newImage size: " << newImage.size ();
                        
                          qDebug()  << "Index: " << index;
                        
                          if(fixModel->setData(index, newImage, Qt::DecorationRole) == true) {
                            qDebug() << "Data IS set. ";
                          } else {
                            qDebug() << "Data is NOT set. ";
                          }
                        }
                        
                        

                        I still has the same problems:

                        • fixViewDelegate::paint doesn't display the images

                        • fixViewDelegate::setData says
                          value is NOT empty.
                          newImage size: QSize(75, 100)
                          Index: QModelIndex(2,2,0x0,QSqlTableModel(0x2130a3f0))
                          Data is NOT set. and the chosen image is not displayed.
                          Thank you for your help.

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

                          Like I already wrote somewhere else: QSqlTableModel doesn't handle Qt::Decoration in its setData implementation (or rather it calls the base class implementation and following up that chain, it ends with nothing done) so if you want to use it you have to handle it yourself.

                          Otherwise, you can use the EditRole as usual to store the data and paint the image when the decoration role is requested..

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

                            Like I already wrote somewhere else: QSqlTableModel doesn't handle Qt::Decoration in its setData implementation (or rather it calls the base class implementation and following up that chain, it ends with nothing done) so if you want to use it you have to handle it yourself.

                            Otherwise, you can use the EditRole as usual to store the data and paint the image when the decoration role is requested..

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

                            @SGaist
                            I changed setModelData:

                            void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                              qDebug() << "Entered setModelData. ";
                            
                              ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                              Q_ASSERT(imgPick);
                            
                              QString value;
                              value = imgPick->selectedFile ();
                            
                              QPixmap newImage(value);
                              newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                              qDebug() << "newImage size: " << newImage.size ();
                            
                              qDebug()  << "Index: " << index;
                            
                              if(fixModel->setData(index, newImage, Qt::EditRole) == true) {
                                qDebug() << "Data IS set. ";
                              } else {
                                qDebug() << "Data is NOT set. ";
                              }
                            }
                            

                            Everything looks ok (newImage exists and Data is set), but it still doesn't paint the image and it is not saved in the db either. Do I need to implement saving and painting, or something is still incorrect?

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

                              Apologies, I did not read the model was QSqlTableModel. You can put a proxy like this one (You'll have to add if(role==Qt::EditRole) return QIdentityProxyModel::setData(index,value,role); in line 29) in between to make the multiple roles work. On the other hand, you latest implementations of setModelData look like you do not completely understand how models work and you are panicking and try to mix a bunch of stuff together hoping that it works

                              #include "fixviewdelegate.h"
                              
                              FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
                                qDebug() << "Entered fixViewDelegate (1).";
                              }
                              
                              QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                              
                                qDebug() << "Entered createEditor";
                                Q_UNUSED(option);
                                Q_UNUSED(index);
                              
                              
                                return new ImagePickButton(parent);
                              
                              
                              }
                              
                              void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                              
                               QPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>();
                              
                              
                               
                              
                                const QWidget* widget = option.widget;
                              
                              
                                QStyle* style = widget ? widget->style() : QApplication::style();
                                style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage);
                              
                              }
                              
                              void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                Q_UNUSED(index);
                                editor->setGeometry(option.rect);
                              }
                              
                              void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
                              
                                qDebug() << "Entered setEditorData.";
                                ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                                Q_ASSERT(imgPick);
                                imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
                              
                              }
                              
                              void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                                qDebug() << "Entered setModelData. ";
                              
                                ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                                Q_ASSERT(imgPick);
                              
                                QString value;
                                value = imgPick->selectedFile ();
                              
                                QPixmap newImage(value);
                              if(newImage.isNull()){
                              fixModel->setData(index, QVariant(), Qt::DecorationRole);
                              fixModel->setData(index, QVariant(), Qt::UserRole);
                              return;
                              }
                                newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                                qDebug() << "newImage size: " << newImage.size ();
                              
                                qDebug()  << "Index: " << index;
                              
                                fixModel->setData(index, newImage, Qt::DecorationRole);
                              fixModel->setData(index, value, Qt::UserRole);
                              }
                              
                              

                              You will still need to use the proxy I linked above inbetween

                              "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 1 Reply Last reply
                              1
                              • VRoninV VRonin

                                Apologies, I did not read the model was QSqlTableModel. You can put a proxy like this one (You'll have to add if(role==Qt::EditRole) return QIdentityProxyModel::setData(index,value,role); in line 29) in between to make the multiple roles work. On the other hand, you latest implementations of setModelData look like you do not completely understand how models work and you are panicking and try to mix a bunch of stuff together hoping that it works

                                #include "fixviewdelegate.h"
                                
                                FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
                                  qDebug() << "Entered fixViewDelegate (1).";
                                }
                                
                                QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                
                                  qDebug() << "Entered createEditor";
                                  Q_UNUSED(option);
                                  Q_UNUSED(index);
                                
                                
                                  return new ImagePickButton(parent);
                                
                                
                                }
                                
                                void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                
                                 QPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>();
                                
                                
                                 
                                
                                  const QWidget* widget = option.widget;
                                
                                
                                  QStyle* style = widget ? widget->style() : QApplication::style();
                                  style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage);
                                
                                }
                                
                                void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                  Q_UNUSED(index);
                                  editor->setGeometry(option.rect);
                                }
                                
                                void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
                                
                                  qDebug() << "Entered setEditorData.";
                                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                                  Q_ASSERT(imgPick);
                                  imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
                                
                                }
                                
                                void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                                  qDebug() << "Entered setModelData. ";
                                
                                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                                  Q_ASSERT(imgPick);
                                
                                  QString value;
                                  value = imgPick->selectedFile ();
                                
                                  QPixmap newImage(value);
                                if(newImage.isNull()){
                                fixModel->setData(index, QVariant(), Qt::DecorationRole);
                                fixModel->setData(index, QVariant(), Qt::UserRole);
                                return;
                                }
                                  newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                                  qDebug() << "newImage size: " << newImage.size ();
                                
                                  qDebug()  << "Index: " << index;
                                
                                  fixModel->setData(index, newImage, Qt::DecorationRole);
                                fixModel->setData(index, value, Qt::UserRole);
                                }
                                
                                

                                You will still need to use the proxy I linked above inbetween

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

                                @VRonin
                                Thank you.
                                I created a class named ExtraRolesProxyModel.
                                I added

                                #include "extrarolesproxymodel.h"
                                

                                to fixviewdelegate.h.
                                My fixviewdelegate.cpp looks like this now:

                                #include "fixviewdelegate.h"
                                
                                FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {
                                  qDebug() << "Entered fixViewDelegate (1).";
                                }
                                
                                QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                
                                  qDebug() << "Entered createEditor";
                                  Q_UNUSED(option);
                                  Q_UNUSED(index);
                                
                                
                                  return new ImagePickButton(parent);
                                
                                
                                }
                                
                                void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                
                                  if(role == Qt::EditRole) return;
                                  QIdentityProxyModel::setData(index, value, role);
                                
                                  QPixmap loadedImage = index.data (Qt::DecorationRole).value<QPixmap>();
                                
                                //  loadedImage.loadFromData (index.data ().toByteArray ()) ;
                                //  loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio);
                                
                                  const QWidget* widget = option.widget;
                                
                                 QStyle* style = widget ? widget->style() : QApplication::style();
                                  style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage);
                                }
                                
                                void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                  Q_UNUSED(index);
                                  editor->setGeometry(option.rect);
                                }
                                
                                void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
                                
                                  qDebug() << "Entered setEditorData.";
                                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                                  Q_ASSERT(imgPick);
                                  imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
                                
                                }
                                
                                void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const {
                                  qDebug() << "Entered setModelData. ";
                                
                                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                                  Q_ASSERT(imgPick);
                                
                                  QString value;
                                  value = imgPick->selectedFile ();
                                
                                  QPixmap newImage(value);
                                  if(newImage.isNull ()) {
                                    fixModel->setData (index, QVariant(), Qt::DecorationRole);
                                    fixModel->setData (index, QVariant(), Qt::UserRole);
                                    return;
                                  }
                                
                                
                                  newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio);
                                  qDebug() << "newImage size: " << newImage.size ();
                                
                                  qDebug()  << "Index: " << index;
                                
                                //  if(fixModel->setData(index, newImage, Qt::EditRole) == true) {
                                //    qDebug() << "Data IS set. ";
                                //  } else {
                                //    qDebug() << "Data is NOT set. ";
                                //  };
                                
                                  fixModel->setData (index, newImage, Qt::DecorationRole);
                                  fixModel->setData (index, newImage, Qt::UserRole);
                                }
                                

                                When I build it it gives the following errors:
                                'role' was not declared in this scope.
                                'value' was not declared in this scope.
                                Did I add the proxy incorrectly?

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

                                  That's a proxy model, it has nothing to do with the delegate. I have no idea why you merged the 2. I gave you the code of both the delegate and the proxy model. You just have to copy-paste them, add 1 single line I posted at the place I posted inside extrarolesproxymodel.h it and then use them

                                  "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 1 Reply Last reply
                                  1
                                  • VRoninV VRonin

                                    That's a proxy model, it has nothing to do with the delegate. I have no idea why you merged the 2. I gave you the code of both the delegate and the proxy model. You just have to copy-paste them, add 1 single line I posted at the place I posted inside extrarolesproxymodel.h it and then use them

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

                                    @VRonin
                                    Is

                                    if(role == Qt::EditRole) return QIdentityProxyModel::setData(index, value, role);
                                    

                                    goes into extrarolesproxymodel.h or cpp?
                                    There are no line 29 in .h. If it goes to the cpp file it replaces line 29, goes before or after?
                                    I tried to add it before and after

                                     if(!index.isValid()) return false;
                                    

                                    in cpp. Tried to place it before and after line 29 and actually replaced line 29 but still images were not displayed.

                                    VRoninV 1 Reply Last reply
                                    0
                                    • G gabor53

                                      @VRonin
                                      Is

                                      if(role == Qt::EditRole) return QIdentityProxyModel::setData(index, value, role);
                                      

                                      goes into extrarolesproxymodel.h or cpp?
                                      There are no line 29 in .h. If it goes to the cpp file it replaces line 29, goes before or after?
                                      I tried to add it before and after

                                       if(!index.isValid()) return false;
                                      

                                      in cpp. Tried to place it before and after line 29 and actually replaced line 29 but still images were not displayed.

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #18

                                      @gabor53 said in QStiledItemDelegate setModelData doesn't display image:

                                      goes into extrarolesproxymodel.h or cpp?

                                      The link I posted is header only so I don't know what you are referring to. Use the line numbering you see in pastebin to understand where to put it

                                      "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 1 Reply Last reply
                                      0
                                      • VRoninV VRonin

                                        @gabor53 said in QStiledItemDelegate setModelData doesn't display image:

                                        goes into extrarolesproxymodel.h or cpp?

                                        The link I posted is header only so I don't know what you are referring to. Use the line numbering you see in pastebin to understand where to put it

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

                                        @VRonin
                                        extraroleproxymodel.h looks like this:

                                        
                                        #include <QIdentityProxyModel>
                                        #include <QVariant>
                                        #include <QHash>
                                        class ExtraRolesProxyModel : public QIdentityProxyModel {
                                          Q_OBJECT
                                          Q_DISABLE_COPY(ExtraRolesProxyModel)
                                         public:
                                          ExtraRolesProxyModel(QObject* parent = Q_NULLPTR)
                                            : QIdentityProxyModel(parent) {
                                        
                                          }
                                          virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE {
                                            if(!index.isValid()) return QIdentityProxyModel::data(index, role);
                                            const QHash<int, QHash<int, QHash<int, QVariant> > >::const_iterator rowIter = m_extraData.constFind(index.row());
                                            if(rowIter == m_extraData.cend())
                                              return QIdentityProxyModel::data(index, role);
                                            const QHash<int, QHash<int, QVariant> >::const_iterator colIter = rowIter->constFind(index.column());
                                            if(colIter == rowIter->cend())
                                              return QIdentityProxyModel::data(index, role);
                                            const QHash<int, QVariant>::const_iterator roleIter = colIter->constFind(role);
                                            if(roleIter == colIter->cend())
                                              return QIdentityProxyModel::data(index, role);
                                            return roleIter.value();
                                          }
                                          virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)Q_DECL_OVERRIDE{
                                            if(!index.isValid()) return false;
                                            if(role == Qt::EditRole) return QIdentityProxyModel::setData(index, value, role);
                                            if(index.parent().isValid()) return QIdentityProxyModel::setData(index, value, role);
                                            QHash<int, QHash<int, QHash<int, QVariant> > >::iterator rowIter = m_extraData.find(index.row());
                                            if(rowIter == m_extraData.end()) {
                                              if(value.isNull()) return false;
                                              rowIter = m_extraData.insert(index.row(), QHash<int, QHash<int, QVariant> >());
                                            }
                                            QHash<int, QHash<int, QVariant> >::iterator colIter = rowIter->find(index.column());
                                            if(colIter == rowIter->end()) {
                                              if(value.isNull()) return false;
                                              colIter = rowIter->insert(index.column(), QHash<int, QVariant>());
                                            }
                                            QHash<int, QVariant>::iterator roleIter = colIter->find(role);
                                            if(roleIter == colIter->end()) {
                                              if(value.isNull()) return false;
                                              roleIter = colIter->insert(role, value);
                                              return true;
                                            }
                                            if(value.isNull()) {
                                              colIter->erase(roleIter);
                                              if(colIter->isEmpty()) {
                                                rowIter->erase(colIter);
                                                if(rowIter->isEmpty()) {
                                                  m_extraData.erase(rowIter);
                                                }
                                              }
                                              return true;
                                            }
                                            roleIter.value() = value;
                                            return true;
                                          }
                                         private:
                                          QHash<int, QHash<int, QHash<int, QVariant> > > m_extraData;
                                        };
                                        
                                        

                                        It gives the following errors:
                                        C:\Programming\Projects\Folkfriends_bzr\checkout\extrarolesproxymodel.cpp:5: error: redefinition of 'QVariant ExtraRolesProxyModel::data(const QModelIndex&, int) const'
                                        QVariant ExtraRolesProxyModel::data(const QModelIndex& index, int role) const {
                                        ^
                                        C:\Programming\Projects\Folkfriends_bzr\checkout\extrarolesproxymodel.cpp:21: error: redefinition of 'bool ExtraRolesProxyModel::setData(const QModelIndex&, const QVariant&, int)'
                                        bool ExtraRolesProxyModel::setData(const QModelIndex& index, const QVariant& value, int role) {
                                        ^
                                        Am I still doing something incorrectly?
                                        Thank you.

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

                                          Like the error suggests: you have the functions implemented twice. Once in the header and once in the .cpp file.

                                          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
                                          0

                                          • Login

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