QPixmap在GUI线程外不安全 [英] QPixmap not safe outside GUI thread

查看:653
本文介绍了QPixmap在GUI线程外不安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序具有主GUI线程来处理用户界面.

My program has the main GUI thread to handle the user interface.

启动另一个线程来处理艰苦的工作(循环,计算等),而无需冻结主GUI.

Another thread is started to handle the hard work (loops, calculations, etc) without freezing the main GUI.

从我的计算线程"中,我正在使用另一个模块draw_plots,该模块仅绘制并保存多种图形.

From my "calculations thread" I am using another module draw_plots which only draws and saves many kinds of plots.

import draw_plots as plots
    class calculatorThread(QtCore.QThread):
        signal1 = QtCore.pyqtSignal(int, int)
        signal2 = QtCore.pyqtSignal(int,int) 
        signal3 = QtCore.pyqtSignal(int) 

        def __init__(self,input_file_txt, parameter_file_txt):
            QtCore.QThread.__init__(self)
            #and etc etc

在此线程的某个时刻,我正在呼叫:

At some point of this thread I am calling:

plots.stacked_barplot(*arguments)

一切正常,但是,我在屏幕上多次收到一条消息:

Everything works fine, however, I get a message on the screen many many times:

QPixmap: It is not safe to use pixmaps outside the GUI thread

想知道我在做什么错以及如何避免此消息.

Would like to know what am I doing wrong and how to avoid this message.

推荐答案

好吧,您从计算器线程发出plot命令,而计算器线程又使用QPixmap绘制绘图-全部从计算器线程内部进行.

Well, you issue the plot command from the calculator thread, which in turn uses a QPixmap to draw the plot - all from inside your calculator thread.

理想情况下,您不应该从计算器线程中进行绘图,例如发出准备好进行绘图的信号-并在主线程中进行绘图.也许遵循以下几行:

Ideally, you shouldn't draw from the calculator thread, but e.g. emit a signal that you're ready for plotting - and do the plot in the main thread. Maybe along the following lines:

class calculatorThread(QtCore.QThread):
    plot_emit = QtCore.pyqtSignal()

    def run(self):
        self.plot_args = your_calculation()
        self.plot_ready.emit()

在外部,将plot_ready信号连接到绘图命令:

Outside, connect the plot_ready signal to your plot command:

calculator.plot_emit.connect(lambda x: 
                             plots.stacked_barplot(*calculator.plot_args))

这篇关于QPixmap在GUI线程外不安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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