Results 1 to 3 of 3

Thread: A stack of QWidget

  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default A stack of QWidget

    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.

    Qt Code:
    1. void MyMainWindow::pushWidget(StackableWidget *widget)
    2. {
    3. StackableWidget *sw = dynamic_cast<StackableWidget *>(this->centralWidget());
    4. m_stack.prepend(sw);
    5. sw->hide();
    6. this->setCentralWidget(widget);
    7. QObject::connect(widget, SIGNAL(closed()), this, SLOT(childClosed()));
    8. }
    9.  
    10. void MyMainWindow::childClosed()
    11. {
    12. StackableWidget *current = dynamic_cast<StackableWidget *>(this->centralWidget());
    13. current->hide();
    14. StackableWidget *old = m_stack.takeFirst();
    15. this->setCentralWidget(old);
    16. delete current;
    17. old->updateData();
    18. }
    To copy to clipboard, switch view to plain text mode 

    I'm pretty a noob with Qt, so please be gentle. Thanks in advance for your help!
    "It is easier to change the specification to fit the program than vice versa." — Alan Perlis

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A stack of QWidget

    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.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    _Mithrandir_ (23rd May 2012)

  4. #3
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A stack of QWidget

    Quote Originally Posted by amleto View Post
    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.
    "It is easier to change the specification to fit the program than vice versa." — Alan Perlis

Similar Threads

  1. Stack object on top of one other
    By Markus_AC in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2011, 11:05
  2. QObject in stack...??
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 30th October 2007, 08:17
  3. stack, heap and C#
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th August 2007, 18:40
  4. stack
    By mickey in forum General Programming
    Replies: 7
    Last Post: 20th November 2006, 14:15
  5. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 12:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.