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

发布于 2024-09-25 07:58:31 字数 3389 浏览 2 评论 0原文

预先感谢您花时间阅读本文。抱歉,它有点冗长。但希望它能完全解释这个问题。包含演示该问题的剥离代码。

我的 PyQt4 信号/插槽有问题。虽然如果我在单个文件中编写,我可以使一切正常工作,但如果我希望使用的某些函数移动到子目录/类,我就无法使一切正常工作。

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

  • 根目录中的 main.py 文件,其中包含 MainWindow __init_ _ 代码。
  • 该文件导入了许多小部件。每个小部件都存储在其自己的子目录中。所有子目录都包含一个 __init__.py 文件。这些子目录位于名为“bin”的目录内,该目录本身位于根目录中。
  • 其中一些小部件需要在它们之间具有信号/插槽链接,这就是我失败的地方。

所以文件结构是:

 - 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 中的 updateUi 函数相连。

如果有人能对此有所了解,我将非常感激。我确信这很简单。但是,对于任何涵盖此内容的教程的指导或我做的这一切都非常错误的声明,我们同样表示赞赏!再次感谢您的宝贵时间:

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

并且

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

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.

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.

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 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.

So the file structure is:

 - main.py
 - bin/textEditor/__init__.py
 - bin/textEditor/plugin.py
 - bin/logWindow/__init__.py
 - bin/logWindow/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)

And

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-10-02 07:58:31

对于初学者来说,您使用非常旧版本的 PyQt 发行文档是否有原因?新的位置是:此处

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

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文