Issue
I created a QGIS Plugin (dialog)
with Qt Creator
. The dialog contains several tabs and additonal content and widgets on each tab. On one of these tabs I created a QLineEdit
, a QPushButton
and a QTableWidget
.
I would like to load locally saved geojson's
into a QTableWidget
(optional into a QTreeWidget).
I can load the geojsons via the button and show the file in QLineEdit but i am not able to show the dict(data)
within the QTableWidget
and load other geojson files
into the QTableWidget
.
class Dialog:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
def tr(self, message):
def add_action(
def initGui(self):
def unload(self):
def select_file(self):
filename, _filter = QFileDialog.getOpenFileName(
self.dlg, "Open File", "", '*.geojson')
self.dlg.lineEditInput.setText(filename)
with open(filename,"r") as geojson:
data = json.load(geojson)
def run(self):
if self.first_start == True:
self.dlg = DialogDialog(parent=self.iface.mainWindow())
self.dlg.pushButtonFile.clicked.connect(self.select_file)
self.dlg.open()
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
35.39314,
72.479306
]
},
"properties": {
"Street": "Text",
"City": "Text",
"Country": "Text"
}
}
]
}
Solution
The structure of a dictionary cannot be represented in a table, instead it is better to use a QTreeWidget or QTreeView, in this case I will use a custom QTreeWidget where you are implementing the loading method:
import json
from PyQt5 import QtCore, QtGui, QtWidgets
class TreeWidget(QtWidgets.QTreeWidget):
def load_geojson(self, geojson):
def fill(parent, d):
if isinstance(d, dict):
for k, v in d.items():
child = QtWidgets.QTreeWidgetItem()
child.setData(0, QtCore.Qt.DisplayRole, k)
parent.addChild(child)
fill(child, v)
elif isinstance(d, list):
for v in d:
fill(parent, v)
else:
child = QtWidgets.QTreeWidgetItem()
parent.addChild(child)
child.setData(0, QtCore.Qt.DisplayRole, d)
for k, v in geojson.items():
it = QtWidgets.QTreeWidgetItem([k])
self.addTopLevelItem(it)
fill(it, v)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QtWidgets.QPushButton("Load GeoJSON", clicked=self.onClicked)
self.tree_widget = TreeWidget()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(self.tree_widget)
self.resize(360, 480)
@QtCore.pyqtSlot()
def onClicked(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
self, "Open File", "", "GeoJSON Files (*.geojson)"
)
if filename:
with open(filename, "r") as geojson:
data = json.load(geojson)
self.tree_widget.load_geojson(data)
self.tree_widget.expandAll()
self.tree_widget.setHeaderLabel(filename)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.