Results 1 to 2 of 2

Thread: How can I dock widgets between two windows?

  1. #1
    Join Date
    Nov 2014
    Posts
    35
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How can I dock widgets between two windows?

    Hello,

    I've been trying to add a second main window with docking functionality to my application, but I can't figure out how to do it.

    This is what I'm trying to achieve:

    The application starts with a QMainWindow with multiple widgets docked in the main window.

    00.jpg

    I can drag a widget outside of the main window and it gets docked to what looks like a newly created second QMainWindow.

    01.jpg

    I can drag any other widget from the first QMainWindow and dock it into the second QMainWindow.

    02.jpg

    (These images are from Natron, a Qt4 open source compositing application)

    I've tried this using two QMainWindow instances, but I can't dock from the first to the second window.

    Any tips or suggestions.

    Thank you!
    pixaeiro
    http://www.pixaflux.com
    Non-destructive Image Editor

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I dock widgets between two windows?

    QDockWidget creates a floating top-level window when it is undocked and dragged outside the QMainWindow., This is not another QMainWindow, it is simply a dock widget with a title bar. If the "widgets" you say you can drag into the floating dock widget are also dock widgets, then your QMainWindow has been configured to allow dock nesting (QMainWindow::setDockNestingEnabled()), which allows the dock widget window to contain more than one dock widget side-by-side or top-to-bottom. You can probably also do this without the dock widget floating (ie. while still docked), but you'd have to test that.

    You can't easily have two QMainWindow instances in the same app. It gets pretty confusing. Instead, you might try using a QMdiArea as the central widget in your single QMainWindow, with multiple QMdiSubWindow instances inside the QMdiArea.

    QMdiSubWindow is somewhat like a QDockWidget except that it is always floating and can't be moved outside the bounds of the QMainWindow. A common use for QMdiSubWindow is in an editor application where you can have more than one document open for editing at a time. Each document lives in its own QMdiSubWindow, and your QMainWindow needs to know which sub window is active (QMdiArea::currentSubWindow()) in order to know which document to apply menu operations to.

    I've tried this using two QMainWindow instances, but I can't dock from the first to the second window.
    The dock widget is owned by whichever QMainWindow it was created for, so it will only be recognized by its own main window when you want to dock it again. It might work if you call QObject::setParent() either to nullptr (which makes it an independent top-level widget that can't be docked anywhere if it isn't destroyed in the process) or to the pointer to the second QMainWindow instance (if you want to transfer it to the second window). That would probably be some tricky drag and drop logic.

    You -can- have two QMainWindow instances; you just need to create and show() them both in main(). Be sure you call QGuiApplication::setQuitOnLastWindowClosed() with "true" to make sure closing one main window doesn't accidentally close them both.
    Last edited by d_stranz; 9th December 2020 at 02:15.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Dock Widgets Example Question
    By webphone in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2011, 02:32
  2. resizeEvent for Dock Widgets
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2010, 12:30
  3. forcing dock windows to dock?
    By eric_vi in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2009, 16:32
  4. Evaluation Example Dock Widgets Hangs Windows
    By inflector in forum Qt Programming
    Replies: 6
    Last Post: 30th December 2006, 20:03
  5. Dock widgets PROBLEM
    By L1L0 in forum Qt Programming
    Replies: 8
    Last Post: 7th April 2006, 19:29

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.