title: How to pass images on QGraphicsView to QTableView programmatically using QPushButton
body: <p>I have been researching for a while how to store images loaded on a <code>QGraphicsView</code> into rows of a <code>QTableView</code> using a <code>QPushButton</code> in a programmatic way but the information I found so far are not that many. </p>
  
  <p>I have 1 <code>QGraphicsView</code>, a <code>QPushButton</code> (Send button) and a <code>QTableView</code> and a <code>QLineEdit</code>. When I upload images using the load button I show them both on the <code>QGraphicsView</code> and on the <code>QLineEidt</code> (I show the path of the image), if I click the Send button, the text of the <code>QLineEdit</code> should be added in the first row of the <code>QTableView</code> (which is happening) and the image should be stored inside the <code>QTableView</code>. </p>
  
  <p>However, the image on the <code>QGraphicsView</code> is not being stored to the <code>QTableView</code> and nothing is being passed.</p>
  
  <p><strong>Currently this is what happens:</strong></p>
  
  <p><img src="https://i.imgur.com/9hlgdIH.png" alt="wrong"></p>
  
  <p><strong>The expected behavior would be:</strong></p>
  
  <p><img src="https://i.imgur.com/1ZoYIpr.png" alt="expected"></p>
  
  <p>I created an <code>ItemDelegate</code> class that takes care of the resizing of the image on the <code>QGraphicsView</code> to be stored inside the <code>QTableView</code> 
  That part is shown below:</p>
  
  <p>This is the <strong>mainwindow.h</strong></p>
  
  <pre><code>#include &lt;QGraphicsView&gt;
  #include &lt;QGraphicsScene&gt;
  #include "imagedelegate.h"
  
  namespace Ui {
  class MainWindow;
  }
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  public:
      explicit MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
      void addData();
      void paint(QPainter * painter, const QStyleOptionViewItem &amp; option, const QModelIndex &amp; index) const;
  
  private slots:
      void on_sendBtn_clicked();
      void on_loadBtn_clicked();
  private:
      Ui::MainWindow *ui;
      QStandardItemModel *model;
      QGraphicsScene *leftScene;
  };
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>and here is the <strong>mainwindow.cpp</strong></p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  #include "imagedelegate.h"
  #include &lt;QGraphicsPixmapItem&gt;
  #include &lt;QBuffer&gt;
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      leftScene = new QGraphicsScene(this);
      ui-&gt;graphicsView-&gt;setScene(leftScene);
      ui-&gt;graphicsView-&gt;show();
      model = new QStandardItemModel();
      ui-&gt;tableView-&gt;setModel(model);
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  void MainWindow::addData()
  {
      // Path on the first column
      QStandardItem *pathAItem = new QStandardItem(ui-&gt;lineEdit-&gt;text());
  
      // Image on the second column - not working yet
      //QStandardItem *image1 = new QStandardItem(/*ui-&gt;graphicsView-&gt;*/);
  
      QPixmap image1;
      QByteArray img1Array;
      QBuffer buffer1(&amp;img1Array);
      buffer1.open(QIODevice::WriteOnly);
      image1.save(&amp;buffer1, "PNG");
  
      QList&lt;QStandardItem*&gt; row;
      row &lt;&lt; pathAItem;
      model-&gt;setColumnCount(1);
      model-&gt;appendRow(row);
  }
  
  void MainWindow::on_sendBtn_clicked()
  {
      addData();
  }
  
  
  
  void MainWindow::paint(QPainter * painter, const QStyleOptionViewItem &amp; option, const QModelIndex &amp; index) const
  {
      QString colName = index.model()-&gt;headerData(index.column(), Qt::Horizontal).toString();
      if(colName == "image1")
      {
          QPixmap iconPix;
          if(!iconPix.loadFromData(index.model()-&gt;data(index).toByteArray())) {
          }
          iconPix = iconPix.scaledToHeight(32);
          painter-&gt;drawPixmap(option.rect.x(),option.rect.y(),iconPix);
      } else {
  //        QStyledItemDelegate::paint(painter, option, index);
      }
  }
  </code></pre>
  
  <p>The entire code will compile if you copy and paste so that you can see the issue I have.</p>
  
  <p>Please shed light on this matter.</p>