Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - How to build a multi-tab window?

Tags:

qt

I want to build a single-window application which has two tabs. How can I set two tabs on the window and click to switch, just like the browser's window and tabs ?

PS: The two tabs have different layout of buttons and text-widgets and functions differently.

like image 897
CDT Avatar asked May 23 '13 02:05

CDT


1 Answers

http://doc.qt.io/qt-5/qtabwidget.html#details

The QTabWidget class provides a stack of tabbed widgets.

A tab widget provides a tab bar (see QTabBar) and a "page area" that is used to display pages related to each tab. By default, the tab bar is shown above the page area, but different configurations are available (see TabPosition). Each tab is associated with a different widget (called a page). Only the current page is shown in the page area; all the other pages are hidden. The user can show a different page by clicking on its tab or by pressing its Alt+letter shortcut if it has one.

The normal way to use QTabWidget is to do the following:

  1. Create a QTabWidget.
  2. Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
  3. Insert child widgets into the page widget, using layouts to position them as normal.
  4. Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.

The position of the tabs is defined by tabPosition, their shape by tabShape.

...

And there is your answer.

EDIT: Link to an example, too.

http://doc.qt.io/qt-5/qtwidgets-dialogs-tabdialog-example.html

Update: In addition to all the advantages that come with using the QTabWidget, some additional functionality of tabs come with the QDockWidget, like tearing them off into a separate window. I've used QDockWidgets recently to get nearly the same appearance of tabs, but the baked in tear-off, and the right click for a checked listing of tabs.

Hope that helps.

like image 113
phyatt Avatar answered Sep 21 '22 15:09

phyatt