PDA

View Full Version : A stack of QWidget



_Mithrandir_
22nd May 2012, 09:52
Hi all,
I need to build an application in which several forms open one over the other. To avoid having many floating windows cluttering up the user's screen, I have thought of realizing a dynamic stack of QWidget(s) (derived from a custom abstract class StackableWidget).

Unfortunately this solution isn't working. When I try to retrieve the old QWidget from the stack I get an invalid reference. In the watch windows of QtCreator it says <unavailable synchronous data>, but I'm not (explicitly) using threads.


void MyMainWindow::pushWidget(StackableWidget *widget)
{
StackableWidget *sw = dynamic_cast<StackableWidget *>(this->centralWidget());
m_stack.prepend(sw);
sw->hide();
this->setCentralWidget(widget);
QObject::connect(widget, SIGNAL(closed()), this, SLOT(childClosed()));
}

void MyMainWindow::childClosed()
{
StackableWidget *current = dynamic_cast<StackableWidget *>(this->centralWidget());
current->hide();
StackableWidget *old = m_stack.takeFirst();
this->setCentralWidget(old);
delete current;
old->updateData();
}

I'm pretty a noob with Qt, so please be gentle. Thanks in advance for your help!

amleto
22nd May 2012, 10:50
it's pretty bad to 'delete ptr' in Qt a lot of the time. You should normally use ptr->deleteLater().

Anyhow, what you're doing looks a bit funny. Why not just use QStackedWidget http://qt-project.org/doc/qt-4.8/qstackedwidget.html ? If this is your centrl widget, then you dont need to mess around with ANOTHER m_stack.

_Mithrandir_
23rd May 2012, 17:00
it's pretty bad to 'delete ptr' in Qt a lot of the time. You should normally use ptr->deleteLater().

Anyhow, what you're doing looks a bit funny. Why not just use QStackedWidget http://qt-project.org/doc/qt-4.8/qstackedwidget.html ? If this is your centrl widget, then you dont need to mess around with ANOTHER m_stack.
Thank you. QStackedWidget does just what I need. I wonder how I missed it in the first place.