0

There is a class MainWindow, which has a bar with tabs. One of the tabs create FactorsTab class, located in FactorsTabWidget.py file.

at startup crashes:

line 13, in init self.table = QtGui.QStandardItemModel(parent=self.frame_factors) AttributeError: 'NoneType' object has no attribute 'QStandardItemModel'

line 13 in file FactorsTabWidget.py: self.table = > QtGui.QStandardItemModel(parent=self.frame_factors)

How to generate the contents of a tab from another class?

file main.py

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        factors_tab = FactorsTabWidget.FactorsTab()
        self.tabs = QtWidgets.QTabWidget()
        self.tabs.addTab(factors_tab, "Factors")        

file FactorsTabWidget.py

class FactorsTab(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self, parent=None)
        self.frame_factors = QtWidgets.QTableView()
        self.table = QtGui.QStandardItemModel(parent=self.frame_factors)
        self.lst1 = ['Дискета', 'Бумага для принтера', 'Барабан для принтера']
        self.lst2 = ["10", "3", "10452048"]
        for row in range(0, 3):
            item1 = QtGui.QStandardItem(self.lst1[row])
            item2 = QtGui.QStandardItem(self.lst2[row])
            self.table.appendRow([item1, item2])
        self.table.setHorizontalHeaderLabels(['Factor Name', 'Кол-во'])
        self.frame_factors.setModel(self.table)
1
  • message means you have QtGui == None , maybe you set QtGui = None or QtGui = something in some place.
    – furas
    Commented Jan 10, 2017 at 14:52

1 Answer 1

1
  1. I have changed parent init in class created fo make tab 'code from QtWidgets.QTableView.__init__(self, parent=None) toQtWidgets.QWidget.__init__(self, parent=None)'

  2. in string QtGui.QStandardItemModel change parameters from (parent=self.frame_factors) to (0, 2)

everything need to create a table in Tab is create an instance of the widget

work solution:

file main.py

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.factors_tab = FactorsTab()
        self.table_tab = TableTab()        
        self.box = QtWidgets.QVBoxLayout()
        self.tabs = QtWidgets.QTabWidget()
        self.tabs.addTab(self.factors_tab, "Factors")
        self.tabs.addTab(self.table_tab, "Table of coding factors")        
        self.setCentralWidget(self.tabs)
        self.tabs.setElideMode(QtCore.Qt.ElideLeft)
        self.tabs.setCurrentIndex(0)
        self.tabs.setUsesScrollButtons(True)
        self.tabs.setLayout(self.box)
        self.add_menu()

file FactorsTabWidget.py

class FactorsTab(QtWidgets.QTableView):
    def __init__(self):
        QtWidgets.QTableView.__init__(self, parent=None)
        self.label = QtWidgets.QLabel("Select folder, project file name")
        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.addWidget(self.label)
        self.frame_factors = QtWidgets.QTableView()
        self.table = QtGui.QStandardItemModel(0, 2)
        self.lst1 = ['it1', 'it2', 'it3']
        self.lst2 = ["10", "3", "10452048"]
        for row in range(0, 3):
            item1 = QtGui.QStandardItem(self.lst1[row])
            item2 = QtGui.QStandardItem(self.lst2[row])
            self.table.appendRow([item1, item2])
        self.table.setHorizontalHeaderLabels(['Factor Name', 'Num'])
        self.frame_factors.setModel(self.table)
        self.vbox.addWidget(self.frame_factors)
        self.setLayout(self.vbox)
4
  • Explain your solution please
    – eyllanesc
    Commented Jan 13, 2017 at 7:13
  • 1. I have changed parent init in class created fo make tab 'code from QtWidgets.QTableView.__init__(self, parent=None) toQtWidgets.QWidget.__init__(self, parent=None)' 2. in string 'QtGui.QStandardItemModel(0, 2)' change parameters Commented Jan 13, 2017 at 11:07
  • Edit your answer with what you wrote, please.
    – eyllanesc
    Commented Jan 13, 2017 at 18:40
  • added more details Commented Jan 14, 2017 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.