Reputation: 1561
I'm working on a project where I'm attempting to get a QMesssageBox to exit with the "accepted" condition in response to incoming MIDI data. The MIDI input library (pygame.midi) needs to poll the input to see if any data has arrived, so I start a QThread to handle this, and have it emit a "dataReceived" signal when data arrives in the buffer. I then attach this signal to the QMessageBox's accept() slot:
def midiLearn(self, mainWindowInstance, widget):
class midiLearnWait(QtCore.QThread):
dataReceived = QtCore.pyqtSignal()
def __init__(self, midiInputDevice, parent=None):
super(midiLearnWait, self).__init__(parent)
self.midiInputDevice = midiInputDevice
def run(self):
if self.midiInputDevice.poll():
self.dataReceived.emit()
if self.midiInputDevice:
midiLearnMessage = QtGui.QMessageBox(1, 'MIDI Learn', 'Please move a controller.',
QtGui.QMessageBox.Cancel)
midiInputThread = midiLearnWait(self.midiInputDevice)
#just trigger accept for testing
midiInputThread.dataReceived.connect(lambda: midiLearnMessage.accept())
midiInputThread.start()
ret = midiLearnMessage.exec_()
if ret == QtGui.QMessageBox.Cancel:
return
else:
QtGui.QMessageBox.warning(mainWindowInstance, 'MIDI Error', 'No MIDI input selected.')
Unfortunately, this doesn't seem to work - the message box never gets accepted when MIDI data gets sent to the program. I am not completely sure at this point if the problem is something to do with how I've configured the MIDI library, or in how I've done this GUI code. If anyone could point out any errors in how I've attempted to set up the GUI aspect of the code it would be much appreciated.
Upvotes: 0
Views: 769
Reputation: 69082
midiInputDevice.poll()
shouldn't be a blocking call, so your thread runs once when started and immediately exits... and probably the poll call will return false, that's why the box stays there.
you'll either have to use midiInputDevice.read()
(which should block), or poll the device in a loop until there is some data.
Upvotes: 1