I want mouse events to reach the appropriate QGraphicsItem but they get no further than the QGraphicsView.
I have reduced my code to 41 lines, commented out event handlers but to no avail. It is either handled by QGraphicsView if that has a handler or, if not, is not caught at all.
I'm sure I'm missing something obvious but I can't see it.
from PyQt5.QtWidgets import *
class MyFrame(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.setScene(QGraphicsScene())
self.scene().addItem(Group())
def mouseReleaseEvent(self, event):
print('in QGraphicsView')
return QGraphicsView.mouseReleaseEvent(self, event)
class Group(QGraphicsItemGroup):
def __init__(self, parent=None):
super().__init__()
item = MyEllipse(0, 0, 20, 20)
self.addToGroup(item)
def mouseReleaseEvent(self, event):
print('in QGraphicsItemGroup')
return QGraphicsItemGroup.mouseReleaseEvent(self, event)
class MyEllipse(QGraphicsEllipseItem):
def mouseReleaseEvent(self, event):
print('in QGraphicsEllipseItem')
return QGraphicsEllipseItem.mouseReleaseEvent(self, event)
if __name__ == '__main__':
app = QApplication([])
f = MyFrame()
f.show()
app.exec_()
mouseReleaseEvent is called if and only if the event that the mousePressEvent handles is accepted, so with the following code the event will arrive at QGraphicsItemGroup:
class Group(QGraphicsItemGroup):
def __init__(self, parent=None):
super().__init__()
item = MyEllipse(0, 0, 20, 20)
self.addToGroup(item)
def mousePressEvent(self, event):
QGraphicsItemGroup.mousePressEvent(self, event)
event.accept()
def mouseReleaseEvent(self, event):
print('in QGraphicsItemGroup')
QGraphicsItemGroup.mouseReleaseEvent(self, event)
But as #ekhumoro points out, the QGraphicsItemGroup acts as a single element so events are not transported to the items they handle.
If you want to detect when the item is pressed you can use the following method:
class Group(QGraphicsItemGroup):
def __init__(self, parent=None):
super().__init__()
self._item = MyEllipse(0, 0, 20, 20)
self.addToGroup(self._item)
def mousePressEvent(self, event):
QGraphicsItemGroup.mousePressEvent(self, event)
event.accept()
def mouseReleaseEvent(self, event):
print('in QGraphicsItemGroup')
if self._item.mapToParent(self._item.boundingRect()).containsPoint(event.pos(), Qt.OddEvenFill):
print("_item")
QGraphicsItemGroup.mouseReleaseEvent(self, event)
Related
How can I detect mouse clicks in QWebEngineView widget?
I tried this but doesn't work:
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.view.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == event.MouseButtonPress:
print ("Widget click")
return super(QtWidgets.QMainWindow, self).eventFilter(obj, event)
Assuming that the view is the QWebEngineView object and you want to track its mouse event then you should use the focusProxy which is an internal widget that handles these types of events. On the other hand you must correctly apply inheritance.
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.view.focusProxy().installEventFilter(self)
def eventFilter(self, obj, event):
if obj is self.ui.view.focusProxy() and event.type() == event.MouseButtonPress:
print("Widget click")
return super(MyWin, self).eventFilter(obj, event)
I have a row of buttons, each of which can accept drops. However, when I leave a button with my cursor with another button being dragged, the 'dragLeaveEvent' is not being called.
class Button(QtGui.QPushButton):
def __init__(self):
super(Button, self).__init__()
self.setAcceptDrops(True)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
drag = QtGui.QDrag(self)
mime = QtCore.QMimeData()
mime.setText("f")
drag.setMimeData(mime)
drag.exec_()
def dragEnterEvent(self, event):
print "enter"
def dragLeaveEvent(self, event):
print "leave"
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.mainLayout = QtGui.QVBoxLayout()
self.setLayout(self.mainLayout)
for i in range(10):
btn = Button()
self.mainLayout.addWidget(btn)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
As the documentation about dragEnterEvent() reports (it's from Qt5, but the same was valid for Qt4 also):
If the event is ignored, the widget won't receive any drag move events.
Note: any drag move events.
This also means drop events.
By default, all drag events are ignored for most widgets if the drag enter event is not accepted, so if you want to receive all events (including the leave event) that first event must be accepted.
class Button(QtGui.QPushButton):
# ...
def dragEnterEvent(self, event):
event.accept()
print "enter"
I am writing a software with Python 3.5 and PyQt5. When I override mousePressEvent of QTableWidget the cellClicked signal doesn't trigger anymore. I need overriding mousePressEvent for getting the pos of mouse clicking and cellClicked for doing another job for cells that are clicked. Why it is like this? Where is the problem?
This is my code for overriding the events:
class QTableWidget(QTableWidget):
def checking(self):
if (self.endgingposx - self.startingposx) >= 50:
self.clear()
def mousePressEvent(self,event):
event.accept()
self.startingposx = event.x()
def mouseReleaseEvent(self,event):
event.accept()
self.endgingposx = event.x()
self.checking()
and this is my clicking signal and it's connected slot:
self.main_table.cellClicked.connect(self.populatescnte)
def populatescnte(self,row,column):
itemnum = self.scentence_listw.count()
exec("item{} = QListWidgetItem(QIcon('content/img/food.png'),'{}')".format(itemnum+1,self.main_table.cellWidget(row,column).text))
exec("self.scentence_listw.addItem(item{})".format(itemnum+1))
self.scentence_listw is a QListWidget.
Thank you in advance.
Update
Thank you every body this code works for me:
class myTableWidget(QTableWidget):
def checking(self):
if (self.endgingposx - self.startingposx) >= 50:
self.clear()
def mousePressEvent(self,event):
super().mousePressEvent(event)
self.startingposx = event.x()
def mouseReleaseEvent(self,event):
super().mouseReleaseEvent(event)
self.endgingposx = event.x()
self.checking()
The event isn't being propagated because you have accepted it in your handler.
You allow need to call the the original handler.
FWIW, you can hook up a handler to itemSelectionChanged and get the selectedItems on the table rather than tracking positions.
import sys
from PyQt5.QtWidgets import (
QApplication, QWidget, QTableWidget,
QTableWidgetItem, QGridLayout
)
class Widget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Simple')
table = TrackedTableWidget()
layout = QGridLayout()
layout.addWidget(table, 0, 0)
self.setLayout(layout)
self.show()
class TrackedTableWidget(QTableWidget):
def __init__(self, *args, **kwargs):
super().__init__()
self.initUI()
def initUI(self):
self.setRowCount(2)
self.setColumnCount(2)
self.setItem(0, 0, QTableWidgetItem('30', 0));
self.setItem(0, 1, QTableWidgetItem('40', 0));
self.setItem(1, 0, QTableWidgetItem('50', 0));
self.setItem(1, 1, QTableWidgetItem('60', 0));
self.cellClicked.connect(self._handle_cell_clicked)
self.itemSelectionChanged.connect(self._handle_item_selection)
def _handle_cell_clicked(self, row, column):
print('cell_clicked', row, column)
print('selected cell positions', self.startingposx, self.endingposx)
def _handle_item_selection(self, *args):
print(self.selectedItems())
print('item_selection', args)
def mousePressEvent(self, event):
print('mousePressEvent')
self.startingposx = event.x()
super().mousePressEvent(event)
def mouseReleaseEvent(self, event):
print('mouseReleaseEvent')
self.endingposx = event.x()
super().mouseReleaseEvent(event)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = Widget()
sys.exit(app.exec_())
Reference
About accepting events
I handle the event as follow:
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__() # Call the inherited classes __init__ method
uic.loadUi('./mainwindow.ui', self) # Load the .ui file
self.myButton.pressed.connect(self.mousePress)
self.show() # Show the GUI
def mousePress(self):
print("Mouse pressed")
i am trying to capture the key_tab event, but no luck. i realized that it only works if there are no other widgets so the cursor has no where to go only then can i get the event to return. here is a simplified code sample.
class MyCombo(QComboBox):
def __init__(self, parent=None):
super(MyCombo, self).__init__(parent)
self.setEditable(True)
def keyPressEvent(self, event):
if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Tab):
print "tab pressed"
elif event.key() == Qt.Key_Return:
print "return pressed"
else:
QComboBox.keyPressEvent(self, event)
class Form_1(QDialog):
def __init__(self, parent=None):
super(Form_1, self).__init__(parent)
self.combo = MyCombo()
self.line = QLineEdit()
layout = QVBoxLayout()
layout.addWidget(self.combo)
layout.addWidget(self.line)
self.setLayout(layout)
app = QApplication(sys.argv)
form = Form_1()
form.show()
app.exec_()
if i comment out the following two lines
self.line = QLineEdit()
layout.addWidget(self.line)
then it works fine, because there is only one widget left on the form.
where am i going wrong?
Cheers, Joe
Apparently the Key_Tab press event is never passed to any handler but to the setFocus() so in order to intercept the Key_Tab event, we need to implement the event() method itself.
so here is the new code:
class MyCombo(QComboBox):
def __init__(self, parent=None):
super(MyCombo, self).__init__(parent)
self.setEditable(True)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return:
print "return pressed"
else:
QComboBox.keyPressEvent(self, event)
def event(self, event):
if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
print "tab pressed"
return False
return QWidget.event(self, event)
class Form_1(QDialog):
def __init__(self, parent=None):
super(Form_1, self).__init__(parent)
self.combo = MyCombo()
self.line = QLineEdit()
layout = QVBoxLayout()
layout.addWidget(self.combo)
layout.addWidget(self.line)
self.setLayout(layout)
I have a QGraphicsScene and I will display a contextmenu. But nothing happend.
Here is my Code:
class graphicsScene(QtGui.QGraphicsScene):
def __init__ (self, parent = None):
super(graphicsScene, self).__init__ (parent)
def contextMenuEvent(self, event):
self.popMenu = QtGui.QMenu()
self.popMenu.addAction(QtGui.QAction('test0', None))
self.popMenu.addAction(QtGui.QAction('test1', None))
self.popMenu.addSeparator()
self.popMenu.addAction(QtGui.QAction('test2', None))
self.popMenu.exec_(event.globalPos())
def mousePressEvent(self, event):
super(graphicsScene, self).mousePressEvent(event)
pos = event.scenePos()
item = self.itemAt(pos)
if event.button() == QtCore.Qt.LeftButton:
#do something
elif event.button() == QtCore.Qt.RightButton:
self.contextMenuEvent(event)
I have no idea how to fix this problem.
Thank you for your help!!!
This may help:
class Table(QtGui.QGraphicsView):
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.setScene(QtGui.QGraphicsScene())
self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
def contextMenuEvent(self, event):
menu = QtGui.QMenu()
menu.addAction('sample')
menu.exec_(event.globalPos())