tags:

views:

271

answers:

3

Hi, I am naive to Qt and GUI programming.

http://stackoverflow.com/questions/1357960/qt-jpg-image-display/2605463#2605463

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 button 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.

+3  A: 

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 Mackellar
A: 

Dear Simon, Thank u for the reply. I got the point. But didnt understand where to change in the code to acheive the desired o/p. I am attaching the code. Could u please help me out in how can I change the code to get the desired o/p. Thanks in advance. radioslot::radioslot(QWidget parent) : QMainWindow(parent), ui(new Ui::radioslot) { but = new QPushButton("HelloWorld", this); but->resize(100,30); QObject::connect(but, SIGNAL(clicked()),this, SLOT(dispImage())); } void radioslot::dispImage() { / code to display image. first answer of above mentioned link */ }

+1  A: 

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.

Brian Roach