PyQt 信号连接但不调用方法..(PyQt4.8,Python 3.2)

发布于 2024-11-01 15:43:07 字数 1209 浏览 0 评论 0原文

嘿伙计们,我知道这个问题还不够清楚(找不到如何用一行字解释这个问题)。我正在开发一个项目,其中主类中包含一个集中式 GUI,项目的各个组件由其他类的组合对象组成(在单独的文件中编码)。为了保持交换数据,每个对象也包含主类的对象(编辑:将尝试 E.Bendersky 建议的方法)。因此每个组件的信号是分开连接的。

这是 classA 构造函数中的连接代码;

QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.impAln)
QtCore.QObject.connect(self.ui.comboBox_2, QtCore.SIGNAL("currentIndexChanged(int)"), self.makeAlnTable)
QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL("clicked()"), self.reset)

classB 的构造函数中的连接代码;

QtCore.QObject.connect(self.ui.comboBox_3, QtCore.SIGNAL("currentIndexChanged(int)"), self.enableLineEdits)
QtCore.QObject.connect(self.ui.comboBox_4, QtCore.SIGNAL("currentIndexChanged(int)"), self.enableLineEdits)
QtCore.QObject.connect(self.ui.comboBox_5, QtCore.SIGNAL("currentIndexChanged(int)"), self.enableLineEdits)
QtCore.QObject.connect(self.ui.pushButton_4, QtCore.SIGNAL("clicked()"), self.wrdMaker)
QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL("clicked()"), self.reset)

问题是,当创建 classA 的实例时,连接和信号工作正常。但是,当创建 classB 的实例时,相应的信号不起作用(按钮不会启动方法等;不会引发错误/异常)。我尝试在网上查找“使用 Python 和 Qt 进行快速 GUI 编程”,但没有一个能解决这个特定问题。

非常感谢任何帮助,提前致谢。

Hey guys, i know the question doesn't make it clear enough (couldn't find how to explain the prob in a single line). I'm working on a project where i have a centralized gui included in a main class, with various components of the project made of composed objects of other classes (coded in separate files). To keep exchanging data, each object has the object of the main class composed in them as well (edit: will try the method E.Bendersky suggested). Thus the signals of each component are connected separately.

This is the connections code in the constructor for classA;

QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.impAln)
QtCore.QObject.connect(self.ui.comboBox_2, QtCore.SIGNAL("currentIndexChanged(int)"), self.makeAlnTable)
QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL("clicked()"), self.reset)

The connections code in the constructor for classB;

QtCore.QObject.connect(self.ui.comboBox_3, QtCore.SIGNAL("currentIndexChanged(int)"), self.enableLineEdits)
QtCore.QObject.connect(self.ui.comboBox_4, QtCore.SIGNAL("currentIndexChanged(int)"), self.enableLineEdits)
QtCore.QObject.connect(self.ui.comboBox_5, QtCore.SIGNAL("currentIndexChanged(int)"), self.enableLineEdits)
QtCore.QObject.connect(self.ui.pushButton_4, QtCore.SIGNAL("clicked()"), self.wrdMaker)
QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL("clicked()"), self.reset)

The problem is that when an instance of classA is created, the connections and signals work fine. But when an instance of classB is created, the respective signals don't work (buttons don't start methods etc; no error/exception is raised). I tried looking up online and in "Rapid Gui Programming with Python and Qt", but none cater to this specific problem.

Any help is greatly appreciated, thanks in advance..

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

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

发布评论

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

评论(1

往事风中埋 2024-11-08 15:43:07

事实上,恕我直言,这个问题太模糊了。如果没有具体最小代码示例来演示问题,将很难为您提供帮助。

我想解决你写的一件令人不安的事情:

保持交换数据,每个对象
具有主类的对象
也由它们组成

这几乎肯定是一个坏主意,而 Qt 的信号和槽正是设计用来解决而不创建这种耦合的问题 你让所有的对象都知道一些“主要”的中心对象——一场即将发生的设计灾难。为什么不使用信号和槽交换数据,而不需要这一额外的组合呢?

这听起来可能偏离主题,但我经常发现过于复杂的设计会导致微妙的编程错误,这些错误在设计被理清之前很难解释和调试。

Indeed, the question is far too vague IMHO. Without a concrete, minimal code sample that demonstrates the problem it will be very hard to help you.

I want to address one troubling thing you wrote though:

to keep exchanging data, each object
has the object of the main class
composed in them as well

This is almost certainly a bad idea, and exactly what Qt's signals and slots were designed to address without creating such coupling. You make all your objects know about some "main", central object - a design disaster waiting to happen. Why not exchange data with signals and slots, without this one extra composition?

This may sound off-topic, but I frequently found over-complicated designs to cause subtle programming errors that were hard to explain and debug before the design was untangled.

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