Elvis
Elvis

Reputation: 73

How to add a tab to QTabWidget using the button on the form?

I decided to write a visual form for my script. The idea is to have a button that will add new tabs to QTabWidget. It does not work and I can not find a good example. I use PyQt5. Here's a piece of what I've tried:

import sys
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore, QtWidgets

class mainForm(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.runUi()

    def runUi(self):
        self.resize(250, 150)
        self.move(300, 300)
        self.setWindowTitle('Let\'s Rock!')
        self.setWindowIcon(QIcon('icon.png'))
        self.setMaximumSize(QtCore.QSize(560, 522))
        self.setMinimumSize(QtCore.QSize(560, 522))

        groupBoxGD = QtWidgets.QGroupBox('Соединение с ГД', self)
        groupBoxGD.setGeometry(QtCore.QRect(10, 10, 541, 151))

        hrLWGDLink = QtWidgets.QWidget(groupBoxGD)
        hrLWGDLink.setGeometry(QtCore.QRect(10, 10, 521, 31))
        hrLGD = QtWidgets.QHBoxLayout(hrLWGDLink)
        hrLGD.setContentsMargins(0, 0, 0, 0)

        btnAddTab = QtWidgets.QPushButton(hrLWGDLink)
        btnAddTab.setText('Add tab')
        hrLGD.addWidget(btnAddTab)

        tabWidget = QtWidgets.QTabWidget(groupBoxGD)
        tabWidget.setGeometry(QtCore.QRect(10, 170, 541, 351))
        btnAddTab.clicked.connect(self.addProjectTab)
        self.show()

    def addProjectTab(self):
        tab = QtWidgets.QWidget()
        #how add tab at this line?

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = mainForm()
    sys.exit(app.exec_())

Upvotes: 1

Views: 6204

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

You have to use the addTab() function, but to do so from another class the QTabWidget object must be a member of the class. Also I made some changes in the design because the button was on the QTabWidget, covering the tabs.

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class mainForm(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.runUi()

    def runUi(self):
        self.resize(250, 150)
        self.move(300, 300)
        self.setWindowTitle('Let\'s Rock!')
        self.setWindowIcon(QtGui.QIcon('icon.png'))
        self.setMaximumSize(QtCore.QSize(560, 522))
        self.setMinimumSize(QtCore.QSize(560, 522))

        layout = QtWidgets.QVBoxLayout(self)

        groupBoxGD = QtWidgets.QGroupBox('Соединение с ГД', self)

        layout2 = QtWidgets.QVBoxLayout(groupBoxGD)

        hrLWGDLink = QtWidgets.QWidget(groupBoxGD)
        hrLGD = QtWidgets.QVBoxLayout(hrLWGDLink)
        hrLGD.setContentsMargins(0, 0, 0, 0)
        btnAddTab = QtWidgets.QPushButton(hrLWGDLink)
        btnAddTab.setText('Add tab')

        hrLGD.addWidget(btnAddTab)
        self.tabWidget = QtWidgets.QTabWidget(hrLWGDLink)
        hrLGD.addWidget(self.tabWidget)
        layout2.addWidget(hrLWGDLink)
        layout.addWidget(groupBoxGD)
        btnAddTab.clicked.connect(self.addProjectTab)

    def addProjectTab(self):
        tab = QtWidgets.QWidget()
        self.tabWidget.addTab(tab, "tab")

app = QtWidgets.QApplication(sys.argv)
w = mainForm()
w.show()
sys.exit(app.exec_())

Screenshot:

enter image description here

Upvotes: 1

Related Questions