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
No comments:
Post a Comment