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

标签 python pyqt pyqt5 qtablewidget

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

我想加载本地保存的geojson's进入QTableWidget (可选进入 QTreeWidget)。

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

最佳答案

字典的结构不能用表格表示,最好使用 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_())

关于python - 如何使用 QToolButton 或 QPushButton 将本地保存的 GeoJSON 加载到带有 QTableWidget 或 QTableTree 的 Qt 对话框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57255207/

相关文章:

python - 使用Google Colaboratory在GCE上连接到MySQL

python - 更新只能从 GUI 线程或从 QQuickItem::updatePaintNode() 安排

python - QTableWidget-自动公式驱动单元格

python - py2app 应用程序与 PyQt5 崩溃

python - 使用 Beautiful soup 在 HTML 表格中查找信息

python - 将最后一行除以组的第一行

Python-PyQt5-MVC

python - 为什么 QPropertyAnimation 动画不起作用?

python - 为什么 Aptana (eclipse) 创建重复的 python 包/文件?

Python Qt 绑定(bind) : How to increase the width of lines with width 0