Search code examples
pythonpyqtqpixmap

I am not using QPixmap, in PyQt. but I get QPixmap: It is not safe to use pixmaps outside the GUI thread in PyQt


I am using PyQt for a project. But not all of a sudden I am getting an error:

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

I am not using QPixmap anywhere in my code... please help.

class itemCheckBtn(QtGui.QDialog):
qApp = None;
okCallback = None;
def __init__(self,parent=None):
    itemCheckBtn.qApp=None;
    QtGui.QWidget.__init__(self, None)
    self.ui = Ui_merchantPriceFrom();
    self.ui.setupUi(self)
    QtCore.QObject.connect(self.ui.itemCheckButton, QtCore.SIGNAL("clicked()"), self.submit)
def submit(self):
    print "Hi";

The main class is

class MyForm(QtGui.QMainWindow):
  serverThreadObject = None;
  qApp = None;
  sock = None;
  def __init__(self, qApp,parent=None):
    MyForm.qApp=qApp;
    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_bluwavemerchantmain()
    self.ui.setupUi(self)
    self.ui.server_connection_status_label.setText("Server Offline..");
    QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.connectUser )
    QtCore.QObject.connect(self.ui.actionStart_Server, QtCore.SIGNAL("triggered()"), self.startServer);
    QtCore.QObject.connect(self.ui.actionStop_Server, QtCore.SIGNAL("triggered()"), self.stopServerFromGui);
    QtCore.QObject.connect(self.ui.actionExit, QtCore.SIGNAL("triggered()"), self.closeEventFromMenu);
    QtCore.QObject.connect(self, QtCore.SIGNAL("triggered()"), self.closeEvent);

i am getting the error when I try to call the class "itemCheckBtn" from the "MyForm" class.


Solution

  • It looks like you are using threads, and somehow you are trying to change the GUI from some thread other than the main GUI thread (this is not allowed). This could be happening somewhat indirectly--for example your server thread calls a function on MyForm, which tries to update the itemCheckBtn. Even though the code is part of MyForm, it is still being executed from the server thread. Instead, you need to use some thread-safe mechanism to inform the GUI thread that a change has occurred, and let it do the GUI work. (see http://doc.qt.nokia.com/4.6/threads-qobject.html)