gpt4 book ai didi

python - 获取由单独的类方法/函数中的事件触发的 QComboBox 项目文本

转载 作者:行者123 更新时间:2023-12-01 00:51:18 25 4
gpt4 key购买 nike

除了函数中的索引之外,我无法从 QComboBox 获取任何信息。大多数类似的示例都提供与触发事件相同的类中的函数。我正在尝试从外部类(和文件)中获取它。

文件夹结构如此处答案中提供:

在paintEventTest.py中,我创建了一个列表,用于用项目填充组合框。 ComboEvent 是从 EventMethods.py 实例化的,我正在尝试在我的函数中打印 itemText。

EventMethods.py

from PySide2.QtWidgets import QWidget, QPushButton, QComboBox

class widgetEventHandler(QWidget):

def closeEvent(self, event):
print("TEST")


class comboBoxEvent(QComboBox):
def getSectionShape(self, index):
text = str(self.itemText(index))
print(text)
print("Index changed to: " + str(index))

paintEventTest.py

import sys

from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import (
QApplication, QPushButton, QLineEdit, QTextEdit, QSpinBox, QMainWindow, QDesktopWidget, QTableWidget,
QTableWidgetItem, QToolButton, QToolTip)
from PySide2.QtCore import QFile, QObject, Qt

from EventMethods import *


class MainForm(QMainWindow):
def __init__(self, ui_file, parent=None):
super(MainForm, self).__init__(parent)
ui_file = QtCore.QFile(ui_file)
ui_file.open(QtCore.QFile.ReadOnly)

### Load UI file from Designer ###
loader = QUiLoader()
self.ui_window = loader.load(ui_file)
ui_file.close()
self.ui_window.show()

#region widget code
widget = self.ui_window.widget
widget.setStyleSheet("""
QWidget {
border: 1px solid lightgrey;
border-radius: 2px;
background-color: rgb(255, 255, 255);
}
""")

#endregion

sectionList = []
sectionList.append("Rectangle")
sectionList.append("Diamond")
sectionList.append("Circle")
sectionList.append("Box")
sectionList.append("T-Section")
sectionList.append("I-Section")

comboBox = self.ui_window.comboBox
#comboBox = QtWidgets.QComboBox #Just to get intellisense working. Gets commented out
comboBox.setCurrentIndex(0)

for item in sectionList:
comboBox.addItem(item)

comboEvent = comboBoxEvent(self)
comboBox.currentIndexChanged.connect(comboEvent.getSectionShape)


if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle('Fusion')
form = MainForm('./UI designer/testUI.ui')
sys.exit(app.exec_())

testUI.ui 文件如下所示,位于“UI 设计器”文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>996</width>
<height>892</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGraphicsView" name="graphicsView">
<property name="minimumSize">
<size>
<width>0</width>
<height>200</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="Drawer" name="widget" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>250</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>300</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>996</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>Drawer</class>
<extends>QWidget</extends>
<header>myDrawWidget</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

调试时 getSectionShape 中的文本字符串为空(需要节类型),但我的索引是正确的。控制台打印一行空白行和一行正确行。 getSectionShape 形式 EventMethods.py 中的 self.itemText(index) 无法正常工作。如有任何帮助,我们将不胜感激!

最佳答案

self.itemText(index) 在comboBoxEvent 中意味着什么? 因为您正在获取当前comboBoxEvent 项目的文本。 comboBoxEvent 有项目吗? 没有,它是空的,只有窗口中的 QComboBox 有项目,comboBoxEvent 不是窗口中的 QComboBox。这就解释了为什么你没有得到任何东西。

根据您想要执行的操作,有以下方法:

1.如果您只想获取 currentText 然后使用 currentTextChanged 信号,comboBoxEvent 不必继承自 QComboBox。

class comboBoxEvent:
def getSectionShape(self, text):
print(text)
# ...
comboBox = self.ui_window.comboBox
comboBox.setCurrentIndex(0)
for item in sectionList:
comboBox.addItem(item)
self.comboEvent = comboBoxEvent()
comboBox.currentTextChanged.connect(self.comboEvent.getSectionShape)
# ...

2.如果您想在选择新项目时获取 QComboBox 的所有可能信息,那么最好在 getSectionShape 中获取 QComboBox,为此有以下可能性。

2.1将comboBoxEvent设置为QObject,以便可以使用槽中的sender()方法获取QComboBox:

from PySide2 import QtCore, QtWidgets


class comboBoxEvent(QtCore.QObject):
@QtCore.Slot()
def getSectionShape(self):
obj = self.sender()
if isinstance(obj, QtWidgets.QComboBox):
index = obj.currentIndex()
text = obj.itemText(index)
print(text)
print("Index changed to: {}".format(index))
# ...
comboBox = self.ui_window.comboBox
comboBox.setCurrentIndex(0)
for item in sectionList:
comboBox.addItem(item)
comboEvent = comboBoxEvent(self)
comboBox.currentIndexChanged.connect(comboEvent.getSectionShape)
# ...

2.2通过functools.partial传递QComboBox:

class comboBoxEvent:
def getSectionShape(self, combo, index):
index = combo.currentIndex()
text = combo.itemText(index)
print(text)
print("Index changed to: {}".format(index))
from functools import partial

# ...
comboBox = self.ui_window.comboBox
comboBox.setCurrentIndex(0)
for item in sectionList:
comboBox.addItem(item)
self.comboEvent = comboBoxEvent()
comboBox.currentIndexChanged.connect(partial(self.comboEvent.getSectionShape, comboBox))
# ...
  • 通过 lambda 传递 QComboBox
  • class comboBoxEvent:
    def getSectionShape(self, combo):
    index = combo.currentIndex()
    text = combo.itemText(index)
    print(text)
    print("Index changed to: {}".format(index))
    # ...
    comboBox = self.ui_window.comboBox
    comboBox.setCurrentIndex(0)
    for item in sectionList:
    comboBox.addItem(item)
    self.comboEvent = comboBoxEvent()
    comboBox.currentIndexChanged.connect(lambda ix, c= comboBox: self.comboEvent.getSectionShape(c))
    # ...

    关于python - 获取由单独的类方法/函数中的事件触发的 QComboBox 项目文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557896/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com