Results 1 to 3 of 3

Thread: Removing window from QWorkspace

  1. #1
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Removing window from QWorkspace

    Short version ...

    I need to prevent Qt from deleting a window that was added to a QWorkspace via QWorkspace::addWindow().

    Long version ...

    Qt Code:
    1. class cPlatView : public QDialog, public Ui::cPlatView, public cView
    2. {
    3. // ...
    4. };
    5.  
    6. // ...
    7. boost::shared_ptr<cPlatView> somePlatView;
    8.  
    9. // ...
    10. mMainWindow->addView(somePlatView.get());
    To copy to clipboard, switch view to plain text mode 

    I'm using an MVC architecture where the controller manages the lifecycle of the model and the view (view being a window in the instance). The controller code (a non-Qt library) uses Boost::shared_ptr to manage memory of the model and view instances. Since the Qt addWindow call handles a naked pointer (retreived from controller by dereferencing Boost::shared pointer), Boost doesn't know that Qt has a reference to the view. So, during shutdown, the view is deleted twice ... once by Boost::shared_ptr and once by Qt.

    To prevent double-delete ... how do I tell Qt not to delete memory for the view? Do I remove the window for the QWorkspace? Or do I call view::setParent(0)?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Removing window from QWorkspace

    The easiest way is to just increment the reference count on the pointer so that boost doesn't auto-delete it. You can't remove the parent pointer from the object, because the workspace would probably lose control over the window, you can try by removing the window from QWorkspace and then clearing the parent (although I don't know if such thing as "removing from workspace" exists at all - you'd have to remove the view object from workspace child window by reparenting the view, then delete the workspace child and hope that everything else works). But surely incrementing the reference count is the proper solution

  3. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Removing window from QWorkspace

    I didn't increment the boost::shared_ptr. However, your suggestion got me thinking. I decided to use the Bridge pattern to separate the MVC's view and the Qt's view. It was the permanent binding (via inheriting from cView) that was causing the problem.

    Now the view bridge handles the memory management issues between Qt and Boost. Inheritance isn't always evil as some claim, but it was in this case.

    Thanks for you input.

Similar Threads

  1. Replies: 3
    Last Post: 23rd July 2006, 18:02
  2. MDI windows without QWorkspace
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 17:15
  3. Retrieving offsets of scroll in QWorkspace
    By hardgeus in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 02:25
  4. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 10:21
  5. Main window
    By Michiel in forum Qt Tools
    Replies: 1
    Last Post: 20th March 2006, 23:54

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
  •  
Qt is a trademark of The Qt Company.