信号发射仅在一种功能中起作用,但在其他功能中不起作用?

发布于 2024-12-12 14:40:56 字数 1345 浏览 7 评论 0原文

我有一个有点奇怪的问题。我正在编写一个多线程应用程序,并使用 SIGNALS 将 QThread 的数据传递给 GUI 类数据。话不多说,这是一个简化的代码。

class GUI(uiMainWindow.MainWindow):
    def __init__(self, parent=None):
        super etc
        self.thread = Thread()
        self.connect(self.thread, SIGNAL("changeStatus(QString)"), self.setStatus, Qt.QueuedConnection)

    def setStatus(self, status):
        self.statusBar.setText(status)


class Thread(QThread):
    def __init__(self, parent=None, create=True):
        super etc
        self.create = create

    def run(self):
        if self.create:
            create_data()
        if not self.create:
            upload_data()

    def create_data(self):
        self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")
        #rest of the code

    def upload_data(self):
        self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")

很基本,对吧?然而,问题在于: self.emit 仅在 create_data 函数中起作用,但在 upload_data 中不起作用(或者在任何其他函数中;我也尝试将其放入 __init__ 中)。我尝试将 print "I got the status" + status 放入 setStatus 函数中。同样,它在 create_data() 函数中起作用,但在 upload_data() 函数中不起作用。

这两个函数之间的差异相对较小,据我所知,没有什么会干扰 self.emit 函数 - 事实上,在这两种情况下,self.emit 与该函数仅“相距”4-5 行定义。

这实在是令我费解。有什么帮助吗?提前致谢!

编辑:再次,据我所知,这两个函数之间的唯一区别在于 run() - 如果 create 参数为 True,则调用第一个函数,如果为 False,则调用第二个函数。

I have a somewhat strange problem. I'm coding a multithreaded app and am using SIGNALS to communicate the QThread's data to the GUI class data. Without further ado, here is a simplified code.

class GUI(uiMainWindow.MainWindow):
    def __init__(self, parent=None):
        super etc
        self.thread = Thread()
        self.connect(self.thread, SIGNAL("changeStatus(QString)"), self.setStatus, Qt.QueuedConnection)

    def setStatus(self, status):
        self.statusBar.setText(status)


class Thread(QThread):
    def __init__(self, parent=None, create=True):
        super etc
        self.create = create

    def run(self):
        if self.create:
            create_data()
        if not self.create:
            upload_data()

    def create_data(self):
        self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")
        #rest of the code

    def upload_data(self):
        self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")

Pretty basic, right? However, here's the problem: the self.emit works only in create_data function, but not in upload_data (or for that matter, in any other function; I tried putting it in __init__ as well). I tried putting print "I got the status" + status in the setStatus function. Again, it works in the create_data() function, but not in the upload_data() function.

The differences between the two functions are relatively minor, and as far as I can tell, nothing is interfering with the self.emit function - in fact, in both cases, self.emit is only 4-5 lines "away" from the function definition.

This is really puzzling to me. Any help? Thanks in advance!

EDIT: again, as far as I can tell, the only difference between the two functions is in the run() - the first one is called if create parameter is True, and the second one if it is False.

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

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

发布评论

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

评论(1

泡沫很甜 2024-12-19 14:40:56

我在我的岗位上是对的。 Thread() 和 Thread(create=False) 之间的区别至关重要。我必须定义一个新方法(一个是 self.thread = Thread() ,另一个是 self.diff_thread = Thread(create=False) )并连接到不同的插槽使其发挥作用。

I was right in my post. The difference between Thread() and Thread(create=False) was crucial. I had to define a new method (one was self.thread = Thread() and the other self.diff_thread = Thread(create=False)) and connect to different slots to make it work.

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