3

I tried to load QgsAttributeTableModel into QgsAttributeTableView or a plain Qt QTableView but it could run from inside a class method or function but it runs without any function or method. Below is the code that doesn't work.

This one works...

from PyQt4.QtGui import QTableView, QDialog, QVBoxLayout
from qgis.core import QgsVectorLayerCache
from qgis.gui import (QgsAttributeTableModel,
                      QgsAttributeTableView,
                      QgsAttributeTableFilterModel)
import qgis.utils
layer = iface.activeLayer()
if not layer is 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)
    dialog = QDialog(iface.mainWindow())
    attribute_table_view.setParent(dialog)
    layout = QVBoxLayout(dialog)
    layout.addWidget(attribute_table_view)
    dialog.resize(800,600)
    dialog.show()

But this one doesn't

from PyQt4.QtGui import QTableView, QDialog, QVBoxLayout
from qgis.core import QgsVectorLayerCache
from qgis.gui import (QgsAttributeTableModel,
                      QgsAttributeTableView,
                      QgsAttributeTableFilterModel)
import qgis.utils

def open_attr_table(layer):
    if not layer is 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)
        dialog = QDialog(iface.mainWindow())
        attribute_table_view.setParent(dialog)
        layout = QVBoxLayout(dialog)
        layout.addWidget(attribute_table_view)
        dialog.resize(800,600)
        dialog.show()
layer = iface.activeLayer()
open_attr_table(layer)

Both codes are identical. The only difference is, one runs inside a function, the other doesn't. I have tested them in QGIS 2.8 and 2.14.

I am wondering if QGIS has disabled the use of QgsAttributeTableFilterModel and QgsAttributeTableModel in QGIS plugins. Is there anything I should do to fix this issue?

Similar issues with no solution

show-attribute-table-in-the-form-table-of-contents

how-to-show-attribute-table-in-the-form

wondim
  • 1,383
  • 11
  • 26
  • 2
    dialog is going out of scope when the function ends, show() isn't blocking so the function, runs, shows the dialog, ends, and delete the dialog instance. – Nathan W Jul 10 '16 at 12:09
  • Thank you for the suggestion! It is now working when using dialog.exec_() But could you please explain more on why show is not working and a workaround as dialog will block the rest of the interface when using exec. – wondim Jul 10 '16 at 12:13

0 Answers0