Putting a close button on QTabWidget

≯℡__Kan透↙ 提交于 2019-12-10 02:15:45

问题


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 Vista and Qt4, so the tab widget is a native windows control; this may affect the feasibility.

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.


回答1:


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 buttons to tabs either manually or by setting a QTabBar.TabsClosable property.

Until then, the only way to get close buttons is to subclass QTabWidget or QTabBar and add it manually (possible, but not trivial).




回答2:


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.

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;
}



回答3:


In 4.5 there is function

void setTabsClosable ( bool closeable )


来源:https://stackoverflow.com/questions/459372/putting-a-close-button-on-qtabwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!