PySide2 QAction事件被阻止直到窗口关闭

时间:2018-08-03 14:43:01

标签: python-3.x qt pyside2

遇到了一个似乎事件循环被阻止的问题(或者我没有开始?),但我一直无法解决它。

将显示GUI,并且菜单具有响应性,但是单击“文件”->“打开”,应该创建几乎立即的打印语句。

我知道我已将正确的信号映射到插槽,因为一旦执行File-> Quit或关闭窗口,打印语句就会出现。

我用相同的UI文件在C ++中实现了相同的示例,并且按预期方式工作。

问题表现为以下配置:

  • Windows 10,Python 3.6.4,pyside2-5.11.1a1.dev1530708810518

  • Windows 10,Python 2.7.14,pyside2-5.11.0a1.dev1528378291

test.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>400</width>
    <height>200</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Qt for Python</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout"/>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>940</width>
     <height>21</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="actionOpen"/>
    <addaction name="actionQuit"/>
   </widget>
   <addaction name="menuFile"/>
  </widget>
  <action name="actionQuit">
   <property name="text">
    <string>Quit</string>
   </property>
  </action>
  <action name="actionOpen">
   <property name="text">
    <string>Open</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>actionQuit</sender>
   <signal>triggered()</signal>
   <receiver>MainWindow</receiver>
   <slot>close()</slot>
  </connection>
 </connections>
</ui>

test.py:

#!/usr/bin/env python
import sys

from PySide2 import QtCore, QtGui, QtWidgets

from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile


class MyWidget(QtCore.QObject):
    def __init__(self):
        QtCore.QObject.__init__(self)

        file = QFile("test.ui")
        file.open(QFile.ReadOnly)

        loader = QUiLoader()
        self.ui = loader.load(file)

        openAction = self.ui.findChild(QtWidgets.QAction, 'actionOpen')
        openAction.triggered.connect(self.func)

    @QtCore.Slot()
    def func(self):
        print("func has been called!")

app = QtWidgets.QApplication(sys.argv)
myWidget = MyWidget()
myWidget.ui.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

在Windows上使用PySide2的问题,打开了错误PYSIDE-767