Results 1 to 17 of 17

Thread: Insert and read Image in QLable using QDataWidgetMapper

  1. #1
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Insert and read Image in QLable using QDataWidgetMapper

    I have simple master detail application using QDataWidgetMapper in master table; I added a blob in database and QLable to show the picture and button to select a picture

    The button code

    Qt Code:
    1. QString pix = QFileDialog::getOpenFileName(
    2. this, tr("Open file"), "/MyDocument/", tr("PNG Images (*.jpg)") );
    3.  
    4. QImage image(pix);
    5. ui->pix_label->setPixmap(QPixmap::fromImage(image));
    To copy to clipboard, switch view to plain text mode 

    and the classic mapper code

    Qt Code:
    1. mapper = new QDataWidgetMapper(this);
    2. mapper->setModel(model);
    3. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    4. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    5. mapper->addMapping(ui->id,model->fieldIndex("id"));
    6. mapper->addMapping(ui->fName,model->fieldIndex("fName"));
    7. mapper->addMapping(ui->lName,model->fieldIndex("lName"));
    To copy to clipboard, switch view to plain text mode 

    Now, I tried to add this line but couldn't navigate pictures

    Qt Code:
    1. mapper->addMapping(ui->picture_label,model->fieldIndex("picture"));
    To copy to clipboard, switch view to plain text mode 


    I need help to make that works, I'm a just beginner, so a detailed code is appreciated. Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    The QLabel does not have a property that set/returns its icon as a QByteArray. You need the image data as a QByteArray in order to insert/retrieve it from the blob database field. You will probably need to implement a QItemDelegate to handle the transfer of data to and from this widget. You would need to provide the QItemDelegate::setEditorData() and QItemDelegate::setModelData() functions and attach an instance of the delegate to your widget mapper.

    You should also look at QPixmap::loadFromData(), QPixmap::save(), and QBuffer for manipulating the image data.

  3. #3
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Hum, it looks I need a lot. Is there a sample code ? Why it is so complicated in Qt ? And why there is no reference describe that case ?! I'm sorry

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Not really a lot. Something like this (compiles but untested):
    Qt Code:
    1. class PicLoadingDelegate: public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. PicLoadingDelegate(QObject * parent = 0 ): QItemDelegate(parent) { }
    6.  
    7. void setEditorData( QWidget *editor, const QModelIndex &index ) const
    8. {
    9. QLabel *label = qobject_cast<QLabel *>(editor);
    10. if (label) {
    11. QByteArray imageData = index.data(Qt::EditRole).toByteArray();
    12. QPixmap pixmap;
    13. if (pixmap.loadFromData(imageData))
    14. label->setPixmap(pixmap);
    15. }
    16. }
    17.  
    18. void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    19. {
    20. QLabel *label = qobject_cast<QLabel *>(editor);
    21. if (label) {
    22. QBuffer buf;
    23. buf.open(QIODevice::WriteOnly);
    24. if (label->pixmap()->save(&buf))
    25. model->setData(index, buf.data(), Qt::EditRole);
    26. }
    27. }
    28. };
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    hsm13 (9th November 2011)

  6. #5
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Insert and read Image in QLable using QDataWidgetMapper

    wow that's great; Thank you so much.
    Is PDF file is different than image ? and can I display it in hyperlink label to open it with outside application ? Many thanks again.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Quote Originally Posted by hsm13 View Post
    Is PDF file is different than image ?
    Yes.
    and can I display it in hyperlink label to open it with outside application ?
    Yes.

  8. #7
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    ummm, sample code if found please

  9. #8
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Let's take a deep breath; In fact I found you post here

    http://www.qtcentre.org/threads/4499...light=pdf+blob

    It's a bit different; so where I want some tricky help. sorry for any disturb.

  10. #9
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Good Morning to all,
    I've the same problem, but in a QSqlRelationalModel, so, how I should use this delegate?
    I've got a table with severals column so, now I use a QSqlRelationalDelegate (I've also 3 QCombobox at 3 relations), so how can I switch from one delegate to other? Or I can subclass QSqlRelationalDelegate to obtain this? And your code affect only the QLabel behavior? The other columns will work as before?

    Thanks for your time!

    Michele

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    The delegate code I posted is an example. It assumes that if the editor is a QLabel that the corresponding column in the model is a blob containing an image. It also shows how the current image in the label can be written back to the model. If the editor widget, this was for mapped widgets not a table view, was not a label then no data would be transferred in either direction.

    Delegates are attached to a view, not model or table, either as a whole or to a column/ row of the the view (see QAbstractItemView::setItemDelegate(), setItemDelegateForColumn, setItemDelegateForRow). If you use one delegate for the whole view then your delegate usually needs to differentiate between columns using index.column() and do something different for each in the setEditorData() and setModelData() functions. If you use a delegate on a single column then the example is the sort of thing you need.

    The default relational delegate should look after creating an populating combo box editors for established relationships and do the default for other columns. You can put separate delegates on columns if you wish or write a combined delegate by subclassing the relational delegate.

    What are you trying to achieve?

  12. #11
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    I'm trying to achive the same effect needed by hsm 13 but I'm using a QSqlRelationalTableModel and QDataWidgetMapper, and I don't know how to use your example.
    If I assign your delegate to the mapper [mapper->setItemDelegate(new PicLoadingDelegate(this));], I lose the link of other fields of my form.
    I use QLabel to show the image stored into Blob file.

    Thanks a lot

    Michele

  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    You can only attach the delegate to the mapper as a whole so it will affect all the columns and you need to write the delegate to discriminate between columns accordingly. The related columns should be handed off to the superclass to get default combo box treatment. Something like:
    Qt Code:
    1. class MyDelegate: public QSqlRelationalDelegate {
    2. Q_OBJECT
    3. public:
    4. MyDelegate(QObject *p = 0): QSqlRelationalDelegate(p) { }
    5. void setEditorData(QWidget *editor, const QModelIndex &index) const
    6. {
    7. switch(index.column()) {
    8. case 0: // do nothing special, simple editors
    9. case 1:
    10. QSqlRelationalDelegate::setEditorData(editor, index);
    11. break;
    12. case 2: // do something special for blob column 2
    13. {
    14. QLabel *label = qobject_cast<QLabel*>(editor);
    15. Q_ASSERT(label);
    16. // blob loading code
    17. break;
    18. }
    19. case 3: // the related column gets default treatment
    20. default:
    21. QSqlRelationalDelegate::setEditorData(editor, index);
    22. }
    23. }
    24. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    25. {
    26. switch(index.column()) {
    27. case 0: // do nothing special for column 0, 1
    28. case 1:
    29. QSqlRelationalDelegate::setEditorData(editor, index);
    30. break;
    31. case 2: // do something special for blob column 2
    32. {
    33. QLabel *label = qobject_cast<QLabel*>(editor);
    34. Q_ASSERT(label);
    35. // blob saving code
    36. break;
    37. }
    38. case 3: // the related column gets default treatment
    39. default: // so does anything we've missed
    40. QSqlRelationalDelegate::setEditorData(editor, index);
    41. }
    42. }
    43.  
    44. };
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Hello ChrisW67,
    thanks a lot for your help. I've tried something like your code, and I've create my Delegate from your code, but, It seems don't work.
    this is my code:
    Qt Code:
    1. class PicLoadDelegate: public QSqlRelationalDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PicLoadDelegate(QObject * parent = 0 ): QSqlRelationalDelegate(parent) { }
    7. void setEditorData(QWidget *editor, const QModelIndex &index) const
    8. {
    9. switch(index.column()) {
    10. case 9: // do something special for blob column 2
    11. {
    12. QLabel *label = qobject_cast<QLabel*>(editor);
    13. Q_ASSERT(label);
    14. if (label) {
    15. QByteArray imageData = index.data(Qt::EditRole).toByteArray();
    16. QPixmap pixmap;
    17. if (pixmap.loadFromData(imageData))
    18. label->setPixmap(pixmap);
    19. }
    20. break;
    21. }
    22. default:
    23. QSqlRelationalDelegate::setEditorData(editor, index);
    24. }
    25. }
    26. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    27. {
    28. switch(index.column()) {
    29. case 9: // do something special for blob column 2
    30. {
    31. QLabel *label = qobject_cast<QLabel*>(editor);
    32. Q_ASSERT(label);
    33. if (label) {
    34. QBuffer buf;
    35. buf.open(QIODevice::WriteOnly);
    36. if (label->pixmap()->save(&buf))
    37. model->setData(index, buf.data(), Qt::EditRole);
    38. }
    39. break;
    40. }
    41. default: // so does anything we've missed
    42. QSqlRelationalDelegate::setEditorData(editor, index);
    43. }
    44. }
    45. };
    To copy to clipboard, switch view to plain text mode 

    What do you think is the mistake?

    Thanks a lot for your time

    Michele

  15. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Which bit doesn't work? Doesn't compile? Doesn't update the QLabel mapped to column 9 (that's the tenth column)? Doesn't save a label image? Have you single stepped it? Is there valid data in the tenth column of the model?

  16. #15
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    You're right, I must explain better

    It compile and run correctly (i tested with debug), but the image isn't stored or retrived. I load a picture in my QLabel's pixmap, and next send the model->submit() method. But into DB there is no image into BLOB field.

    The DB is SQLITE, do you think I need to use base64 conversion?

    Thanks a lot for your help

    Michele

    You're right, I must explain better

    It compile and run correctly (i tested with debug), but the image isn't stored or retrived. I load a picture in my QLabel's pixmap, and next send the model->submit() method. But into DB there is no image into BLOB field.

    The DB is SQLITE, do you think I need to use base64 conversion?

    Thanks a lot for your help

    Michele


    Added after 47 minutes:


    It's work!!
    I forgot to specify the format into pixmap->save() method.
    So, in setModelData method, now I have:
    Qt Code:
    1. [...]
    2. if (label->pixmap()->save(&buf,"PNG"))
    3. model->setData(index, buf.data(), Qt::EditRole);
    4. [...]
    To copy to clipboard, switch view to plain text mode 
    and it work fine!

    Thank a lot for your help.

    Michele
    Last edited by cia.michele; 10th January 2012 at 09:28.

  17. #16
    Join Date
    Mar 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    Hello everyone,

    I'm kind of new to Qt, and I'm struggling with a similar problem as the one evoked by Michele :-) I'm also trying to synchronize a Qlabel pixmap with a blob in SQLITE3. The only difference I have are :
    * My image blobs are contained in a separate table and I'm using a setRelation(1, QSqlRelation(images, "planteid", "imagedata") on the QSqlRelationalTableModel
    * My images are in "JPG" format

    Everything compiles and works perfectly thanks to your help, but I have a problem at the recording step. The pixmap QByteArray doesn't get sored in the model, neither synchronized in the database. I can save it as a file, which looks ok.
    The only thing I can see is that the call to model->setData(index, buf.data(), Qt::EditRole) returns false. But the Qt documentation and researches I did aren't clear on this.

    Do you have a hint, or an idea of way I could understand the problem further?

  18. #17
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert and read Image in QLable using QDataWidgetMapper

    If the setData() call returns false then it has failed. Never tried updating through a QSqlRelationalTableModel, so this is a best guess.

    The docs talk about the value in setData() being the "index" of the record in the related table. I think they mean the primary key value of the entry in the related table. In the case of the situation of a completely new image, I think you would need to:
    • Use a separate QSqlQuery to insert the binary data into the related table
    • Collect newId = lastInsertId() after the insert. The related table needs to have some sort of auto-populated key for that to work (INTEGER PRIMARY KEY in Sqlite). You could also manually manage the key column.
    • Call setData(index, newId, Qt::EditRole) on the QSqlRelationalTableModel

Similar Threads

  1. Replies: 6
    Last Post: 17th July 2011, 02:06
  2. Insert/Delete points over image
    By sergio87 in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2011, 07:37
  3. Replies: 2
    Last Post: 18th October 2010, 14:33
  4. How to insert an image through Qt
    By thanisha in forum Newbie
    Replies: 1
    Last Post: 26th June 2010, 05:55
  5. Insert image in RTL QTextEdit problem!
    By SudaNix in forum Newbie
    Replies: 2
    Last Post: 23rd October 2009, 22:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.