Tabs allow you to condense the information from several screens into a relatively small place. Tabs provide titles at the top of the window, which present an individual widget layout for each title when clicked. In this recipe, we'll create a simple tabbed interface.
Open the QGIS Python Console by selecting the Plugins menu and then clicking on Python Console.
We will create an overarching tab widget. Then, we'll create three generic widgets to represent our tabs. We'll set up layouts with three different GUI widgets and assign each layout to our tab widgets. Finally, we'll add our tabs to the tab widget and display it. To do this, we need to perform the following steps:
from PyQt4.QtCore import * from PyQt4.QtGui import *
qtw = QTabWidget() qtw.setWindowTitle("PyQGIS Tab Example") qtw.resize(400,300)
tab1 = QWidget() tab2 = QWidget() tab3 = QWidget()
layout1 = QVBoxLayout() layout1.addWidget(QTextEdit("<b>Type text here</b>")) tab1.setLayout(layout1)
layout2 = QVBoxLayout() layout2.addWidget(QPushButton("Button")) tab2.setLayout(layout2)
layout3 = QVBoxLayout() layout3.addWidget(QLabel("Label text example")) tab3.setLayout(layout3)
qtw.addTab(tab1, "First Tab") qtw.addTab(tab2, "Second Tab") qtw.addTab(tab3, "Third Tab")
qtw.show()
The key to this recipe is the QTabWidget()
.method. Everything else is just arbitrary layouts and widgets, which are ultimately contained in the tab widget.
There is no way to predict how the user will interact with a tabbed interface, and if the information across tabs is dependent, problems will arise.