Search code examples
pythontimepyqt4maya

What's wrong with this code? Time does not update


This is my code. The code does not give any errors but the problem is that time does not update and 50% of my CPU is taking by maya like maya's "scriptjob -idle flag", I hope I will get an answer here whereas I am new to this site for asking questions.

from PyQt4 import QtGui, QtCore
import maya.OpenMayaUI as mui
import sip

def maya_main_window():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

class RenamingDialog(QtGui.QDialog):
    def __init__(self, parent= maya_main_window()):
        QtGui.QDialog.__init__(self,parent)

        self.setWindowTitle("Clock")
        self.setFixedSize(250, 200)

        self.createLayout()
        self.button_update()

        self.run_time()        

    def run_time(self):
        self.timer = QtCore.QTimer(self)
        self.connect(self.timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("update_time()"))
        self.timer.start()

    def createLayout(self):
        self.button1 = QtGui.QPushButton("Ok")
        self.label = QtGui.QLabel("time")

        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addWidget(self.button1)
        buttonLayout.addWidget(self.label)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addLayout(buttonLayout)
        self.setLayout(mainLayout)

    def update_time(self):      
        self.time = QtCore.QTime.currentTime()
        self.updater = QtCore.QString(self.time.toString("hh:mm:ss"))
        self.label.setText(self.updater)

    def button_update(self):
        self.connect(self.button1, QtCore.SIGNAL("clicked()"), self.printer)

    def printer(self):
        print "hai",

if __name__ == "__main__":
    dialog = RenamingDialog()
    dialog.show()

Thank you.


Solution

  • If you have a python slot, you can specify it just by tipping the method: self.update_time. This works for all methods accessible by Python.

    self.connect(self.timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("update_time()"))
    

    should be

    self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.update_time)
    

    anyway you can use the newer connection way:

    self.timer.timeout.connect(self.update_time)
    

    if you want to use your original syntax, you have to decorate your method update_time with decorator @pyqtSlot