PDA

View Full Version : widget doesnt show



tuli
5th September 2012, 11:14
Hi,

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);
//c.setWindowModality(Qt::WindowModal);
c.show();
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?

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

MyWidget *c=new Mywidget(this);
c->show();

spirit
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).

tuli
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()
{
this->close();
}



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

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

Qt#WidgetAttribute-enum