PDA

View Full Version : Convert QStandardItemModel to QImage



Pschnot1000
27th June 2017, 15:31
Hey,

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

high_flyer
27th June 2017, 15:46
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.

Pschnot1000
27th June 2017, 16:13
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!

d_stranz
27th June 2017, 18:31
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.

Santosh Reddy
28th June 2017, 06:07
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.

Pschnot1000
28th June 2017, 07:22
Thank a lot Santosh Reddy,

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

d_stranz
28th June 2017, 18:06
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.