Hey,
is it possible to convert a QStandradItemModel to QImage?
I want to save my model as a tiff.
Hey,
is it possible to convert a QStandradItemModel to QImage?
I want to save my model as a tiff.
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.
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!
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.
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.
Pschnot1000 (28th June 2017)
Thank a lot Santosh Reddy,
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.that is the comment that I needed. It works fine.
<=== 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.
Bookmarks