E
E
Elvis2017-08-09 23:39:19
Python
Elvis, 2017-08-09 23:39:19

Why are widgets not being added in the first tab?

Hey!
Why is a tab added after the first click on "++++" but no content? And if you click again, a second tab will be added already with the content.
Do not look at the form itself, I pulled it out just for an example, removed everything that was not needed. The essence at start is clear.

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('Teest!')
        self.setMaximumSize(QtCore.QSize(560, 620))
        self.setMinimumSize(QtCore.QSize(560, 620))

        self.Vlayout = QtWidgets.QVBoxLayout(self)
        self.H2layout = QtWidgets.QHBoxLayout()
        self.tabWidgets = QtWidgets.QTabWidget(self)
        self.tabWidgets.setGeometry(QtCore.QRect(10, 170, 541, 351))
        self.H2layout.addWidget(self.tabWidgets)
        self.Vlayout.addLayout(self.H2layout)

        self.H3layout = QtWidgets.QHBoxLayout()
        self.frame = QtWidgets.QFrame(self)
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.H3layout.addWidget(self.frame)
        self.Vlayout.addLayout(self.H3layout)

        self.buttonAddProject = QtWidgets.QPushButton(self.frame)
        self.buttonAddProject.setGeometry(QtCore.QRect(266, 5, 131, 23))
        self.buttonAddProject.setText('+++++++++')
        self.buttonAddProject.clicked.connect(self.add_project_tab)

        self.Vlayout.setStretch(0, 3)
        self.Vlayout.setStretch(1, 6)

    def add_project_tab(self):
        self.tab = QtWidgets.QWidget()
        self.tabWidgets.addTab(self.tab, '+1')
        self.groupBox_2 = QtWidgets.QGroupBox(self.tab)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 0, 511, 101))
        self.horizontalLayoutWidget_3 = QtWidgets.QWidget(self.groupBox_2)
        self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(10, 50, 491, 41))
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_3)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.txtLogin = QtWidgets.QLabel(self.horizontalLayoutWidget_3)
        self.horizontalLayout_3.addWidget(self.txtLogin)
        self.txtLogin.setText('Логин')
        self.tabWidgets.setCurrentIndex(self.tabWidgets.indexOf(self.tab))

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JaxxDexx, 2017-08-10
@Dr_Elvis

self.tabWidgets.addTab(tab, '+1')
move to the end before
self.tabWidgets.setCurrentIndex(self.tabWidgets.indexOf(self.tab))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question