Newbie: Minimal program to display a PNG in a window - c++

All,
I must have a fundamental neuron missing, but I cannot get a simple program to load a PNG file and display it in a window. I'm not sure if it is a QPixmap, a QPicture, or what. All of the samples in the QTCreator are a bit more than I need right now. Baby steps...
I can get the window to display, and the program doesn't barf when I try to load the PNG, but it never gets displayed.
If someone would post a simple program to load a PNG from a file and display it, it would greatly appreciated. (I know, asking a lot, but...).
Thanks!
:bp:

this example is minimal: http://doc.trolltech.com/4.6/widgets-imageviewer.html
You will want to have a look at the function ImageViewer::open():
Build a QImage object from a filename;
Convert your QImage to a QPixmap with QPixmap::fromImage();
Put your QPixmap in a QLabel with QLabel::setPixmap().
The QImage object will automatically chose an appropriate reader according to the format of the image it detects in step 1.

Related

Qt - QPixmap converts png to gray scale?

In Qt I used the following method to load images and set them as the background of QLabels:
QPixmap *pixmap= new QPixmap("home\\Images\\circle.png");
ui->cell11_Image->setPixmap(*pixmap);
ui->cell11_Image->setMask(pixmap->mask());
ui->cell11_Image->show();
delete(pixmap);
With the following result
The thing is: the images are in grayscale. However, here is one of the images
Does anyone know what is going on?
I think your label or one of parent widget is disable. Try to enable and check.

convert Qt qml file to bit map image

I want to take a qml I created in the QTCreator and transform it to a bmp image.
what I want in the bmp image is just the items I created.
I read about grabWindow but this is not realy what I want because it takes a snapshot of the screen and not creating bmp image with only the items in the qml.
Thanks
From QML:
youritem.grabToImage(function(result) {
result.saveToFile("something.bmp");
});
Although a bmp image is a bad idea, those things are huge. png is the best candidate for QML stuff, which is usually more "vector-y".

how vtk do th zoom for jpeg reader and dicom reader

This is an example of a code of vtk to read a jpeg image http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/JPEGReader or dicom file http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadDICOM ,
when I run these projects, i can zomm the image with the right button of the mouse
however, in the code there is nothing in a direct relation with zoom or something like this, so there is something inside the classes
can anyone tell me how vtk can do this zoom because i need it in another project ?
Yes, certainly there is code inside the other classes. vtkRenderWindow, vtkRenderWindowInteractor, and vtkRenderer are all referenced by that sample. You can trace down the zooming code through those. https://github.com/Kitware/VTK/tree/master/Rendering/Core

Qt rendering an image and showing it

I have a small app that renders an image. It's in .ppm format and opens nicely in Mac's Xee image viewer. The image is created in the default project folder.
However, the user doesn't know where the image is after it is rendered and I would like to open it automatically or perhaps offer where to save the image before it is created.
That is the first problem. The second problem is .ppm - it's not opened by default on Windows, you need Irfan Viewer or something alike.
Is there a way to solve both those problems easily in Qt? For instance, the image is created where the user wants and my app displays it in that ppm format without using some other software? And If a user wants to reopen the image, I should probably make it possible, as well.
I am not a Qt, nor a C++ developer so I am struggling a bit with this, but I have to do it.
Thanks in advance for the tips and advices.
If you convert your image to a QImage (if it's not one already), you can specify where and in what format to save it when calling the QImage::save method.

How to update a QLabel to display a QImage

I may be misunderstanding how to do this, hopefully somebody can clarify.
I have placed a QLabel on my main window from inside Qt Designer. Currently, it just displays the letters "TextLabel" when I run the program.
I have a line edit where I type in the path to an image, which then loads the image as a QImage into memory (I have this working).
Finally, I want to display this QImage. I was told this can be done by QLabels. I have done the following, but nothing is happening:
ui.input_label->setPixmap(QPixmap::fromImage(my_qimage));
Why is nothing happening? How can I display this image?
If you are sure the image file is ok but then the problem is probably with loading the file into the QImage. If you are calling QImage::load(), you are probably getting a return value of false.