mardi 19 mars 2019

How to pass images on QGraphicsView to QTableView programmatically using QPushButton

I have been researching for a while how to store images loaded on a QGraphicsView into rows of a QTableView using a QPushButton in a programmatic way but the information I found so far are not that many.

I have 1 QGraphicsView, a QPushButton (Send button) and a QTableView and a QLineEdit. When I upload images using the load button I show them both on the QGraphicsView and on the QLineEidt (I show the path of the image), if I click the Send button, the text of the QLineEdit should be added in the first row of the QTableView (which is happening) and the image should be stored inside the QTableView.

However, the image on the QGraphicsView is not being stored to the QTableView and nothing is being passed.

Currently this is what happens:

wrong

The expected behavior would be:

expected

I created an ItemDelegate class that takes care of the resizing of the image on the QGraphicsView to be stored inside the QTableView That part is shown below:

itemdelegate.h

class ImageDelegate : public QStyledItemDelegate
{
public:
    ImageDelegate(QObject * parent = nullptr);
    void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
};
#endif // IMAGEDELEGATE_H

itemdelegate.cpp

#include "imagedelegate.h"

ImageDelegate::ImageDelegate(QObject * parent) : QStyledItemDelegate(parent)
{}

QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    return QSize(32,32);
    Q_UNUSED(option);
    Q_UNUSED(index);
}

void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    qDebug() << (index.model()->headerData(index.column(), Qt::Horizontal).toString());
    QString colName = index.model()->headerData(index.column(), Qt::Horizontal).toString();
    if(colName == "image1" || colName == "image2")
    {
        QPixmap iconPix;
        if(!iconPix.loadFromData(index.model()->data(index).toByteArray())) {
        }
        iconPix = iconPix.scaledToHeight(32);
        painter->drawPixmap(option.rect.x(),option.rect.y(),iconPix);
    } else {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

And this is the mainwindow.h

#include <QGraphicsView>
#include <QGraphicsScene>
#include "imagedelegate.h"

namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void addData();
private slots:
    void on_sendBtn_clicked();
    void on_loadBtn_clicked();
private:
    Ui::MainWindow *ui;
    QStandardItemModel *model;
    QGraphicsScene *leftScene;
};
#endif // MAINWINDOW_H

and here is the mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "imagedelegate.h"
#include <QGraphicsPixmapItem>
#include <QBuffer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    leftScene = new QGraphicsScene(this);
    ui->graphicsView->setScene(leftScene);
    ui->graphicsView->show();
    model = new QStandardItemModel();
    ui->tableView->setModel(model);
    ui->tableView->setItemDelegate(new ImageDelegate(this));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::addData()
{
    QStandardItem *pathAItem = new QStandardItem(ui->lineEdit->text());
    //QStandardItem *image1 = new QStandardItem(/*ui->graphicsView->*/);

    QPixmap image1;
    QByteArray img1Array;
    QBuffer buffer1(&img1Array);
    buffer1.open(QIODevice::WriteOnly);
    image1.save(&buffer1, "PNG");

    QList<QStandardItem*> row;
    row << pathAItem;
    model->setColumnCount(1);
    model->appendRow(row);
}

void MainWindow::on_sendBtn_clicked()
{
    addData();
}

The entire code will compile if you copy and paste so that you can see the issue I have.

Please shed light on this matter.

Aucun commentaire:

Enregistrer un commentaire