使用子目录时的 PyQt4 SIGNAL/SLOT 问题 [英] PyQt4 SIGNAL/SLOT problem when using sub-directories

查看:58
本文介绍了使用子目录时的 PyQt4 SIGNAL/SLOT 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Thanks in advance for taking the time to read this. Apologies that it is somewhat verbose. But hopefully it fully explains the problem. Stripped code demonstrating the issue is included.

我遇到了 PyQt4 信号/插槽问题.如果我在单个文件中编写,我可以使一切正常工作,但如果我希望使用的某些功能被移动到子目录/类中,我就无法工作.

I'm having an issue with PyQt4 SIGNAL/SLOTS. While I can make everything work fine if I am writing in a single file, I can't make things work if I some of the functions I wish to use are moved to sub-directories/classes.

我浏览了 Python 绑定文档 我可以看到使用单个文件时这是如何工作的.但我想要做的是:

I've looked through the Python Bindings document I can see how this works when using a single file. But what I am trying to do is this:

  • 根目录中的 main.py 文件,其中包含 MainWindow __init__ 代码.
  • 该文件导入了许多小部件.每个小部件都存储在其自己的子目录中.所有子目录都包含一个 __init__.py 文件.这些子目录位于名为bin"的目录中,该目录本身位于根目录中
  • 其中一些小部件需要在它们之间建立信号/插槽链接.这就是我失败的地方.
  • main.py file in root dir which contains the MainWindow __init__ code.
  • This file imports a number of widgets. Each widget is stored in its own sub-directory. All sub-directories contain an __init__.py file. These sub-directories are inside of a directory called 'bin', which is itself in the root dir
  • Some of these widgets need to have SIGNAL/SLOT links between them This is where I fall down.

所以文件结构是:

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

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

The following code shows the problem. This code creates a very basic main window that contains a central QTextEdit() widget and a dockable QTextEdit() widget. All that happens is that when the text in the central widget is changed, the same text is shown in the dockable widget. The example works. But it does so by connecting the signal textChanged() in the bin/textEditor/plugin.py file that creates the central QTextEdit() with a function in main.py. I would like it to do exactly the same thing but connexted to the updateUi function in bin/textEditor/plugin.py

如果有人能对此有所了解,我将不胜感激.我确定这很简单.但是,对于涵盖此内容的任何教程或我所做的一切都非常错误的陈述的指导同样值得赞赏!再次感谢您的时间:

If anyone could shed some light on this, I would be hugely grateful. I'm sure it is simple. But direction to any tutorials that cover this or statements that I am doing it all very wrong are equally appreciated!. Thanks again for your time:

### 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()

两个插件文件里面的代码是:

The code inside of the two plugin files is:

### 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)

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

def create(self):

    # Create a text editing box
    self.editor = QTextEdit()

    # Add to main window
    self.setCentralWidget(self.editor)

    # connect text change to update log window. This is presumably what I need to 
    # change so that it connects to the function below instead of the on in main.py
    self.connect(self.editor, SIGNAL("textChanged()"), self.updateUi)

def updateUi(self):
    text = self.editor.toPlainText()
    self.logWidget.setText(text)

推荐答案

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

For starters, is there a reason you're using a very old version of the PyQt release document? The new one is: here

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

There are a few things you are doing that are a bit unusual. Generally import statements in python are placed at the top of the file (to more easily see dependencies), but I assume you're doing this to support a more generalized import system for plugins in the future.

似乎基本问题是您试图将信号源连接到另一个对象中的插槽,而不是将另一个对象存储在特定位置.为此,您可能需要在 main 中建立连接,创建一个中性的updateUi"插槽,以发出所有插件都在等待的特殊信号,或者只是在 main 中保留对这些子对象的引用,并小心初始化顺序.

It seems like the basic problem is you're trying to connect a signal source to a slot in another object, without storing that other object in a particular place. To do this you probably need to either make the connection in main, make a neutral "updateUi" slot that emits it's own special signal that all the plugins are waiting for, or just keep a reference to those subobjects in main and be careful with the initialization order.

这篇关于使用子目录时的 PyQt4 SIGNAL/SLOT 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