简体   繁体   English

Qt拆分器禁用

[英]Qt splitter disable

I want to be able to stop a user from moving a QSplitter at runtime. 我希望能够阻止用户在运行时移动QSplitter。 Calling setEnabled(false) does this, but it also disables all child widgets - which isn't what I want. 调用setEnabled(false) ,但它也会禁用所有子窗口小部件 - 这不是我想要的。 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? 谁能提出任何建议?

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? 实际上,我从未见过有人曾经禁用分离器:它们在那里,因此用户可以根据需要布局UI,那么为什么有人想要禁用它呢? 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() . 如果您仍想尝试,我认为您应该查看nearestLegalPosition()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 . 将分割器内的小部件(不应该FixedSize大小)设置为FixedSize ,并将对应的分割器句柄的光标更改为Qt::ArrowCursor The handles start with zero (left and not used), so the first handle between two widgets is by index 1. 句柄从零开始(左侧未使用),因此两个小部件之间的第一个句柄是索引1。

Here's a sample (put the code in main.cpp): 这是一个示例(将代码放在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. 现在第一个拆分器句柄被disabled ,第二个拆分器句柄工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM