View Full Version : widget doesnt show

5th September 2012, 11:14

I am probably missing something very simple, but i cant figure out what it is.

I have a QMainWIndow MyMainWindow, which shows up just fine. A toolbar holds an action.
When i click on the toolbar item, the action is triggered correctly, and the slot it is connected to is executed. The slot is a member of MyMainWindow.
The slot looks like this:

bool MyMainWin::myslot()
MyWidget c(this);
return 1;

As you can see, i just want it to show my custom widget "MyWidget", but it doesnt work.
It just steps over the c.show() call, but doesnt show anything.

I am using Qt 4.8.2 with VisualStudio 2008 and the QtAddin. Bot widgets/windows have been designed with the QtDesigner. The custom window shows just fine when i place above code right in main().

What am i missing?

5th September 2012, 11:24
You are creating MyWidget on the stack.
try creating it on the heap.

MyWidget *c=new Mywidget(this);

5th September 2012, 11:24
MyWidget is a local variable, so it's going to be destroyed after exiting out of "myslot" scope.
You have to create a widget in the heap (e.g. using new).

5th September 2012, 12:04
yes, that was it. thanks.
Should have seen that myself... :S

Added after 14 minutes:

Although, i then have a memoryleak, dont i?
Who deletes the widget, once it is closed?
This doesnt:

void MyWidget::CancelClick()

And there is another really wired problem:
When i set the parent to mainwindow

MyWidget *c=new Mywidget(this);

the widget doesnt show properly. It doesnt become a window of its own, but is somehow fixed to mainwindow. Also, only the controls are shown, the widet doesnt have a background. Really strange.
if i use

MyWidget *c=new Mywidget(0);

it works fine...

5th September 2012, 12:23
If you give a widget a parent, then it wont be its own window - it will be 'inside' the parent.

If you wish the widget to be destroyed on close, then you need to set the WA_deleteonclose widget attribute.