Repository: pyqtgraph/pyqtgraph

Issue:
GraphicsScene.mouseReleaseEvent exception when clicking a plot
<!-- In the following, please describe your issue in detail! -->
<!-- If some of the sections do not apply, just remove them. -->

I sometimes get an exception when clicking on the plot. 



### Short description
<!-- This should summarize the issue. -->

### Code to reproduce
clinking on the plot sometimes causes the issue

### Expected behavior
no error

### Real behavior
An exception is raised because `self.clickEvents` is an empty list at that time.

```
File "D:\TMP\daxil_venv_3.9\lib\site-packages\pyqtgraph\GraphicsScene\GraphicsScene.py", line 243, in mouseReleaseEvent
    self.clickEvents.remove(cev[0])
```

### Tested environment(s)

 * PyQtGraph version:  0.12.3
 * Qt Python binding: PyQt5 5.15.6
 * Python version: 3.9.4 x64
 * NumPy version: 1.21.2
 * Operating system: Windows 10 x64
 * Installation method: pip

### Additional context



Discussion:
Hi @danielhrisca 

Sorry nobody replied to your issue here; have you seen this.

I haven't seen this issue, and I'm puzzled as to why this error would occur.  The `cev` list is made up of elements _from_ `self.clickEvents`, the only method that is called before the `remove` call is `sendClickEvent`.

Is it possible in your code you're modifying `self.clickEvents` by some code that is tied to the `sigMouseClicked` call?

I'm going to close out this issue as we're unable to reproduce, but if you can provide an example that reproduces this error, we would very much like to know!
Hi @danielhrisca 

Please check out the discussion here: https://github.com/pyqtgraph/pyqtgraph/pull/2580#issuecomment-1399292221

Someone else ran into the same issue, we were able to troubleshoot it to an extra `QApplication.processEvents()` call in the user code.  Now that I'm confident that the bug here is not in the pyqtgraph library, we will be demoting the unhandled exception to a warning.  If you're still running into this issue, I would consider looking into `QApplication.processEvents()` calls in your code-base and seeing if you can do away with them.
Hello @j9ac9k , thank you for the heads-up. I certainly have some places were `QApplication.processEvents()` is called in my code.

I think the PR handles the problem 
That PR won't be merged in its current form; I've requested a change which would replace the ValueError with a RuntimeWarning (which should address the unhandled exception but still cause irritation if if you monitor stdout :D 

Resolving patch:
diff --git a/pyqtgraph/GraphicsScene/GraphicsScene.py b/pyqtgraph/GraphicsScene/GraphicsScene.py
--- a/pyqtgraph/GraphicsScene/GraphicsScene.py
+++ b/pyqtgraph/GraphicsScene/GraphicsScene.py
@@ -1,3 +1,4 @@
+import warnings
 import weakref
 from time import perf_counter, perf_counter_ns
 
@@ -220,10 +221,18 @@ def mouseReleaseEvent(self, ev):
                 cev = [e for e in self.clickEvents if e.button() == ev.button()]
                 if cev:
                     if self.sendClickEvent(cev[0]):
-                        #print "sent click event"
                         ev.accept()
-                    self.clickEvents.remove(cev[0])
-                
+                    try:
+                        self.clickEvents.remove(cev[0])
+                    except ValueError:
+                        warnings.warn(
+                            ("A ValueError can occur here with errant "
+                             "QApplication.processEvent() calls, see "
+                            "https://github.com/pyqtgraph/pyqtgraph/pull/2580 "
+                            "for more information."),
+                            RuntimeWarning,
+                            stacklevel=2
+                        )
         if not ev.buttons():
             self.dragItem = None
             self.dragButtons = []


Test patch: