How to handle mouse events in Qt? - qt

Dragging, panning and mouse tracking items in Qt scene are the 3 actions which I want to simultaneously use in my app. However, it is more difficult than I thought.
Example code :
from PySide.QtCore import *
from PySide.QtGui import *
class View(QGraphicsView):
"""# HOW TO ATTACH MOUSE EVENTS PROGRAMMATICALLY ?
def mouseMoveEvent(self, event):
print "View MOVE", event.pos()
"""
class Scene(QGraphicsScene):
pass
class CircleForTrackingSwitch(QGraphicsEllipseItem):
def __init__(self, scene):
super(CircleForTrackingSwitch, self).__init__()
self.scene = scene
self.view = self.scene.views()[0]
self.setRect(QRect(20,20,20,20))
self.scene.addItem(self)
def mousePressEvent(self, event):
if self.view.hasMouseTracking() :
self.view.setMouseTracking(False)
else :
self.view.setMouseTracking(True)
print "Circle View SetTrack", event.pos()
def mouseMoveEvent(self, event):
print "Circle MOVE", event.pos()
class DraggableRectangle(QGraphicsRectItem):
def __init__(self, scene):
super(DraggableRectangle, self).__init__()
self.scene = scene
self.setRect(QRect(-20,-20,40,40))
self.scene.addItem(self)
#self.setFlag(QGraphicsItem.ItemIsMovable, True)
def mousePressEvent(self, event):
print "Rectangle PRESS", event.pos()
def mouseMoveEvent(self, event):
print "Rectangle MOVE", event.pos()
self.setPos(event.pos())
def mouseReleaseEvent(self, event):
print "Rectangle RELEASE", event.pos()
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.s = Scene()
self.s.setSceneRect(-200,-100,300,300,)
self.v = View(self.s)
self.v.setDragMode(QGraphicsView.ScrollHandDrag)
self.setCentralWidget(self.v)
CircleForTrackingSwitch(self.s)
DraggableRectangle(self.s)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.resize(300, 200)
window.show()
sys.exit(app.exec_())
MASTER QUESTION :
How to attach mouse events programmatically? Many thanks.

For the draggable Rectangle, since the Rectangle is already moveable, you only have to subclass it, to overload the different mouse functions (with the track stuff), then to call the parent's mouse event. Basically, you could have what you want with this rectangle class:
class DraggableRectangle(QGraphicsRectItem):
def __init__(self, scene, *args, **kwargs):
super().__init__(*args, **kwargs)
self.scene = scene
self.setRect(QRect(-20,-20,40,40))
self.scene.addItem(self)
self.setFlag(QGraphicsItem.ItemIsMovable, True) # Keep that
def mousePressEvent(self, event):
print "Rectangle PRESS", event.pos()
super().mousePressEvent(event) # Call parent
def mouseMoveEvent(self, event):
print "Rectangle MOVE", event.pos()
# Do not move by yourself
# Call parent that already handles the move
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
print "Rectangle RELEASE", event.pos()
super().mouseReleaseEvent(event) # Call parent
I hope this is what you were looking for.

Related

How can I resize the window after a widget is hidden while still allowing the window to be resized?

Here is a simple example:
from PyQt4 import QtCore, QtGui
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
super(MainWindow, self).__init__()
self.button = QtGui.QPushButton('Show/Hide')
self.button.setCheckable(True)
self.frame = QtGui.QFrame()
self.frame.setFixedHeight(100)
layout = QtGui.QVBoxLayout()
layout2 = QtGui.QVBoxLayout()
self.setLayout(layout)
self.frame.setLayout(layout2)
layout.addWidget(self.button)
layout.addWidget(self.frame)
layout.addStretch(1)
layout2.addWidget(QtGui.QLabel('Yoyoyo'))
self.button.toggled.connect(self.clickAction)
def startup(self):
self.show()
sys.exit(self.app.exec_())
def clickAction(self):
checked = self.button.isChecked()
if checked:
self.frame.show()
else:
self.frame.hide()
if __name__ == "__main__":
myApp = MainWindow()
myApp.startup()
When the button is toggled, the frame is shown/hidden appropriately. I realize if I set the MainWindows sizeConstraint to SetFixedSize as follows:
layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
that the window will resize like I want. However, I wish for the user to still be able to resize the window, which SetFixedSize will not allow me to do.
Therefore, how can I achieve both a user-resizeable window while still having it resize based on it's contents?
Based on the conversation found here I was able to get this to work. The solution involved adding a QTimer after showing/hiding the frame. The QTimer calls a resizeMe method, which is also defined below.
from PyQt4 import QtCore, QtGui
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
super(MainWindow, self).__init__()
self.button = QtGui.QPushButton('Show/Hide')
self.button.setCheckable(True)
self.frame = QtGui.QFrame()
self.frame.setFixedHeight(100)
self.layout = layout = QtGui.QVBoxLayout()
layout2 = QtGui.QVBoxLayout()
self.setLayout(layout)
self.frame.setLayout(layout2)
layout.addWidget(self.button)
layout.addWidget(self.frame)
layout.addStretch(1)
layout2.addWidget(QtGui.QLabel('Yoyoyo'))
self.button.toggled.connect(self.clickAction)
def startup(self):
self.show()
sys.exit(self.app.exec_())
def clickAction(self):
checked = self.button.isChecked()
if checked:
self.frame.show()
else:
self.frame.hide()
QtCore.QTimer.singleShot(0, self.resizeMe)
def resizeMe(self):
self.resize(self.minimumSizeHint())
if __name__ == "__main__":
myApp = MainWindow()
myApp.startup()

