使用子目录时出现PyQt4 SIGNAL / SLOT问题

时间:2010-09-30 01:08:48

标签: python qt qt4 pyqt pyqt4

提前感谢您花时间阅读本文。抱歉它有点冗长。但希望它能完全解释这个问题。包括证明该问题的剥离代码。

我遇到了PyQt4 SIGNAL / SLOTS的问题。虽然如果我在一个文件中写作,我可以使一切工作正常,但如果我希望使用的一些函数被移动到子目录/类中,我就无法工作。

我查看了Python Bindings document我可以看到使用单个文件时这是如何工作的。但我想要做的是:

  • 根目录中的main.py文件,其中包含MainWindow __init_ _ code。
  • 此文件导入许多小部件。每个小部件都存储在自己的子目录中。所有子目录都包含__init__.py个文件。这些子目录位于名为“bin”的目录中,该目录本身位于根目录
  • 其中一些小工具需要在它们之间有SIGNAL / SLOT链接。这就是我倒下的地方。

所以文件结构是:

 - main.py
 - bin/textEditor/__init__.py
 - bin/textEditor/plugin.py
 - bin/logWindow/__init__.py
 - bin/logWindow/plugin.py

以下代码显示了该问题。此代码创建一个非常基本的主窗口,其中包含一个中心QTextEdit()窗口小部件和一个可停靠的QTextEdit()窗口小部件。所发生的一切是,当中央窗口小部件中的文本发生更改时,可停靠窗口小部件中会显示相同的文本。该示例有效。但它是通过将创建中心bin/textEditor/plugin.py的{​​{1}}文件中的信号textChanged()与QTextEdit()中的函数相连来实现的。我希望它完全相同但连接到main.py

中的updateUi函数

如果有人能够对此有所了解,我将非常感激。我确信这很简单。但是,对于任何涵盖此内容的教程或我正在做的所有非常错误的陈述的指导同样受到赞赏!再次感谢您的时间:

bin/textEditor/plugin.py

两个插件文件中的代码是:

### main.py
import os
import sys
# Import PyQT modules
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# Start the main class
class MainWindow(QMainWindow):

    # Initialise
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        # Name and size the main window
        self.setWindowTitle("EDITOR/LOG")
        self.resize(800, 600)

        import bin.logWindow.plugin as logWindow
        logWindow.create(self)

        import bin.textEditor.plugin as textEditor
        textEditor.create(self)

    def updateUi(self): 
        # I can connect to this function from within bin/textEditor/plugin.py (see 
        # below) but I want to connect to the function located in 
        # bin/textEditor/plugin.py instead
        text = self.editor.toPlainText()
        self.logWidget.setText(text)

# Run the app
def main():
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()
# Call main
main()

### bin/textEditor/plugin.py
# Import PyQT modules
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def create(self):
    # Add a dockable widget
    self.logDockWidget = QDockWidget("Log", self)
    self.logDockWidget.setObjectName("LogDockWidget")
    self.logDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea|
                                       Qt.RightDockWidgetArea)

    self.logWidget = QTextEdit()
    self.logDockWidget.setWidget(self.logWidget)
    self.addDockWidget(Qt.LeftDockWidgetArea, self.logDockWidget)

1 个答案:

答案 0 :(得分:2)

首先,您是否有理由使用旧版本的PyQt发布文档?新的是:here

你正在做的一些事情有点不寻常。通常python中的import语句放在文件的顶部(更容易看到依赖项),但我假设你这样做是为了支持将来更普遍的插件导入系统。

似乎基本的问题是你试图将信号源连接到另一个对象的插槽,而不将其他对象存储在特定的位置。要做到这一点,您可能需要在main中建立连接,创建一个中性的“updateUi”插槽,发出它自己的特殊信号,所有插件都在等待,或者只是在main中保留对这些子对象的引用,并注意初始化顺序。

相关问题