开发者

Qt splitter disable

开发者 https://www.devze.com 2022-12-09 03:17 出处:网络
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 t

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 t开发者_运维问答he 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?


Do this:

for (int i = 0; i < splitter->count(); i++)
{
    QSplitterHandle *hndl = splitter->handle(i);
    hndl->setEnabled(false);
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消