3

I am developing a software which has a virtual piano and that can be controlled by a midi keyboard. What I'm trying to do is I want a thread watching the midi inputs ans when there is a data coming corresponding process should be triggered ( here playing the sound and animating the key). How can I do it with Qt Threading and events?

Hemanth Raveendran
  • 157
  • 2
  • 3
  • 8
  • Create a thread. Emit a signal. If you need a more extensive answer, you will need to be more specific about what you have tried and what isn't working, as opposed to asking for a full generic example of how to use QThreads, and signals. – jdi Mar 23 '12 at 19:01
  • I've some push buttons as my piano keys. And I've done the program which computer keyboard is used to play these keys. And now I've to play this using my midi keyboard. so now I'm done it as by pressing a key the program will be entering to the midi reading loop,then trigger corresponding action. But here other Qt functions of the program not getting executed. So I just wanted to implement a seperate event that emit signal when there is a data in the midi port. How can i do with PyQt? – Hemanth Raveendran Mar 23 '12 at 19:36

2 Answers2

3

Here is a good page on how to use custom signals: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/new_style_signals_slots.html

And here is a page showing how to use QThread: http://joplaete.wordpress.com/2010/07/21/threading-with-pyqt4/

Thats pretty much all you need. You create the QThread with a run() function that will loop and monitor your midi port, and then emit a custom signal. You would start this thread with your application launch. And you would connect the QThread's custom signal that you created to a handlers on your main app or whatever widget should be notified.

David Renner
  • 454
  • 3
  • 15
jdi
  • 90,542
  • 19
  • 167
  • 203
0

Here you have small example:

import time
import sys

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import SIGNAL, QObject


class DoSomething(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        time.sleep(3)
        self.emit(SIGNAL('some_signal'))


def signalHandler():
    # We got signal!
    print 'Got signal!'
    sys.exit(0)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    # Create new thread object.
    d = DoSomething()

    # Connect signalHandler function with some_signal which 
    # will be emited by d thread object.
    QObject.connect(d, SIGNAL('some_signal'), signalHandler, QtCore.Qt.QueuedConnection)

    # Start new thread.
    d.start()

    app.exec_()
Adam
  • 2,254
  • 3
  • 24
  • 42