Qt display image in a new window
I am naive to Qt and GUI programming.
Qt jpg image display
The procedure given for displaying an image is working fine and thank you for providing that. But I want to display an image when I click on radiobutton.
I created a slot, and I connected the button click event to the slot (dispImage is my slot). My slot consists only the code which is working to display an image (First answer in this link).
I am able to compile it and run it. But the o/p is not as we desire.
On but开发者_如何转开发ton click, image window flashes for a sec and disappears.
One more point to share is, I tried the same with windowsflags example present in qt examples.
In this example I want to display the image on the preview window created by us. Even this is also not worked for me.
Please provide me the solution.
Thanks in advance.
This is happening because the method that you're connecting to the slot is creating all of the objects needed to display the image on the stack and they're going out of scope and being destroyed when that method returns. The linked example has the event loop running at the end of the method, so the objects don't go out of scope until the program exits.
You can fix this by making the necessary objects member variables of a class that has application lifetime.
Stu's answer above is correct.
If you don't understand what he's talking about, you may want to get a little bit more familiar with C++ before jumping into Qt. The example you are citing is different from what you are trying to do. It's a main() function whose stack variables aren't going to go out of scope until the process exits (that's when main() returns). The stack variables in a method go out of scope as soon as the method returns.
Using the example you cite as a template for your method, you need to declare the QGraphicsView object as a class variable in the header file for your radioslot object. This will make it so it stays in scope until your radioslot object is destroyed.
精彩评论