python3.4 + pyqt5: Getting x,y out of QMouseEvent

python3.4+pyqt5
I'm trying to write a program in which buttons shall create other buttons by dragging and press_releasing to form a representation of a rolling mill street.
I created a class Button which does the moving and handles several clicking events for the buttons in creation. This part is working fine.
Where I'm stuck is, I want to read the mouse position where these button should be created.
Here's the code:
def createConnects(self):
self.id000001.released.connect(self.do_something)
self.id000002.released.connect(self.do_something)
#QtCore.pyqtSlot()
def do_something(self):
self.bname = 'Button'
self.button = Button(self.bname, self)
posx = QtGui.QMouseEvent.x()
print(posx)
posy = QtGui.QMouseEvent.y
print(posy)
sender = self.sender()
print(str(sender.objectName()))
#self.button.move(posx, posy)
qmouseevent provides several functions, but the posx line gives me this error:
TypeError: QMouseEvent.x(): first argument of unbound method must have type 'QMouseEvent'
posy-line gives < built-in function y > which isn't what I want, too, but clear.
MouseTracking is switched on in MainWindow class.
Maybe, normaly one would do that by using event in the def line but since it's a slot, that would lead to other problems.
Any hints to get along?
Update:
As recommended here's the full code of the prototype:
#!/usr/bin/env python3.4
import sys, os, math, shutil, re
from subprocess import call, Popen
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUiType
Ui_MainWindow, QMainWindow = loadUiType('mainwindow.ui')
def main(argv):
app = QtWidgets.QApplication(argv)
mainwindow = MyMainWindow()
mainwindow.show()
sys.exit(app.exec_())
class Button(QtWidgets.QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
self.button = QtWidgets.QPushButton()
def mouseMoveEvent(self, event):
if event.buttons() != QtCore.Qt.RightButton:
return
mimeData = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(event.pos() - self.rect().topLeft())
dropAction = drag.exec_(QtCore.Qt.MoveAction)
def mousePressEvent(self, event):
QtWidgets.QPushButton.mousePressEvent(self, event)
if event.button() == QtCore.Qt.LeftButton:
print(event.button(),' pressed')
class MyMainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self, *args):
QtWidgets.QMainWindow.__init__(self, *args)
self.setupUi(self)
self.setMouseTracking(True)
self.createConnects()
def dragEnterEvent(self, event):
event.accept()
def dropEvent(self, event):
position = event.pos()
self.button.move(position)
event.setDropAction(QtCore.Qt.MoveAction)
event.accept()
def createConnects(self):
self.id000001.released.connect(self.do_something)
self.id000002.released.connect(self.do_something)
#QtCore.pyqtSlot()
def do_something(self):
print('do_something')
self.bname = 'Button'
self.button = QtWidgets.QPushButton()
#self.button = Button(self.bname, self)
self.button.move(100, 65)
#posx = QtGui.QMouseEvent.x()
#print(posx)
#posy = QtGui.QMouseEvent.y
#print(posy)
sender = self.sender()
print(str(sender.objectName()))
#self.button.move(posx, posy)
if __name__ == '__main__':
main(sys.argv)
cheers,
Christian

Why does clicking a QToolButton generate a leaveEvent? Is there a workaround?

