windows 在 QTabWidget 上放置关闭按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/459372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Putting a close button on QTabWidget
提问by conmulligan
I'm using a QTabWidget
to render multiple documents in a window, and I want to draw a close button on each tab. I'm using Vistaand Qt4, so the tab widget is a native windows control; this may affect the feasibility.
我正在使用 aQTabWidget
在一个窗口中呈现多个文档,并且我想在每个选项卡上绘制一个关闭按钮。我使用的是Vista和Qt4,因此选项卡小部件是本机 Windows 控件;这可能会影响可行性。
Does anyone know if it is possible to do this using the QTabWidget
control, or do I have to create a custom widget? If creating a new widget is the only option, any pointers would be much appreciated; I'm relatively new to Qt.
有谁知道是否可以使用QTabWidget
控件来做到这一点,或者我是否必须创建一个自定义小部件?如果创建一个新的小部件是唯一的选择,任何指针将不胜感激;我对 Qt 比较陌生。
采纳答案by dF.
Currently there is no way to do this with the stock QTabWidget, however the upcoming Qt 4.5 (planned to be released in March 2009) will have the ability to add close buttonsto tabs either manually or by setting a QTabBar.TabsClosable
property.
目前没有办法使用库存的 QTabWidget 来做到这一点,但是即将推出的 Qt 4.5(计划于 2009 年 3 月发布)将能够手动或通过设置QTabBar.TabsClosable
属性向选项卡添加关闭按钮。
Until then, the only way to get close buttons is to subclass QTabWidget
or QTabBar
and add it manually (possible, but not trivial).
在那之前,获得关闭按钮的唯一方法是子类化QTabWidget
或QTabBar
手动添加它(可能,但并非微不足道)。
回答by Cyril Leroux
Since Qt 4.5. If you just call setTabsClosable(true)
on QTabWidget
, you will have the close buttons but they won't be bound to an action.
You have to connect the tabCloseRequested(int) signal to one of your own slots if you want the buttons to do something.
从 Qt 4.5 开始。如果你只是叫setTabsClosable(true)
上QTabWidget
,你将不得不关闭按钮,但他们不会被绑定到一个动作。
如果您希望按钮执行某些操作,您必须将 tabCloseRequested(int) 信号连接到您自己的插槽之一。
MainWindow::MainWindow()
m_tabs = new QTabWidget();
m_tabs->setTabsClosable(true);
connect(m_tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
void MainWindow::closeTab(const int& index)
{
if (index == -1) {
return;
}
QWidget* tabItem = m_tabs->widget(index);
// Removes the tab at position index from this stack of widgets.
// The page widget itself is not deleted.
m_tabs->removeTab(index);
delete(tabItem);
tabItem = nullptr;
}
回答by Pavels
In 4.5 there is function
在 4.5 中有函数
void setTabsClosable ( bool closeable )