I\'m new to PyQt.
I\'m trying to put a QTableView in a class, so I can define it\'s behaviour in the class without mixing it with all the other code, but when I do so it
You defined Table
as a QWidget
with an attribute self.view=QTableView
.
But you didn't define a layout on Table
, so it will be displayed as an empty widget.
You either have to define a layout for Table
, and add the view to it, or directly add the view to the main window's layout:
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel = MyTableModel(my_array, self)
table = Table(tablemodel)
layout = QVBoxLayout(self)
layout.addWidget(table.view) #add view instead of table
self.setLayout(layout)
A third way is to change the definition of Table
: you could subclass QTableView
instead of QWidget
(code not tested):
class Table(QTableView):
def __init__(self, model, parent):
super(Table,self).__init__(parent)
self.setMinimumSize(300,300)
self.setModel(model)