For some reason, whenever the menu-button part of the QToolButton gets clicked, it generates a momentary leaveEvent (at least when it is in a toolbar). I even tested underMouse() in the leaveEvent, and it returns false. Why is this? Is there a way to fix this?
Sample for testing (Py3.3, change super() for py2.7):
from PyQt4.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
toolbar = QToolBar(self)
toolbar.addWidget(ToolButton())
class ToolButton(QToolButton):
def __init__(self, parent=None):
super().__init__(parent)
self.setText('test')
self.setPopupMode(QToolButton.MenuButtonPopup)
self.setMenu(QMenu())
self.menu().addAction('Stub')
def enterEvent(self, event):
print('entered')
super().enterEvent(event)
def leaveEvent(self, event):
print('left')
super().leaveEvent(event)
if __name__ == '__main__':
import sys
application = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(application.exec_())
The following can be used to double check; unlike leaveEvent, it always returns the correct information:
def leaveEvent(self, event):
if not QApplication.widgetAt(QCursor().pos()) is self:
#do stuff
super().leaveEvent(event)

QPushButton won't fire clicked slot (method of QObject)

I have a QObject, which works as a controller.
This QObject has a reference to a QPushButton.
This QOjbect has a method set to be fired upon QPushButton's clicked event.
Ex:
class MyController(QObject):
def __init__(self, parent=None):
super(MyController, self).__init__(parent)
self.some_ref = ....
self.button = self.some_ref.button (returns QPushButton)
self.button.clicked.connect(self.button_clicked)
# #Slot(type)
def button_clicked(self):
print 'button clicked: ', self.sender()
# print 'button clicked (no sender req.)
Here, the button_clicked won't get fired.
I tried decorating it with #Slot(), #Slot(QObject), #Slot(type), #Slot(str), #Slot(int) but still won't work.
What am I doing wrong?
If I use ..clicked.connect(lambda: self.button_clicked) it of course works. So I assume this is a type mismatch but shouldn't #Slot(..) decoration have fixed it?
Thank you.
I don't know if the problem is that #Slot() is commented (have a # at the beginning), but this code works for me (it's in python 3, but just change the print line)
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.button = QPushButton()
self.button.setText("Test")
self.setCentralWidget(self.button)
def GetButton(self):
return self.button
class MyController(QObject):
def __init__(self, parent=None):
super(MyController, self).__init__(parent)
self.button = parent.GetButton() #(returns QPushButton)
self.button.clicked.connect(self.button_clicked)
#Slot()
def button_clicked(self):
print('button clicked: ', self.sender())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
controller = MyController(window)
window.show()
app.exec_()
sys.exit(0)
May be could try the released signal instead of the clicked signal, because the clicked signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button).
Or you could try the #method 2 of connecting the signals.
class MyController(QObject):
def __init__(self, parent=None):
super(MyController, self).__init__(parent)
self.some_ref = ....
self.button = self.some_ref.button
# method 1
self.button.released.connect(self.button_clicked)
# method 2
self.connect(self.button, SIGNAL('released()'), self.button_clicked)
def button_clicked(self):
print "yipee it works..."

Qt Line drawing to unexpected location

I'm trying to write a drawing program but I'm having problems with drawing lines. When I draw a line on the bottom portion of the QGraphicsView the line is drawn to the center of the widget. Why? I don't think I understand the mapTo functions well enough, but the more I read the Qt docs the more confused I get. Hope someone can help.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class Main(QWidget):
def __init__(self, parent):
super(Main, self).__init__(parent)
self.resize(300, 300)
vBox = QVBoxLayout(self)
view = View(self)
vBox.addWidget(view)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
sys.exit()
class View(QGraphicsView):
def __init__(self, parent):
super(View, self).__init__(parent)
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
def mousePressEvent(self, event):
self.start = event.pos()
def mouseReleaseEvent(self, event):
self.stop = event.pos()
self.line = Line(self, self.start, self.stop)
self.scene.addItem(self.line)
class Line(QGraphicsLineItem):
def __init__(self, parent, *args):
# args = start, stop
points = map(parent.mapToScene, args)
(start, stop) = map(QPointF, points)
self.line = QLineF(start, stop)
super(Line, self).__init__(self.line)
def run():
app = QApplication(sys.argv)
a = Main(None)
a.show()
sys.exit(app.exec_())
run()
If you don't set a rect for your scene, one will be calculated automatically and the view will centre itself on the objects within it.
To fix this, add the following to the end of View.__init__():
self.setSceneRect(QRectF(self.viewport().rect()))

Resources