Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Software triggered event



  • I am running PyQt5 on a Raspberry PI 3B+. There are some add on boards that trigger an interrupt in response to some external events. Presently the interrupt service routine needs to read some information from the external hardware to determine the cause of the interrupt but this causes problems if the interrupt occurs when the code is in the process of reading or writing to the hardware. I would like to change the ISR to simple trigger and event with a callback that is handled by the main task handler, in other words an event that can be manually triggered in software. How do I do this in PyQt?

    # the ISR would look like this
    def externalINT(self):
         triggerMyEvent()
    
    #the event handler would be called by the main event handler
    def myEventHandler(self):
         readIntCause()  
         #etc
    

    Thanks.


  • Lifetime Qt Champion

    Hi
    Do you mean like a custom event ?

    Your handler must be in a QObject class.

    sorry Im a python noob so cant type it.

    hopefully the c++ version is clear enough

      const QEvent::Type MyEvent = (QEvent::Type)1234;
        ...
        QApplication::postEvent(obj, new QCustomEvent(MyEvent));
        
    The event must be of type QCustomEvent (or a subclass). The argument to the constructor is the type of event. Values under 1024 are reserved by Qt for predefined event types; other values can be used by applications.
    
    To handle custom event types, reimplement the customEvent() function:
    
        void yourwidget::customEvent(QCustomEvent *event)
        {
            if (event->type() == MyEvent) {
               // do it!
            } else {
                baseeclass::customEvent(event);
            }
        }
        
    


  • That sounds like exactly what i need but I am also a Python noob so not sure how to pull it off.



  • Searching for "custom event" resulted in finding this: http://zetcode.com/gui/pyqt5/eventssignals/

    The example entitled "Emitting Signals" looks promising. I'll post an update after I give it a try but would appreciate any additional suggestions.

    Thanks


  • Lifetime Qt Champion

    @dtruex
    Well the normal event functions are very much
    like the customEvent so you would overwrite it in same manner.

    like

    
    class Example(QWidget):
            
        def customEvent(self, e):
         ...
    

    (i would assume )

    Anyway, the signal section seems to be a normal Qt signals which is not like an event in Qt as such.
    But signals and slots could also be used.

    Did you read
    https://doc.qt.io/qtforpython/overviews/signalsandslots.html
    Im aware you are using PyQt but the overall design etc is the same.


  • Banned

    Okay here is an article that seems to cover this question

    http://zetcode.com/gui/pyqt5/eventssignals/



  • @dtruex

    @dtruex said in Software triggered event:

    Searching for "custom event" resulted in finding this: http://zetcode.com/gui/pyqt5/eventssignals/

    The example entitled "Emitting Signals" looks promising. I'll post an update after I give it a try but would appreciate any additional suggestions.

    Thanks

    The approach suggested in the article in the link above solved the problem.

    class Communicate(QObject):
        intSig = pyqtSignal() 
    
    class MainWindow(QMainWindow):    
        self.c = Communicate()
        self.c.intSig.connect(self.piPlateINT2)
    etc
    
    #ISR
    def piPlateINT(self,channel):
            self.c.intSig.emit()
    
    #the code 
    def piPlateINT2(self):
            self.buttonIntCount = self.buttonIntCount + 1
    etc
    

Log in to reply