Я поместил подкласс (QDicomLabel_contrast), унаследованный от QLabel, в QWidget, и событие перемещения мыши в QDicomLabel_contrast нельзя вызвать обратно. Код:
from PyQt5.QtWidgets import *
import sys
class QDicomLabel_contrast(QLabel):
def __init__(self,parent=None):
super(QDicomLabel_contrast,self).__init__(parent)
self.setStyleSheet("background-color:black")
self.setMouseTracking(True)
def mouseMoveEvent(self, QMouseEvent):
pos = QMouseEvent.pos()
print(pos.x(), pos.y())
if __name__ == '__main__':
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(500,500)
self.imagelabel = QDicomLabel_contrast()
self.imagelabel.resize(300,300)
self.imagelabel.setParent(self)
#self.imagelabel.show()
self.imagelabel.setGeometry(10, 20, 300,300)
widget = QWidget()
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
событие mouseMoveEvent в QDicomLabel_contrast не может быть вызвано. Однако, если я использую self.imagelabel.show() вместо self.imagelabel.setParent(self), может быть вызвано событие mouseMoveEvent, как: from PyQt5.QtWidgets import * import sys
class QDicomLabel_contrast(QLabel):
def __init__(self,parent=None):
super(QDicomLabel_contrast,self).__init__(parent)
self.setStyleSheet("background-color:black")
self.setMouseTracking(True)
def mouseMoveEvent(self, QMouseEvent):
pos = QMouseEvent.pos()
print(pos.x(), pos.y())
if __name__ == '__main__':
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(500,500)
self.imagelabel = QDicomLabel_contrast()
self.imagelabel.resize(300,300)
#self.imagelabel.setParent(self)
self.imagelabel.show()
self.imagelabel.setGeometry(10, 20, 300,300)
widget = QWidget()
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()