QTimer misses timeout signal

Hi, Matthias!

I tried to use QTimer in my macro, but it turns out that it missed timeout signal, looks like in code and definitely in documentation. I subclassed QTimer and reimplemented timerEvent as workaround.

I use KLayout 0.26.4.

Comments

  • Hi Eugene,

    could you paste some code so I see how you're doing this?

    Thanks,

    Matthias

  • Example is artificial, but it uses both methods.

    import pya
    
    class Subclassed_Timer(pya.QTimer):
    
        def __init__(self, parent, line_edit):
            super(Subclassed_Timer, self).__init__(parent)
            self.__count = 0
            self.__line_edit = line_edit
    
        def timerEvent(self, event):
            super(Subclassed_Timer, self).timerEvent(event)
            self.__count += 1
            self.__line_edit.setText('Subclassed {0}'.format(self.__count))
    
    class QTimer_Test_Dialog(pya.QDialog):
    
        def __init__(self, parent):
            super(QTimer_Test_Dialog, self).__init__(parent)
            self.__count = 0
    
            self.setWindowTitle('QTimer Test')
            self.resize(300, 200)
    
            vertical_layout = pya.QVBoxLayout(self)
            self.setLayout(vertical_layout)
    
            button = pya.QPushButton(self)
            button.setText('Start')
            button.pressed = self.button_pressed
            vertical_layout.addWidget(button)
    
            self.__line_edit_original = pya.QLineEdit(self)
            self.__line_edit_original.setText('Original 0')
            vertical_layout.addWidget(self.__line_edit_original)
    
            self.__line_edit_subclassed = pya.QLineEdit(self)
            self.__line_edit_subclassed.setText('Subclassed 0')
            vertical_layout.addWidget(self.__line_edit_subclassed)
    
            self.__timer_original = pya.QTimer(self)
            self.__timer_original.setInterval(500)
            self.__timer_original.timeout = self.timer_timeout
    
            self.__timer_subclassed = Subclassed_Timer(self, self.__line_edit_subclassed)
            self.__timer_subclassed.setInterval(500)
    
        def button_pressed(self):
            self.__timer_original.start()
            self.__timer_subclassed.start()
    
        def timer_timeout(self):
            self.__count += 1
            self.__line_edit_original.setText('Original {0}'.format(self.__count))
    
    def qtimer_test():
        main_window = pya.Application.instance().main_window()
        QTimer_Test_Dialog(main_window).exec_()
    
  • Hi Eugene,

    thanks for your code. I understand what you mean now.

    First of all, I'd suggest a small enhancement:

    def qtimer_test():
        main_window = pya.Application.instance().main_window()
        dialog = QTimer_Test_Dialog(main_window)
        dialog.exec_()
        dialog._destroy()
    

    Without the "_destroy", the QTimer_Test_Dialog stays alive because the object is owned by main window. If you re-run the macro you'll see an error message than happens because you redefine a class which is already in use.

    Your workaround is working nicely, but you're right, the timeOut signal should be there. It's there for Qt4, but not for Qt5. I'll check that. For your reference I have created a ticket for this: https://github.com/KLayout/klayout/issues/629

    Thanks and best regards,

    Matthias

  • Hi, Matthias!

    Thank you for suggestion! Actually my real code use show() (modal dialog could not be moved on Ubuntu). What is right place to call _destroy() in this case?

    Eugene.

  • I tried to call _destroy() in my other code that uses exec_(), but PyLint complains about access to protected member.

    Eugene.

  • Hi Eugene,

    For "show()" is pretty difficult to use "_destroy". When your dialog is opened and you re-run the macro, this is probably crash the application or cause weird effects.

    The issue is only there for debugging and development. If you're not using the integrated IDE, this is not a problem.

    I have found a way to map private events too, so the bug is fixed. I just don't know if I'll be able to backport this to the 0.26 branch.

    Best regards,

    Matthias

  • Hi, Matthias!

    Thank you for quick fix!

    Eugene.

Sign In or Register to comment.