tags:

views:

277

answers:

2

Hi,

I want to be able to stop a user from moving a QSplitter at runtime. Calling setEnabled(false) does this, but it also disables all child widgets - which isn't what I want. Is there a way to achieve this? Do I have to disable the splitter, and then manually re-enable all child widgets? That seems rather cumbersome, for something that must be a reasonably common practise.

Can anyone suggest anything?

+2  A: 

Actually, I've never seen anyone ever disable a splitter: They are there so the user can layout the UI as she needs, so why would anyone want to disable this? Either you need a splitter or you can use one of the normal layouts (which the user can't resize).

If you still want to try, I think you should look at closestLegalPosition() or getRange(). If you just return the width of the widget, then resizing should stop working.

Aaron Digulla
I guess that's going to be the easiest way - the use case is this: I Have a ui, which can be put into a certain mode, in which one of the splitter's child widgets disappears. In that case, I don't want the user to be able to move the splitter, since there's only a widget on one side - does that make sense?
Thomi
Odd. I'd guess that the splitter wouldn't offer a drag element when it has only a single child. Can you check that there is really only a single child?
Aaron Digulla
Well, you can't actually remove a widget from the splitter - the QSplitter public API does not provide a method to do this. The best you can achieve is to hide the children. Any ideas?
Thomi
Report a bug against Qt. It should be possible to change the number of children for a QSplitter. As a workaround, I'd try to hide the whole splitter replace it with a normal layout but that means you'll have to duplicate one child :/
Aaron Digulla
A: 

You have to do two things. Set the widgets (that shouldn't be resizeable) inside the splitter to FixedSize and change the cursor of the correspondent splitter handles to Qt::ArrowCursor. The handles start with zero (left and not used), so the first handle between two widgets is by index 1.

Here's a sample (put the code in main.cpp):

#include <QtGui>

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QWidget window;
     window.resize(800, 300);
     window.setWindowTitle("Splitter Test");
     window.show();

     QSplitter *splitter = new QSplitter(&window);
     QListView *listview = new QListView;
     QTreeView *treeview = new QTreeView;
     QTextEdit *textedit = new QTextEdit;

     splitter->addWidget(listview);
     splitter->addWidget(treeview);
     splitter->addWidget(textedit);
     splitter->setChildrenCollapsible(false);

     splitter->show();
     listview->show();
     treeview->show();
     textedit->show();

     //make the lisview 'fix'
     listview->setFixedSize(listview->width(), listview->height());
     //change the cursor over the splitter handle between listview and
     //treeview to ArrowCursor
     splitter->handle(1)->setCursor(Qt::ArrowCursor);;

     return app.exec();
 }

Now the first splitter handle is 'disabled' and the second works.

stemuedendron