Results 1 to 4 of 4

Thread: how to setup simple mouse event

  1. #1
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default how to setup simple mouse event

    Hello,
    Continuing on my studying of Qt, Id like to learn about MouseEvents and Events in general.

    for testing i have a super basic window with just a single label on it.
    I've turned on mouse tracking as per the docs instruction, but i dont know how to capture the signals. i dont see onMouseOver / Hover.

    what I am trying to do:

    when my mouse is over the label, it executes a function 'changedText' that changes the text to 'yay'

    ...
    self.myLabel.setMouseTracking(True)
    self.myLabel.setText('put mouse is over the label')


    #function to call
    def changeText():
    self.myLabel.setText(' YaY!')


    Cheers!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to setup simple mouse event


  3. #3
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: how to setup simple mouse event

    I found this presentation really useful.

    http://www.comp.nus.edu.sg/~cs3249/l...processing.pdf

  4. #4
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to setup simple mouse event

    Thanks!

    For anyone that's interested, I was able to implement enter & leave event by subclassing the widget like this:

    Qt Code:
    1. from PyQt4 import QtGui, QtCore
    2. from PyQt4.QtCore import pyqtSignal
    3. import os,sys
    4.  
    5.  
    6. class Main(QtGui.QWidget):
    7.  
    8. def __init__(self, parent=None):
    9. super(Main, self).__init__(parent)
    10.  
    11. layout = QtGui.QVBoxLayout(self) # layout of main widget
    12.  
    13. button = HoverButton(self)
    14. button.setIconSize(QtCore.QSize(200,200))
    15.  
    16. layout.addWidget(button) # set your button to the widgets layout
    17. # this will size the button nicely
    18.  
    19.  
    20. class HoverButton(QtGui.QToolButton):
    21.  
    22. def __init__(self, parent=None):
    23. super(HoverButton, self).__init__(parent)
    24. self.setMouseTracking(True)
    25.  
    26. def enterEvent(self,event):
    27. print("Enter")
    28. self.setStyleSheet("background-color:#45b545;")
    29.  
    30. def leaveEvent(self,event):
    31. self.setStyleSheet("background-color:yellow;")
    32. print("Leave")
    33.  
    34. app = QtGui.QApplication(sys.argv)
    35. main = Main()
    36. main.show()
    37. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 17th April 2016 at 23:53. Reason: changed [quote] to [code]

Similar Threads

  1. Ogre+qt mouse event (add object with mouse problem)
    By rimie23 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2012, 11:49
  2. Replies: 14
    Last Post: 17th January 2012, 10:01
  3. Replies: 3
    Last Post: 7th January 2012, 09:38
  4. Mouse Event - source of event?
    By PowerPlate in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2011, 10:46
  5. Replies: 3
    Last Post: 12th May 2010, 14:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.