PyQt custom widget not showing

后端 未结 2 1667
傲寒
傲寒 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:28

    the problem: table.view has no parent. If you add self.view.show() to test to Table.initUi(), you get two widgets, MyWindow with empty table as tmoreau wrote and table.view as isolated widget.

    You can either pass a parent when constructing table.view in Table.initUi()

    self.view = QTableView(self) 
    

    (then you don't need a layout) or add table.view to a layout as written by tmoreau, Then the tableview is reparented.

    Removing the class has the same effect, then the tableview is added to layout.

    0 讨论(0)
  • 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) 
    
    0 讨论(0)
提交回复
热议问题