python - PyQt5: Check if mouse is held down in enter-event -


my actual application more complicated this, example below sums majority of problem. have multiple qlabels i've subclassed make them clickable. labels display 16x16 images requires process of loading images via pillow, converting them imageqt objects, , setting pixmap of label. in example, have 3 clickable qlabels run print_something function each time click on them. goal able hold mouse down, , each label hover over, function gets called. pointers great.

from pyqt5 import qtcore, qtwidgets, qtgui pil import image pil.imageqt import imageqt import sys   class clickablelabel(qtwidgets.qlabel):      def __init__(self):         super().__init__()      clicked = qtcore.pyqtsignal()      def mousepressevent(self, ev):         if app.mousebuttons() & qtcore.qt.leftbutton:             self.clicked.emit()   class mainwindow(qtwidgets.qmainwindow):      def __init__(self):         super().__init__()         central_widget = qtwidgets.qwidget()          self.setfixedsize(300, 300)         image = image.open("16x16image.png")         image_imageqt = imageqt(image)          hbox = qtwidgets.qhboxlayout()         hbox.setspacing(0)         hbox.addstretch()          label01 = clickablelabel()         label01.setpixmap(qtgui.qpixmap.fromimage(image_imageqt))         label01.clicked.connect(self.print_something)         hbox.addwidget(label01)          label02 = clickablelabel()         label02.setpixmap(qtgui.qpixmap.fromimage(image_imageqt))         label02.clicked.connect(self.print_something)         hbox.addwidget(label02)          label03 = clickablelabel()         label03.setpixmap(qtgui.qpixmap.fromimage(image_imageqt))         label03.clicked.connect(self.print_something)         hbox.addwidget(label03)          hbox.addstretch()          central_widget.setlayout(hbox)          self.setcentralwidget(central_widget)      def print_something(self):         print("printing something..")   if __name__ == "__main__":     app = qtwidgets.qapplication(sys.argv)     main_window = mainwindow()     main_window.show()     sys.exit(app.exec_()) 

the cause of problem stated in docs qmouseevent:

qt automatically grabs mouse when mouse button pressed inside widget; widget continue receive mouse events until last mouse button released.

it not there simple way around this, hackish required. 1 idea initiate fake drag , use dragenterevent instead of enterevent. should work:

class clickablelabel(qtwidgets.qlabel):     clicked = qtcore.pyqtsignal()      def __init__(self):         super().__init__()         self.setacceptdrops(true)         self.dragstart = none      def mousepressevent(self, event):         if event.buttons() & qtcore.qt.leftbutton:             self.dragstart = event.pos()             self.clicked.emit()      def mousereleaseevent(self, event):         self.dragstart = none      def mousemoveevent(self, event):         if (self.dragstart not none ,             event.buttons() & qtcore.qt.leftbutton ,             (event.pos() - self.dragstart).manhattanlength() >              qtwidgets.qapp.startdragdistance()):             self.dragstart = none             drag = qtgui.qdrag(self)             drag.setmimedata(qtcore.qmimedata())             drag.exec_(qtcore.qt.linkaction)      def dragenterevent(self, event):         event.acceptproposedaction()         if event.source() not self:             self.clicked.emit() 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -