基于Tuple自动创建QStackedWidget页面 [英] Automatically create QStackedWidget pages based on Tuple

查看:81
本文介绍了基于Tuple自动创建QStackedWidget页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 python 还很陌生,我觉得这是一个高级问题,考虑到它可能超出了 Stack Exchange 的范围.请耐心等待.

I'm fairly new to python, and I feel this is an advanced question, with that in mind it might be out of the scope of Stack Exchange. Please bear with me.

我有一个 QTreeWidget 和 QStackedWidget.我已经使用元组填充了 QTreeWidget

I have a QTreeWidget and QStackedWidget. I have populated the QTreeWidget using a tuple

TreeList = ({

    'Header1': ((
        'Item11',
        'Item12',
    )),

    'Header2': ((
        'Item21',
        'Item22'
    ))
})

for key, value in TreeList.items():
    root = QTreeWidgetItem(self.QTreeWidget, [key])
    for val in value:
        root.addChild(QTreeWidgetItem([val]))

我想使用相同的元组来填充 QStackedWidget 与 QTreeWidget 对应的页面,并在同一循环中建立信号和槽.我试过使用:

I would like to use this same tuple to populate a QStackedWidget with pages that correspond with the QTreeWidget and establish signals and slots with in the same loop. I have tried using:

for key in TreeList['Item1']:
    page = QWidget()
    page.setObjectName(key)
    self.QStackedWidget.addWidget(page)
    print(self.QStackedWidget)

但所有对象都指向相同的十六进制值,我认为这意味着它们不是唯一的.也许我需要增加索引?在任何一种情况下,我都无法弄清楚如何在执行此操作时生成信号和槽,因为它们是动态创建的,我需要唯一但可预测的名称.

but all of the objects point to the same hex value, which I assume means they are not unique. Perhaps I need to increment the index? In either case I have not been able to figure out how to generate the signals and slots while doing this, since they are being dynamically created and I need unique but predictable names.

这个想法是通过简单地向元组添加一个新的类别或项目,QTreeView、QStackedWidget 和信号/槽都在运行时生成,而无需修改元组之外的任何源代码.

The idea is that by simply adding a new category or item to the tuple, the QTreeView, QStackedWidget, and signals / slots are all generated at runtime without having to modify any source code beyond the tuple.

实现这一目标的最佳方法是什么?

What would be the best way to accomplish this?

对不起,如果这太多了,但我认为这可能是一个很好的问题,以防其他人在以后尝试同样的事情.

Sorry if this is too much, but I figured it might be a good question in case anyone else attempts the same thing at a later time.

我使用的是 Python 3.6 和 PyQt5

I'm using Python 3.6 and PyQt5

推荐答案

的想法是将 QStackedWidget 的索引与 QTreeWidgetItem 关联起来,所以我们可以使用QTreeWidgetItemsetData()方法保存索引,然后用data()方法恢复:

The idea is to associate the index of the QStackedWidget with the QTreeWidgetItem, so we can use the setData() method of QTreeWidgetItem to save the index, and then recover it with the data() method:

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.tree = QTreeWidget(self)
        self.stack = QStackedWidget(self)
        lay = QHBoxLayout(self)
        lay.addWidget(self.tree)
        lay.addWidget(self.stack)

        for key, value in TreeList.items():
            root = QTreeWidgetItem(self.tree, [key])
            for val in value:
                item = QTreeWidgetItem([val])
                root.addChild(item)
                widget = QLabel(val,  self)
                ix = self.stack.addWidget(widget)
                item.setData(0, Qt.UserRole, ix)
        self.tree.expandAll()
        self.tree.itemClicked.connect(self.onItemClicked)

    def onItemClicked(self, item, column):
        val = item.data(0, Qt.UserRole)
        if val is not None:
            self.stack.setCurrentIndex(val)

TreeList = ({

    'Header1': ((
        'Item11',
        'Item12',
    )),

    'Header2': ((
        'Item21',
        'Item22'
    ))
})

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于基于Tuple自动创建QStackedWidget页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