Pyside6获取动态创建的小部件的索引

huangapple go评论58阅读模式
英文:

Pyside6 get index of dynamically created widget

问题

我写了这个简单的脚本(用于概念理解),以更好地理解如何处理/管理动态创建的组合框。

所以在这个示例中,我们总共有5个动态创建的组合框,每个组合框都包含3个变量的列表。

在选择任何变量时,将运行comboFunction函数。

我想要理解的是:
1)如何获取正在选择的组合框的索引
2)正在选择的变量的索引。

并在comboFunction中打印出组合框和变量的索引。


例如,在下面的截图中,我选择了索引为0的组合框和索引为0的变量。

import sys
from PySide6 import QtWidgets

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QtWidgets.QGridLayout(self)
        self.lists = ["1", "2", "3"]

        for i in range(5):
            self.combobox = QtWidgets.QComboBox(self)
            self.combobox.addItems(self.lists)
            self.layout.addWidget(self.combobox, i, 0)
            self.combobox.currentIndexChanged.connect(self.comboFunction)

    def comboFunction(self):
        index_combo = self.layout.indexOf(self.sender())
        index_variable = self.sender().currentIndex()
        print(f"Selected Combo Box Index: {index_combo}, Selected Variable Index: {index_variable}")

if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec())

Pyside6获取动态创建的小部件的索引


<details>
<summary>英文:</summary>

I&#39;ve written this simple script(for concept understanding) to better understand how to handle/manage dynamically created combo boxes.

So in this example, we have a total of 5 dynamically created combo boxes, each containing a list of 3 variables.

When selecting any variable the function `comboFunction` is run.

What I want to understand, is:
1) How can I retrieve the index of the combo box being selected
2) The index of the variable being selected.

And print in the `comboFunction` the index of the Combobox and the variable.

---

For example in the screenshot below, I selected the combo box at index 0 and the variable at index 0.

---


[![enter image description here][1]][1]

    import sys
    
    from PySide6 import  QtWidgets
    
    
    class MyWidget(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
            
            self.layout = QtWidgets.QGridLayout(self)
            self.lists = [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;]
            
            for i in range(5):
                
                self.combobox = QtWidgets.QComboBox(self)
                self.combobox.addItems(self.lists)
                self.layout.addWidget(self.combobox, i,0)
                self.combobox.currentIndexChanged.connect(self.comboFunction)
    
        def comboFunction(self):
            print(&quot;hello world&quot;)
    
    
    
    if __name__ == &quot;__main__&quot;:
        app = QtWidgets.QApplication([])
    
        widget = MyWidget()
        widget.resize(800, 600)
        widget.show()
    
        sys.exit(app.exec())


  [1]: https://i.stack.imgur.com/oCENX.png

</details>


# 答案1
**得分**: 1

```python
一个简单的方法是将其数字作为属性添加,然后稍后读取:
```python
class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QtWidgets.QGridLayout(self)
        self.lists = ["1", "2", "3"]

        for i in range(5):
            self.combobox = QtWidgets.QComboBox(self)
            self.combobox.id_number = i
            self.combobox.addItems(self.lists)
            self.layout.addWidget(self.combobox, i, 0)
            self.combobox.currentIndexChanged.connect(self.comboFunction)

    def comboFunction(self, idx):
        combo = self.sender()
        print(f"从组合框 {combo.id_number} 中选择了变量 {idx}")


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())
英文:

A simple way would be to add its number as an attribute and read it later:

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QtWidgets.QGridLayout(self)
        self.lists = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]

        for i in range(5):
            self.combobox = QtWidgets.QComboBox(self)
            self.combobox.id_number = i
            self.combobox.addItems(self.lists)
            self.layout.addWidget(self.combobox, i, 0)
            self.combobox.currentIndexChanged.connect(self.comboFunction)

    def comboFunction(self, idx):
        combo = self.sender()
        print(f&quot;Selected the variable {idx} from combo {combo.id_number}&quot;)


if __name__ == &quot;__main__&quot;:
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())

huangapple
  • 本文由 发表于 2023年2月8日 19:41:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385302.html
  • pyside
  • pyside6
  • python
  • qt-designer
  • user-interface
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定