Repository: pyqtgraph/pyqtgraph

Issue:
Arrow keys in ImageView Without Time Axis
<!-- In the following, please describe your issue in detail! -->
<!-- If some of the sections do not apply, just remove them. -->

### Short description
When you type an arrow key in to a ImageView without a time axis, the application crashes with a unhandled exception.

### Code to reproduce
```
from PySide2.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PySide2.QtCore import Qt

import numpy as np
import pyqtgraph as pg

class BugWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(800, 800)
        self.imv = pg.ImageView()
        self.setCentralWidget(self.imv)



app = QApplication([])
win = BugWindow()
win.show()

app.exec_()

```


### Expected behavior
Nothing, 

### Real behavior
An unhandled exception occurs.

```
Traceback (most recent call last):
  File "D:\Repositories\Art\pyqtgraph\pyqtgraph\imageview\ImageView.py", line 452, in keyPressEvent
    self.evalKeyState()
  File "D:\Repositories\Art\pyqtgraph\pyqtgraph\imageview\ImageView.py", line 476, in evalKeyState
    self.jumpFrames(1)
  File "D:\Repositories\Art\pyqtgraph\pyqtgraph\imageview\ImageView.py", line 516, in jumpFrames
    if self.axes['t'] is not None:
KeyError: 't'

```

### Tested environment(s)

 * PyQtGraph version: 0.11.1.dev0/commit: cd914495ece
 * Qt Python binding: 'PySide2 5.13.2 Qt 5.12.9'
 * Python version: 3.9.1
 * NumPy version: '1.19.5'
 * Operating system:  Windows 10
 * Installation method: conda and git

### Additional context
I have a patch at https://github.com/pknodle/pyqtgraph/tree/image-display-bugfix
I'm not sure if this fix affects any other part of the system.


Discussion:
From a read of ImageView.py, it appears that all axes, including the time
axis, are created upon calling setImage(), which your minimal example does
not do.

So the issue is more that the keys processing code does not check for the
presence of an image.


On Sat, 6 Feb 2021, 06:48 pknodle, <notifications@github.com> wrote:

> Short description
>
> When you type an arrow key in to a ImageView without a time axis, the
> application crashes with a unhandled exception.
> Code to reproduce
>
> from PySide2.QtWidgets import QMainWindow, QAction, qApp, QApplication
> from PySide2.QtCore import Qt
>
> import numpy as np
> import pyqtgraph as pg
>
> class BugWindow(QMainWindow):
>     def __init__(self):
>         QMainWindow.__init__(self)
>         self.resize(800, 800)
>         self.imv = pg.ImageView()
>         self.setCentralWidget(self.imv)
>
>
>
> app = QApplication([])
> win = BugWindow()
> win.show()
>
> app.exec_()
>
>
> Expected behavior
>
> Nothing,
> Real behavior
>
> An unhandled exception occurs.
>
> Traceback (most recent call last):
>   File "D:\Repositories\Art\pyqtgraph\pyqtgraph\imageview\ImageView.py", line 452, in keyPressEvent
>     self.evalKeyState()
>   File "D:\Repositories\Art\pyqtgraph\pyqtgraph\imageview\ImageView.py", line 476, in evalKeyState
>     self.jumpFrames(1)
>   File "D:\Repositories\Art\pyqtgraph\pyqtgraph\imageview\ImageView.py", line 516, in jumpFrames
>     if self.axes['t'] is not None:
> KeyError: 't'
>
>
> Tested environment(s)
>
>    - PyQtGraph version: 0.11.1.dev0/commit: cd91449
>    <https://github.com/pyqtgraph/pyqtgraph/commit/cd914495ece08bdbe6fd47698f3835f922cda599>
>    - Qt Python binding: 'PySide2 5.13.2 Qt 5.12.9'
>    - Python version: 3.9.1
>    - NumPy version: '1.19.5'
>    - Operating system: Windows 10
>    - Installation method: conda and git
>
> Additional context
>
> I have a patch at
> https://github.com/pknodle/pyqtgraph/tree/image-display-bugfix
> I'm not sure if this fix affects any other part of the system.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/pyqtgraph/pyqtgraph/issues/1545>, or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAUIWA6VOISUF2655MXVJXLS5RYUXANCNFSM4XFRX7VQ>
> .
>

With this example, it crashes with up/down, but not left/right.

```

from PySide2.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PySide2.QtCore import Qt

import numpy as np
import pyqtgraph as pg

class BugWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(800, 800)
        self.imv = pg.ImageView()
        junk_array = np.random.random((400,400))
        self.imv.setImage(junk_array)
        self.setCentralWidget(self.imv)



app = QApplication([])
win = BugWindow()
win.show()

app.exec_()

```

Resolving patch:
diff --git a/pyqtgraph/imageview/ImageView.py b/pyqtgraph/imageview/ImageView.py
--- a/pyqtgraph/imageview/ImageView.py
+++ b/pyqtgraph/imageview/ImageView.py
@@ -429,7 +429,10 @@ def close(self):
         self.setParent(None)
         
     def keyPressEvent(self, ev):
-        #print ev.key()
+        if not self.hasTimeAxis():
+            super().keyPressEvent(ev)
+            return
+
         if ev.key() == QtCore.Qt.Key_Space:
             if self.playRate == 0:
                 self.play()
@@ -454,6 +457,10 @@ def keyPressEvent(self, ev):
             super().keyPressEvent(ev)
 
     def keyReleaseEvent(self, ev):
+        if not self.hasTimeAxis():
+            super().keyReleaseEvent(ev)
+            return
+
         if ev.key() in [QtCore.Qt.Key_Space, QtCore.Qt.Key_Home, QtCore.Qt.Key_End]:
             ev.accept()
         elif ev.key() in self.noRepeatKeys:
@@ -756,7 +763,7 @@ def updateImage(self, autoHistogramRange=True):
             
     def timeIndex(self, slider):
         ## Return the time and frame index indicated by a slider
-        if self.image is None:
+        if not self.hasTimeAxis():
             return (0,0)
         
         t = slider.value()


Test patch: