[Qt-interest] Fwd: QMainWindow centralWidget issue..

Andre Somers andre at familiesomers.nl
Fri Jan 29 11:35:58 CET 2010


Nikos Gerontidis wrote:
>
>
> ---------- Forwarded message ----------
> From: *Andre Somers* <andre at familiesomers.nl 
> <mailto:andre at familiesomers.nl>>
> Date: Fri, Jan 29, 2010 at 11:56 AM
> Subject: Re: [Qt-interest] QMainWindow centralWidget issue..
> To: Qt-interest <qt-interest at trolltech.com 
> <mailto:qt-interest at trolltech.com>>
>
>
> Nikos Gerontidis wrote:
> > Hi list,
> >
> > I create 2 widgets and /append/ them in a /QList/ object,
> How? I hope you're creating them on the heap, right? Not on the stack?
> > which is a member of
> > my /CustomMainWindow/ class. While in run time, I set the central widget
> > with the /setCentralWidget(QWidget*)/ function. My application crashes
> > when I
> > set twice the same widget as a central widget.
> >
> > Is it a problem related to the fact that QMainWindow takes ownership
> > of the widget, when this widget is being set as a central widget.
> > Any ideas? Thanks in advance.
> Are you sure your widget still exists when you set it again? Put a
> breakpoint in the destructor of your widget, or generate some debut
> output there to check.
>
> The question is of course, why are you changing what widget is the
> central widget in the first place? Would a QStackedWidget not be a
> better candidate to manage what's visisble in your application?
>
> André
>
>
> Thank you for your instant responses..
> Before setting a new widget in the /on_setNewWidget /function
> I check if pWidget is NULL with
> /QWidget *pWidget = listOfCentralWidgets.at(0); // this is the list 
> containg the cetral/ widgets
> /if (pWidget)
>     setCentralWidget(pointerToAnotherWidget);/
> the if statement passes normally and then the application crashes..
A pointer not being 0, does not mean that the pointer is still valid. It 
may have been deleted behind your back. If you want that functionality, 
use a QPointer to wrap your raw pointer. Note that this only works for 
QObject derived classes, but lucky for you, QWidget is derived from 
QObject. A QPointer will be reset to 0 if the object is deleted behind 
your back.
> It is strange at the moment. I ll debug it and check it better.
>
> I will use the QStackedWidget, which I believe is a solution to my issue.
Even better. :-)

André




More information about the Qt-interest-old mailing list