关闭按钮仅适用于 Qt 中的某些选项卡
我正在使用 Qt 来做大学作业,我想使用 QTabWidget
来显示一个聊天窗口,就像 洋泾浜语。我想让“群聊”选项卡始终打开且无法关闭,而其余“私人频道”选项卡可关闭。
QTabWidget
的 setTabsClosable(bool)
没有帮助。
有什么想法吗?
I am using Qt for an assignment I have for college, and I want to use QTabWidget
to display a chat window much like Pidgin's. I want to make the "group chat" tab always open and impossible to close and the rest of the "private channel" tabs closable.
QTabWidget
's setTabsClosable(bool)
is not helping.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我想我找到了一个更简单的解决方案。
只需访问相关的关闭按钮并调整其大小即可。
tabWidget->tabBar()->tabButton(0, QTabBar::RightSide)->resize(0, 0);
I found an easier solution, I think.
Simply access the relevant close button and resize it.
tabWidget->tabBar()->tabButton(0, QTabBar::RightSide)->resize(0, 0);
找到该栏(它是私有的,因此使用 findChild())并删除按钮。文档声称关闭按钮也可以放置在左侧。
Find the bar (it is private, so use findChild()) and remove the buttons. Documentation claims that close buttons may be placed on left side too.
你好,
我想这篇文章不会对这个帖子的作者有帮助,但也许其他人会在这里徘徊。
在大多数情况下,不可关闭的选项卡不仅应该忽略关闭符号,而且也不应该在其角落显示关闭符号。实现此目的的一个好方法是修改 QTabWidget 内部的 QTabBar。
例子:
Hallo,
I guess this post won't help the author of this thread but perhaps someone else wanders over here.
In most cases a non-closable tab should not only ignore the closevent it also should not show a close symbol in its corner. A nice way to reach this is to modify the QTabBar which is inside the QTabWidget.
Example:
尽管 tabBar() 确实受到保护,但 Klaus 指出了正确的方向。只需子类化 QTabWidget 并实现包装方法即可。
Although tabBar() is indeed protected, Klaus pointed into the right direction. Simply subclass QTabWidget and implement a wrapper method.
您应该重新实现您的小部件的 event(Event *e) 方法,检查 e 的类型,找出 CloseEvents,并在您可以允许选项卡关闭时调用父类的事件,或者在您不希望时调用 e->ignore() 。
注意,那么你必须父级的 event() 处理其他事件,所以不要accept()、reject() 或忘记它们 Ж)
You should reimplement your widget's event(Event *e) method, check the type of e, find out CloseEvents, and call parent class's event when you can allow tab to close, or e->ignore() when you do not want it.
Note, then you must parent's event() handle othr events, so do not accept(), reject() or forget them Ж)
我想您可以处理 tabCloseRequest 信号并决定是否关闭给定选项卡
http://doc.qt.io/archives/4.6/qtabwidget.html#tabCloseRequested
编辑:我创建了一个小示例来检查它。我的示例是一个简单的 QtGui 应用程序,其主窗口有一个选项卡小部件。然后我添加了 tabCloseRequested 插槽。以下是此示例中的代码,
仅关闭不适用条件的选项卡。
I guess you can handle the tabCloseRequest signal and decide whether u'll close a given tab or not
http://doc.qt.io/archives/4.6/qtabwidget.html#tabCloseRequested
Edit: I created a small example to check it out. My example is a simple QtGui application with a mainwindow that has a tabwidget. I then added the tabCloseRequested slot. Here is the code
From this example only tabs where the condition doesn't apply will be closed.
将按钮添加到某些选项卡而不是其他选项卡的最佳方法是定义 QTabWidget 的子类,以获取受保护的 QTabBar!
下面的代码经过测试并且有效:
要使用此子类并创建带有自定义按钮的新选项卡,您必须遵循此说明( ui->tabWidget 是带有 setClosableTabs=false 的 QTabWidget ):
The best way for adding a pushbutton to some tabs and not in other is to define a subclass of QTabWidget for taking the QTabBar that is a potected!
The code below is tested and it works:
For using this subclass and create a new tab with a custom button you have to following this instructions ( ui->tabWidget is a QTabWidget with setClosableTabs=false):
不确定,为什么这里没有人提到最简单的工作解决方案:
这完全消除了关闭按钮及其占用的空间。
文档
Not sure, why nobody here mentioned the simplest working solution:
This completely removes the close button, and the space taken by it.
Documentation