Some working code ...
{
//prepare data dor id - generated key
record.setValue("id", generateId("foto"));
//prepare data for name - get it form the label in the ui
record.setValue("name", ui->fileNameLabel->text());
//prepare data for image - get it from the label in the ui
QImage currentImage
= ui
->foto
->pixmap
()->toImage
();
currentImage.save(&buffer, "PNG");
record.setValue("image", bytes);
}
void Dialog::insertNewRecord()
{
int row = modeloFoto->rowCount();
modeloFoto->insertRow(row);
modeloFoto->submitAll();
}
void Dialog::getCurrentRecord()
{
//get image filename
ui->fileNameLabel->setText(modeloFoto->data(modeloFoto->index(index.row(), foto_name),0).toString());
//get image
QVariant currentImage
= modeloFoto
->data
(modeloFoto
->index
(index.
row(), foto_image
),
0);
image.loadFromData(bytes);
ui
->foto
->setPixmap
(QPixmap::fromImage(image
));
}
void Dialog::beforeInsertFoto(QSqlRecord &record)
{
//prepare data dor id - generated key
record.setValue("id", generateId("foto"));
//prepare data for name - get it form the label in the ui
record.setValue("name", ui->fileNameLabel->text());
//prepare data for image - get it from the label in the ui
QImage currentImage = ui->foto->pixmap()->toImage();
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
currentImage.save(&buffer, "PNG");
record.setValue("image", bytes);
}
void Dialog::insertNewRecord()
{
int row = modeloFoto->rowCount();
modeloFoto->insertRow(row);
modeloFoto->submitAll();
}
void Dialog::getCurrentRecord()
{
//get image filename
QModelIndex index = vistaFoto->currentIndex();
ui->fileNameLabel->setText(modeloFoto->data(modeloFoto->index(index.row(), foto_name),0).toString());
//get image
QVariant currentImage = modeloFoto->data(modeloFoto->index(index.row(), foto_image),0);
QByteArray bytes = currentImage.toByteArray();
QImage image;
image.loadFromData(bytes);
ui->foto->setPixmap(QPixmap::fromImage(image));
}
To copy to clipboard, switch view to plain text mode
I was stuck in this final lines and the loadFromData helped.
Now i can exchange png and other data between objects in the ui/my model and view / MySql database.
Just posting in case somebody needs this and for comments on optimizing this code.
Thanks
Bookmarks