QMdiArea tabs with close button

If you want to build an application with multiple document interface and tabs, you don’t have the close buttons in the tabs by default. That’s why I’ve come up with a workaround to get those buttons in every tab. Here’s a code example:

  1. Q_FOREACH (QTabBar* tab, ui>mdiArea>findChildren())
  2. {
  3.    tab>setTabsClosable(true);
  4.    connect(tab, SIGNAL(tabCloseRequested(int)),
  5.                this, SLOT(closeTab(int)));
  6. }
  7.  
  8. void MainWindow::closeTab(int i)
  9. {
  10.    QMdiSubWindow *sub = ui>mdiArea>subWindowList()[i];
  11.    QWidget *win = sub>widget();
  12.    win>close();
  13.    ui>mdiArea>setActiveSubWindow(sub);
  14.    ui>mdiArea>closeActiveSubWindow();
  15. }
Share and Enjoy:
  • Facebook
  • del.icio.us
  • MySpace
  • RSS
  • LinkedIn
  • PDF
  • Print

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.