PyQt custom widget not showing

后端 未结 2 1666
傲寒
傲寒 2021-01-25 03:40

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

2条回答
  •  隐瞒了意图╮
    2021-01-25 04:32

    You defined Tableas 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) 
    

提交回复
热议问题