如何使用 QToolButton 或 QPushButton 将本地保存的 GeoJSON 加载到带有 QTableWidget 或 QTableTree 的 Qt 对话框中?

How to load a local saved GeoJSON into a Qt Dialog with QTableWidget or QTableTree by using a QToolButton or QPushButton?

我用 Qt Creator 创建了一个 QGIS Plugin (dialog)。该对话框包含多个选项卡以及每个选项卡上的其他内容和小部件。在其中一个选项卡上,我创建了一个 QLineEdit、一个 QPushButton 和一个 QTableWidget

我想将本地保存的 geojson's 加载到 QTableWidget(可选到 QTreeWidget)中。

我可以通过按钮加载 geojsons 并在 QLineEdit 中显示文件,但我无法在 QTableWidget 中显示 dict(data) 并将其他 geojson files 加载到 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"
      }
    }
  ]
}

字典的结构不能用table表示,最好使用QTreeWidget或QTreeView,在这种情况下,我将在您实现加载方法的地方使用自定义QTreeWidget:

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_())