Reputation: 3
I wanted to implement a GUI where the image gets updated each time a keypress event happens, here is an attempt but the image is not updated.
class Test(QMainWindow):
def __init__(self, path):
super().__init__()
self.path = path
self.sliceno = 40
self.initUI()
def vol(self):
slices = [1,2,3,4,5,6]
return slices
def keyPressEvent(self, event):
key = event.key()
if event.key() == QtCore.Qt.Key_Q: #Event
self.sliceno = self.sliceno + 1
def initUI(self):
im = np.uint8(self.vol()[self.sliceno]) #should change the index when 'q' pressed
qimage = QImage(im, im.shape[1], im.shape[0], QImage.Format_Grayscale8)
pixmap = QPixmap(qimage)
pixmap_label.setPixmap(pixmap)
self.setCentralWidget(pixmap_label)
self.show()
Upvotes: 0
Views: 104
Reputation: 244003
You have to get the new QPixmap so you have to create a function that updates:
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q: # Event
self.sliceno = self.sliceno + 1
self.change()
def initUI(self):
self.pixmap_label = QLabel()
self.change()
self.setCentralWidget(self.pixmap_label)
self.show()
def change(self):
im = np.uint8(self.vol()[self.sliceno])
qimage = QImage(im, im.shape[1], im.shape[0], QImage.Format_Grayscale8)
pixmap = QPixmap.fromImage(qimage)
self.pixmap_label.setPixmap(pixmap)
Upvotes: 1