Results 1 to 5 of 5

Thread: Deleting a Qsplitter

  1. #1
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Deleting a Qsplitter

    Hi, i have a glwidget that renders a cube. I want to have different views of the cube, and that the user can choose how many views it want.
    So if he wants dual view for example:

    gridGroupBox = new QGroupBox(tr("Grid layout"));
    hbox = new QHBoxLayout(this);
    splitter1->addWidget(glWidget);
    splitter1->addWidget(topright);
    hbox->addWidget(splitter1);

    So my question is, how do i remove the splitter to go back to single view?
    I tride hide, doesnt work, delete later doesnt work. It should be something like.

    hbox->removeWidget(splitter);
    hbox->addWidget(glWidget);

    That doesn't work because the splitter is still there!
    Thank u for ur help!
    Elbe

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Deleting a Qsplitter

    If a QSplitter contains only one widget, or only one visible widget, then the splitter handle is not shown. The QSplitter still exists, and can have widgets re-added. Watch this for 10 seconds:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0): QMainWindow(p) {
    8. QSplitter *central = new QSplitter(this);
    9. left = new QLabel("Left", this);
    10. central->addWidget(left);
    11. right = new QLabel("Right", this);
    12. central->addWidget(right);
    13.  
    14. setCentralWidget(central);
    15.  
    16. QTimer::singleShot(5000, this, SLOT(removeRight()));
    17. QTimer::singleShot(10000, this, SLOT(restoreRight()));
    18. }
    19. public slots:
    20. void removeRight() {
    21. qDebug() << "removeRight";
    22. right->hide();
    23. // right->deleteLater(); // if you do not need the widget any more
    24. }
    25. void restoreRight() {
    26. qDebug() << "restoreRight";
    27. right->show();
    28. }
    29. private:
    30. QLabel *left;
    31. QLabel *right;
    32. };
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication app(argc, argv);
    37.  
    38. MainWindow m;
    39. m.show();
    40. return app.exec();
    41. }
    42. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    If you really want the splitter to go away entirely then (in this example):
    Qt Code:
    1. void removeSplitter() {
    2. qDebug() << "removeSplitter";
    3. setCentralWidget(left);
    4. splitter->deleteLater();
    5. right->deleteLater();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 16th May 2011 at 00:32.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting a Qsplitter

    Thank you very much Chris!
    I realized that i was doing splitter.hide() instead of the widgets in the splitter.
    So i have 4 views of my scene topleft,topright, botleft and botright.
    Single view shows topleft. Dual view shows topleft and topright. Quad view shows all 4.

    With your help i can switch now between dual and single view without problem. But when i go into Quad view , Debug shows:
    QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout
    and the execution of my program stops.

    Why would that be? What am i missing?

    Here is part of my code

    //In Constructor
    Qt Code:
    1. glWidget = new GLWidget(1);
    2. topright = new GLWidget(2);
    3. botleft = new GLWidget(3);
    4. botright = new GLWidget(4);
    5.  
    6. old_view=1;
    7. gridGroupBox = new QGroupBox(tr("Grid layout"));
    8. hbox = new QHBoxLayout(this);
    9.  
    10. splitter1 = new QSplitter(Qt::Horizontal, this);
    11. splitter2 = new QSplitter(Qt::Horizontal, this);
    12. splitter3 = new QSplitter(Qt::Vertical, this);
    13.  
    14. splitter1->addWidget(glWidget);
    15. splitter1->addWidget(topright);
    16. topright->hide();
    17. splitter2->addWidget(botleft);
    18. splitter2->addWidget(botright);
    19. botleft->hide();
    20. botright->hide();
    21. splitter3->addWidget(splitter1);
    22. splitter3->addWidget(splitter2);
    23. splitter2->hide();
    24.  
    25. hbox->addWidget(splitter3);
    26. gridGroupBox->setLayout(hbox);
    27. setCentralWidget(gridGroupBox);
    To copy to clipboard, switch view to plain text mode 

    //Slots
    Qt Code:
    1. void MainWindow::setSingleV()
    2. {
    3. if(old_view==2)
    4. {
    5. topright->hide();
    6.  
    7. }else if(old_view==4)
    8. {
    9. topright->hide();
    10. botleft->hide();
    11. botright->hide();
    12. splitter2->hide();
    13. }
    14. old_view=1;
    15. }
    16. void MainWindow::setDualView()
    17. {
    18. if(old_view==1)
    19. {
    20. topright->show();
    21. }else if(old_view==4)
    22. {
    23. botleft->hide();
    24. botright->hide();
    25. splitter2->hide();
    26. }
    27. old_view=2;
    28. }
    29. void MainWindow::setQuadView()
    30. {
    31. if(old_view==1)
    32. {
    33. topright->show();
    34. botleft->show();
    35. botright->show();
    36. splitter2->show();
    37.  
    38. }else if(old_view==2)
    39. {
    40. botleft->show();
    41. botright->show();
    42. splitter2->show();
    43. }
    44. old_view=4;
    45. }
    To copy to clipboard, switch view to plain text mode 

    Thank u very much for ur time!
    Elbe

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Deleting a Qsplitter

    The warning message about layouts comes about because at line 8 of the constructor you create the layout with the QMainWindow as its parent. When you later add the layout to that window it see that there is already a child layout and informs you (not considering that it is the same layout). This is non-fatal and can be avoided by removing "this" from line 8 (and moving it closer to where it is used at 25).

    Nothing else I can see in your code snippets is program life threatening.

    What does "my program stops" mean? Crashes with a backtrace in your debugger? Where is the crash originating?

    On a widget juggling note... You never need to hide the lower widgets individually: just hide their parent splitter and let them inherit visibility.

    Edit:
    Make sure that you are not still using deleteLater() on the widgets or splitters and later trying to dereference an invalid pointer.
    Last edited by ChrisW67; 17th May 2011 at 02:41.

  5. #5
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting a Qsplitter

    Oh Chris it works so lovely.

    I just move what you just said with the layout and voilà !

    Thank you very much. It was nagging on me quite a long time!

    Regards,

    Elberion

Similar Threads

  1. Bug in QSplitter with Qt4.5.1
    By araglin in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2009, 08:45
  2. QSplitter
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2009, 14:46
  3. QSplitter/SplitterMoved
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2008, 09:27
  4. New to QSplitter
    By bruccutler in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2007, 17:43
  5. QSplitter
    By Solarity in forum Newbie
    Replies: 2
    Last Post: 10th February 2006, 18:05

Tags for this Thread

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.