Results 1 to 3 of 3

Thread: Insert a image into a MySql BLOB database fiel - using Model/View

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Insert a image into a MySql BLOB database fiel - using Model/View

    Hello all,
    In the picture i send it is possible to manage a table containing 3 flields:

    Qt Code:
    1. mysql> describe foto;
    2. +-------+-------------+------+-----+---------+-------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +-------+-------------+------+-----+---------+-------+
    5. | id | int(11) | NO | PRI | NULL | |
    6. | name | varchar(45) | NO | | NULL | |
    7. | image | blob | YES | | NULL | |
    8. +-------+-------------+------+-----+---------+-------+
    9. 3 rows in set (0.00 sec)
    To copy to clipboard, switch view to plain text mode 

    sample.png
    The load button allows me to load an image form disk into a QLabel.
    Now, if i press the add button a key is generated for the id, the name comes from the opened filname and ... the question is:

    How can i manage the data for the hidden column (image) if i want to insert the loaded image into that BLOB?

    Thanks for any tips on this subject

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Insert a image into a MySql BLOB database fiel - using Model/View


  3. The following user says thank you to Lykurg for this useful post:

    graciano (17th February 2010)

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Insert a image into a MySql BLOB database fiel - using Model/View

    Some working code ...
    Qt Code:
    1. void Dialog::beforeInsertFoto(QSqlRecord &record)
    2. {
    3. //prepare data dor id - generated key
    4. record.setValue("id", generateId("foto"));
    5.  
    6. //prepare data for name - get it form the label in the ui
    7. record.setValue("name", ui->fileNameLabel->text());
    8.  
    9. //prepare data for image - get it from the label in the ui
    10. QImage currentImage = ui->foto->pixmap()->toImage();
    11. QByteArray bytes;
    12. QBuffer buffer(&bytes);
    13. buffer.open(QIODevice::WriteOnly);
    14. currentImage.save(&buffer, "PNG");
    15. record.setValue("image", bytes);
    16. }
    17.  
    18. void Dialog::insertNewRecord()
    19. {
    20. int row = modeloFoto->rowCount();
    21. modeloFoto->insertRow(row);
    22. modeloFoto->submitAll();
    23.  
    24. }
    25.  
    26. void Dialog::getCurrentRecord()
    27. {
    28. //get image filename
    29. QModelIndex index = vistaFoto->currentIndex();
    30. ui->fileNameLabel->setText(modeloFoto->data(modeloFoto->index(index.row(), foto_name),0).toString());
    31.  
    32. //get image
    33. QVariant currentImage = modeloFoto->data(modeloFoto->index(index.row(), foto_image),0);
    34. QByteArray bytes = currentImage.toByteArray();
    35. QImage image;
    36. image.loadFromData(bytes);
    37. ui->foto->setPixmap(QPixmap::fromImage(image));
    38. }
    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

Similar Threads

  1. how to save a image file ( say png ) into mysql database?
    By AviMittal in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2011, 12:49
  2. Inserting blob into mysql using QT?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 31st August 2010, 04:37
  3. Replies: 1
    Last Post: 14th September 2009, 08:48
  4. Model/View -- Sql Insert
    By kroenecker in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2007, 15:55
  5. Replies: 7
    Last Post: 12th August 2006, 15:11

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.