如何在PYQT中打开多个持久窗口?

发布于 2025-02-04 00:08:42 字数 1093 浏览 4 评论 0原文

我正在尝试在pyqt6/pyside6 qtreeview中显示JSON元数据。 长度大于1。

如果

def openTreeWidget(app, jmd):
    view = QTreeView()
    model = JsonModel()
    view.setModel(model)
    model.load(jmd)
    app.w = view  # app = `self` of a QMainWindow instance
    app.w.show()

for md in jsonMetadataList:
    openTreeWidget(self, md)

我的JSON元数据列表的 > JSONMODEL 基于: https:https://doc.qt。 io/qtforpython/tutorials/basicutorial/treewidget.html
我从: htttps”> htttps ://www.pythonguis.com/tutorials/pyqt6-creating-multiple-windows/

在当前案例中,所有弹出式UPS(除一个)暂时打开后关闭。仅JSONMETADATALIST中的最后一个项目仍显示在持久窗口中。我相信,某种程度上,我不会对以前的Windows进行引用,并在单个小部件上重新打开/重写数据。如何保留参考?

另外,我对pyqt/pyside是非常新的,所以我 在做事,无论他们现在看多么丑陋。当然,这会随着时间的流逝而变得更好:);

I am trying to display JSON metadata in PyQt6/PySide6 QTreeView. I want to generalize for the case where multiple persistent windows (QtWidgets) pop up if my JSON metadata list has a length greater than 1.

for example:

def openTreeWidget(app, jmd):
    view = QTreeView()
    model = JsonModel()
    view.setModel(model)
    model.load(jmd)
    app.w = view  # app = `self` of a QMainWindow instance
    app.w.show()

for md in jsonMetadataList:
    openTreeWidget(self, md)

where TreeItem and JsonModel are based on: https://doc.qt.io/qtforpython/tutorials/basictutorial/treewidget.html

I stole the app.w idea from: https://www.pythonguis.com/tutorials/pyqt6-creating-multiple-windows/

In the current case, all pop ups (except one) close after momentarily opening. Only the last item in jsonMetadataList remains displayed in a persistent window. I believe that somehow I am not keeping the reference to previous windows and reopening/rewriting data on a single widget. How can I keep the reference?

Also, I am very new to PyQt/PySide so I'm just doing things no matter how ugly they look at the moment. This will, of course, get better with time :);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2025-02-11 00:08:42

我设法通过不破坏参考来使它变得更加紧密。这就是我做的。

def openTreeWidget(app, jmd):
    """
    app is the parent QWidget (here, a QMainWindow)
    jmd is JSON metadata stored in a string 
    """
    view = QTreeView()
    model = JsonModel()
    view.setModel(model)
    model.load(jmd)
    return view  

# `self` of a QMainWindow instance
self.temp = [None]*len(jsonMetadataList)  # a list storing individual handles for all JSON metadata entries
for ii, md in enumerate(jsonMetadataList):
    self.temp[ii] = openTreeWidget(self, md)  # get the reference for QTreeView and store it in temp[ii]
    self.temp[ii].show()  # show the ii-th metadata in QTreeView

仍然欢迎更好的想法:)

I managed to bodge it up by not destroying the reference. Here's how I did it.

def openTreeWidget(app, jmd):
    """
    app is the parent QWidget (here, a QMainWindow)
    jmd is JSON metadata stored in a string 
    """
    view = QTreeView()
    model = JsonModel()
    view.setModel(model)
    model.load(jmd)
    return view  

# `self` of a QMainWindow instance
self.temp = [None]*len(jsonMetadataList)  # a list storing individual handles for all JSON metadata entries
for ii, md in enumerate(jsonMetadataList):
    self.temp[ii] = openTreeWidget(self, md)  # get the reference for QTreeView and store it in temp[ii]
    self.temp[ii].show()  # show the ii-th metadata in QTreeView

Better ideas are still welcome :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文