2009-01-19 66 views
11

我使用QTabWidget在窗口中呈现多个文档,并且我想在每个选项卡上绘制一个关闭按钮。我正在使用VistaQt4,因此选项卡窗口小部件是本地Windows控件;这可能会影响可行性。在QTabWidget上放置一个关闭按钮

有谁知道是否有可能使用QTabWidget控件执行此操作,还是必须创建自定义小部件?如果创建一个新的小部件是唯一的选择,任何指针将非常赞赏;我对Qt比较陌生。

回答

6

目前无法通过股票QTabWidget进行此操作,但即将推出的Qt 4.5(计划于2009年3月发布)将通过手动或设置QTabBar.TabsClosable属性来选择ability to add close buttons

在此之前,获取关闭按钮的唯一方法是子类QTabWidgetQTabBar并手动添加(可能,但不是微不足道)。

10

在4.5有功能

void setTabsClosable (bool closeable) 
11

由于Qt的4.5。如果您拨打QTabWidget拨打setTabsClosable(true),您将拥有关闭按钮,但他们不会被绑定到某个操作。
如果您想要按钮执行某些操作,您必须将tabClos​​eRequested(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; 
}