PDA

View Full Version : simultaneous keypress events and disabling related checkboxes in PyQt5



oudent
5th December 2016, 22:49
First off, sorry, I am very new to this and as such I know this isn't a well laid out post, but I've been trying to figure out this problem for months (when ever I get the chance to look at the code, so once every month or two).
I'm working with PyQt5, the same issue occurred in PyQt4, and I suspect C++ Qt experience will be very helpful.

I have a number of checkboxes (actually setup as QtPushButtons in a button group), when some of them are clicked other options need to be disabled (not all options are compatible). Works great with mouse clicks BUT the problem is when they are accessed by keyboard shortcuts. Its possible to hit 2 keys at once and both "check" and disable options at the same time (and than you can't uncheck them, see image).

12239

In the image attached, you can see that the two observations "Absent" and "Large" are checked (and how can something both be large and absent?! I'm a biologist not a quantum physicist), but everything is now "checkboxItem.setEnables(False)". It seems that the event handler is possibly multi-threaded or something? I didn't have this issue when I made a similar form with HTML/javascript.

Really really simplified version of code:


...
self.qualityGroup.buttonClicked[QtWidgets.QAbstractButton].connect(self.setQuality)

def setQuality(self, qualityClicked):
disableIncompatibleObservations(qualityClicked.obj ectName()) #looks up the option clicked, and sets all incompatible checkboxes/buttons to setEnable(False)
....



Any suggestions how I can disable other checkboxes (or again, in my case, checkable buttons) but do so without any chance of two events conflicting with each other? This system is being designed to record a number of observations quickly from the numpad.

I'm new to PyQt, plus I'm a biologist not a programmer...but I've done a bit of coding in Python, Java, C, C++, VBA, R, HTML/CSS/PHP, etc. So I know enough to get myself in trouble.

Added after 55 minutes:

Quick update, I think the issue is partially resolved by using PyQt5, be adding a quick check to see if the checkbox/button is enabled. Then I can "uncheck" is programmatically and keep going. It is a bit of a workaround, but it works I think... I'm not sure if this just makes it less likely to happen (depending how quickly things happen) or actually solves the problem.


def setQuality(self, qualityClicked):
if not qualityClicked.isEnabled():
qualityClicked.setChecked(False)
print("Attempted to set quality for disabled option")
else:
disableIncompatibleObservations(qualityClicked.obj ectName()) #looks up the option clicked, and sets all incompatible checkboxes/buttons to setEnable(False)

anda_skoa
6th December 2016, 09:00
Usually events in Qt do not happen at the same time, the event loop always processes one after another.

From the description it sounds a bit as if the disableIncompatibleObservations() function is missing some consistency checks or does not enable all checkboxes before it starts disabling the incompatible ones.

Cheers,
_