Results 1 to 3 of 3

Thread: Exchanging a widget in a four way split with QSplitter

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Exchanging a widget in a four way split with QSplitter

    I used QSplitters in a layout to split up my screen in four parts like so:

    sshot.jpg

    The horizontal splitter "contains" two vertical splitters

    Qt Code:
    1. verticalSplitterTop = new QSplitter();
    2. verticalSplitterTop->addWidget(&configWidget);
    3. verticalSplitterTop->addWidget(&landscapeWidget);
    4.  
    5. verticalSplitterBottom = new QSplitter(ui.centralWidget);
    6. verticalSplitterBottom->addWidget(&checkboxWidget);
    7. verticalSplitterBottom->addWidget(&graphWidget);
    8.  
    9. horizontalSplitter = new QSplitter(Qt::Vertical);
    10. horizontalSplitter->addWidget(verticalSplitterTop);
    11. horizontalSplitter->addWidget(verticalSplitterBottom);
    12.  
    13. centralLayout->addWidget(horizontalSplitter);
    To copy to clipboard, switch view to plain text mode 


    and the vertical splitters are synchronized so that if one is dragged, the other one moves the same way.

    Qt Code:
    1. connect(verticalSplitterTop, SIGNAL(splitterMoved(int, int)), this, SLOT(topSplitterMoved()));
    2. connect(verticalSplitterBottom, SIGNAL(splitterMoved(int, int)), this, SLOT(bottomSplitterMoved()));
    3.  
    4. // Vertical splitter synchronization.
    5. void CaptureStepFramework::topSplitterMoved()
    6. {
    7. verticalSplitterBottom->setSizes(verticalSplitterTop->sizes());
    8. }
    9. void CaptureStepFramework::bottomSplitterMoved()
    10. {
    11. verticalSplitterTop->setSizes(verticalSplitterBottom->sizes());
    12. }
    To copy to clipboard, switch view to plain text mode 

    This is to make the splitters behave like a cross. It does the job but it doesn't always look pretty. Anyhow. Now I would like to exchange the widget in the top right corner by the press of a button, and since I can't find a way to remove a widget from a splitter and to replace it with another one, I see myself forced to reconstruct the whole cross.

    Qt Code:
    1. void CaptureStepFramework::toggleMainWidget()
    2. {
    3. bool w = false;
    4. if (verticalSplitterTop->widget(1) == &limpWidget)
    5. w = true;
    6.  
    7. verticalSplitterTop = new QSplitter();
    8. verticalSplitterTop->addWidget(&configWidget);
    9. if (w)
    10. verticalSplitterTop->addWidget(&landscapeWidget);
    11. else
    12. verticalSplitterTop->addWidget(&limpWidget);
    13. verticalSplitterTop->setSizes(verticalSplitterBottom->sizes());
    14.  
    15. QLayout* centralLayout = ui.centralWidget->layout();
    16. centralLayout->removeWidget(horizontalSplitter);
    17.  
    18. horizontalSplitter = new QSplitter(Qt::Vertical, ui.centralWidget);
    19. horizontalSplitter->addWidget(verticalSplitterTop);
    20. horizontalSplitter->addWidget(verticalSplitterBottom);
    21. centralLayout->addWidget(horizontalSplitter);
    22.  
    23. connect(verticalSplitterTop, SIGNAL(splitterMoved(int, int)), this, SLOT(topSplitterMoved()));
    24. }
    To copy to clipboard, switch view to plain text mode 


    This also does the job, but the result is ugly. The top left widget disappears for a moment. I also don't think that this code cleans up properly, but my experiments with deleteLater() or just delete things were fatal. Can anyone help me with doing this properly?

    Thanks
    Cruz

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Exchanging a widget in a four way split with QSplitter

    I see two options:

    1) if you don't need to destroy the two widgets that are displayed in the top right cell, use a QStackedWidget to which you add both. Then use setCurrentIndex() or setCurrentWidget() for toggling

    2) if you need to delete the widget that is currently not displayed, add an empty QWidget to the "grid" and have the current widget as its only child.

    Cheers,
    _

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

    Cruz (9th February 2014)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Exchanging a widget in a four way split with QSplitter

    QStackedWidget works like a charm.

Similar Threads

  1. Replies: 7
    Last Post: 6th February 2017, 19:10
  2. Replies: 5
    Last Post: 20th February 2012, 08:59
  3. Replies: 4
    Last Post: 10th January 2012, 12:13
  4. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16
  5. advanced split widget
    By vitaly in forum Qt Programming
    Replies: 10
    Last Post: 24th January 2006, 20:00

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.