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.
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 (seeTabPosition
). 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:
- Create a
QTabWidget
.- Create a
QWidget
for each of the pages in the tab dialog, but do not specify parent widgets for them.- Insert child widgets into the page widget, using layouts to position them as normal.
- Call
addTab()
orinsertTab()
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 bytabShape
....
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With