1

I have created a plugin using the Plugin Builder plugin, and I tried to adapt it to show a QgsAttributeTable (by following this example). I get absolutely no errors or warnings, but it simply shows an empty white box. Do you have any suggestions for what I should try?

class FooDockWidget(QtWidgets.QDockWidget):
    def __init__(self, parent=None):

        super().__init__(parent)
        self.setWindowTitle('This_is_the_window_title!!!')

        widget = QWidget(self)
        self.setWidget(widget)

        layout = QVBoxLayout(widget)
        widget.setLayout(layout)

        layer = iface.activeLayer()
        assert layer is not None

        canvas = iface.mapCanvas()
        vector_layer_cache = QgsVectorLayerCache(layer, 10000)
        attribute_table_model = QgsAttributeTableModel(vector_layer_cache)
        attribute_table_model.loadLayer()

        attribute_table_filter_model = QgsAttributeTableFilterModel(canvas, attribute_table_model)
        attribute_table_view = QgsAttributeTableView()
        attribute_table_view.setModel(attribute_table_filter_model)

        layout.addWidget(attribute_table_view)

enter image description here

1 Answer 1

2

Your class lacks of self, so, at the end of the instantiation (= end of the __init__), the references of the objects without self not binded in other objects of the class are destroyed, so not displayed.

Correct the end of your __init__ with :

        self.canvas = iface.mapCanvas()
        self.vector_layer_cache = QgsVectorLayerCache(layer, 10000)
        self.attribute_table_model = QgsAttributeTableModel(self.vector_layer_cache)
        self.attribute_table_model.loadLayer()

        self.attribute_table_filter_model = QgsAttributeTableFilterModel(self.canvas, self.attribute_table_model)
        self.attribute_table_view = QgsAttributeTableView()
        self.attribute_table_view.setModel(self.attribute_table_filter_model)

        layout.addWidget(self.attribute_table_view)

Your Answer

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

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