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

Python 获取由单独类方法/函数中的事件触发的QComboBox项文本,python,python-3.x,qt-designer,qcombobox,pyside2,Python,Python 3.x,Qt Designer,Qcombobox,Pyside2,除了函数中的索引之外,我在从QComboBox获取任何信息时遇到问题。大多数类似示例都在与触发事件相同的类中提供函数。我正试图从外部类和文件中获取此信息 文件夹结构如下所示: 在paintEventTest.py中,我创建了一个列表,用于用项目填充组合框。ComboEvent是从EventMethods.py实例化的,我正在尝试在函数中打印itemText EventMethods.py from PySide2.QtWidgets import QWidget, QPushButton, QC

除了函数中的索引之外,我在从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中的文本字符串为空,应为节类型,但我的索引正确。Console打印一个空白行和一个正确行。getSectionShape表单EventMethods.py中的self.itemTextindex出现问题。感谢您的帮助

comboBoxEvent中self.itemTextindex是什么意思?因为您正在获取当前comboBoxEvent项的文本。Comboxevent有物品吗?不,它是空的,只有窗口中的QCOMBOX包含项,ComboxEvent不是窗口中的QCOMBOX。这就解释了为什么你什么都得不到

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

一,。如果您只想获取currentText,则使用currentTextChanged信号,comboBoxEvent不必从QComboBox继承

类ComboBox事件: def getSectionShapeself,文本: 打印文本 ... comboBox=self.ui\u window.comboBox comboBox.setCurrentIndex0 对于部分列表中的项目: comboBox.AddItem self.comboEvent=comboBoxEvent comboBox.currentTextChanged.connectself.comboEvent.getSectionShape ... 二,。如果您希望在选择新项目时获取QComboBox的所有可能信息,那么最好在getSectionShape中获取QComboBox,因为存在以下可能性

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

从PySide2导入QtCore、QtWidgets 类ComboboxenTQCore.QObject: @QtCore.插槽 def getSectionShapeself: obj=自发送程序 如果isinstanceobj,则QtWidgets.QComboBox: index=obj.currentIndex text=obj.itemTextindex 打印文本 printIndex更改为:{}.formatindex ... comboBox=self.ui\u window.comboBox comboBox.setCurrentIndex0 对于部分列表中的项目: comboBox.AddItem comboEvent=comboBoxEventself comboBox.currentIndexChanged.connectcomboEvent.getSectionShape ... 2.2通过functools.partial传递QCOMBOX:

类ComboBox事件: def getSectionShapeself、组合、索引: index=combo.currentIndex text=combo.itemTextindex 打印文本 printIndex更改为:{}.formatindex 从functools导入部分 ... comboBox=self.ui\u window.comboBox comboBox.setCurrentIndex0 对于部分列表中的项目: comboBox.AddItem self.comboEvent=comboBoxEvent comboBox.currentIndexChanged.connectpartialself.comboEvent.getSectionShape,comboBox ... 将QCOMBOX通过lambda 类ComboBox事件: def getSectionShapeself,组合: index=combo.currentIndex text=combo.itemTextindex 打印文本 printIndex更改为:{}.formatindex ... comboBox=self.ui\u window.comboBox comboBox.setCurrentIndex0 对于部分列表中的项目: comboBox.AddItem self.comboEvent=comboBoxEvent comboBox.currentIndexChanged.connectlambda ix,c=comboBox:self.comboEvent.getSectionShapec ...
感谢您展示了将QComboBox/QObject传递给另一个类的多种方式。这是一个品味的问题,你选择或是其中一个被认为是最好的做法。对于lambda示例,您是否打算编写2.3?