Results 1 to 7 of 7

Thread: Convert QStandardItemModel to QImage

  1. #1
    Join Date
    Jun 2017
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Convert QStandardItemModel to QImage

    Hey,

    is it possible to convert a QStandradItemModel to QImage?
    I want to save my model as a tiff.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Convert QStandardItemModel to QImage

    A model is something logical, not visual, which is why there is a separation of a model and a view - you can visualize the data in the model in any number of ways.
    In deed with Qt you can simultaneously show the same model in very different views at the same time.
    You can't "convert" a model to an image.
    What you can do is visualize the data, and convert that visualization to an image.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jun 2017
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert QStandardItemModel to QImage

    I visualize the model in a tableView and I can change in the table the colors for every "pixel". And now I try to convert my model to a QImage. I can read out each item of the model to get the background color of each field.
    But how I put these information in a QImage. I googled already a bit, but didnt find a good first impression.
    Maybe someone have a tip!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert QStandardItemModel to QImage

    What you are saying doesn't make a lot of sense. As high_flyer said, a "model" is just a collection of data. The "view" determines how the data is displayed on screen or paper. And the QTableView isn't an image - it is a widget that displays a model as a matrix of rows and columns.

    If you want to make a screenshot of the table view, you can call QWidget::render() and give it the address of a pixmap to render into. But unless your model is small, this will only be part of the model - what is displayed on the screen at that moment.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Convert QStandardItemModel to QImage

    You could use the model's APIs to read the background color of each field (as you say) and put them in QImage using QImage::setPixelColor(), iterating over all the rows and columns. Take care of creating a proper QImage as required QImage::QImage(const QSize &size, Format format);

    I think there is something else which you are missing to mention.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    Pschnot1000 (28th June 2017)

  7. #6
    Join Date
    Jun 2017
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert QStandardItemModel to QImage

    Thank a lot Santosh Reddy,

    that is the comment that I needed. It works fine.

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert QStandardItemModel to QImage

    that is the comment that I needed. It works fine.
    So what are you actually trying to do? Make some kind of heatmap image based on the color-coded background of your model cells? You should have said that from the start - saying that you wanted to save your model as an image isn't actually what you wanted.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Convert 8bpp Image to Qimage
    By pyromanci in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2017, 20:48
  2. How to convert QString to QImage
    By gunturrohith in forum Qt Programming
    Replies: 12
    Last Post: 28th July 2015, 07:34
  3. Convert Html content into QStandardItemModel
    By nikhilqt in forum Qt Programming
    Replies: 5
    Last Post: 29th February 2012, 20:46
  4. How To Convert CGImageRef to QImage
    By nareshqt in forum Qt Programming
    Replies: 0
    Last Post: 23rd June 2008, 08:21
  5. Convert RAW 8 bit pixels into a QImage
    By danielperaza in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2008, 14: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.