In my program I can select a button that sets self.rectmode=1. Once that variable is set to 1 it draws rectangles using mouse events on the qgraphicsview. After pressing a button to set self.rectmode=0 the program continues to draw rectangles using the mouse events. Am I missing some line to end the rectangle drawing event. My code is below thanks in advance:
def mousePressEvent(self, event):
if (self.rectmode==1 and event.button() == Qt.LeftButton and not self._photo.pixmap().isNull()):
self.origin = event.pos()
self.rubberBand.setGeometry(QRect(self.origin, QSize()))
self.rectChanged.emit(self.rubberBand.geometry())
self.rubberBand.show()
self.changeRubberBand = True
else:
super(PhotoViewer, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.rectmode==1 and self.changeRubberBand:
self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized())
self.rectChanged.emit(self.rubberBand.geometry())
else:
super(PhotoViewer, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.rectmode==1 and event.button() == Qt.LeftButton:
self.changeRubberBand = False
self.endpoint = event.pos()
print(self.origin.x())
print(self.origin.y())
print(self.endpoint.x())
print(self.endpoint.y())
else:
super(PhotoViewer, self).mouseReleaseEvent(event)
In your code rectmode is always 1, I think that is what's causing the problem, here is a working example, I also removed the variable changeRubberBand because the same can be achieved with only the variable rectMode:
import sys
from PyQt5.Qt import QApplication, QRect, QSize, Qt, QRubberBand, QVBoxLayout, pyqtSignal
from PyQt5.QtWidgets import QMainWindow
class PhotoViewer(QMainWindow):
rectChanged = pyqtSignal(QRect)
def __init__(self):
super().__init__()
self.origin = None
self.endpoint = None
self.rectMode = 0
self.setFixedSize(1024, 768)
self.layout = QVBoxLayout(self)
self.rubberBand = QRubberBand(QRubberBand.Rectangle, self)
self.rubberBand.hide()
self.layout.addChildWidget(self.rubberBand)
def mousePressEvent(self, event):
if self.rectMode == 0 and event.button() == Qt.LeftButton:
self.origin = event.pos()
self.rubberBand.setGeometry(QRect(self.origin, QSize()))
self.rectChanged.emit(self.rubberBand.geometry())
self.rubberBand.show()
self.rectMode = 1
else:
super(PhotoViewer, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.rectMode == 1:
self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized())
self.rectChanged.emit(self.rubberBand.geometry())
else:
super(PhotoViewer, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.rectMode == 1 and event.button() == Qt.LeftButton:
self.rectMode = 0
self.endpoint = event.pos()
print(self.origin.x())
print(self.origin.y())
print(self.endpoint.x())
print(self.endpoint.y())
else:
super(PhotoViewer, self).mouseReleaseEvent(event)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = PhotoViewer()
mainWindow.show()
sys.exit(app.exec_())
Hope it helps.
Related
I have a scene where I would like to draw a line between two points(mouse press should be the start point and mouse release as the endpoint) using the QPainterpath.
Here is a demonstration of how I want it to be.
Here is what's happening with my current code.
Below is the code I have tried
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, *args, **kwargs):
super(Scene, self).__init__(*args, **kwargs)
self.point = QtCore.QPointF(0.0, 0.0)
self.path = QtGui.QPainterPath()
self.start_point = None
self.end_point = None
self.start = False
def mousePressEvent(self, event):
self.start_point = event.scenePos()
self.start = True
self.path = QtGui.QPainterPath(self.start_point)
# self.addLine(self.line.setP1(self.start_point))
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.start:
self.path.lineTo(event.scenePos())
# self.path.moveTo(event.scenePos())
self.addPath(self.path, QtGui.QPen(QtCore.Qt.red))
super(Scene, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.start:
print(self.path)
self.path.lineTo(event.scenePos())
# self.path.moveTo(event.scenePos())
self.addPath(self.path, QtGui.QPen(QtCore.Qt.red))
self.start = False
super(Scene, self).mouseReleaseEvent(event)
def main():
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QGraphicsView()
view.setRenderHint(QtGui.QPainter.Antialiasing)
view.setMouseTracking(True)
scene = Scene()
view.setScene(scene)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Every time lineTo is used then a new line is created where the starting point is the last point added and the end point is the one that is passed to the function, so you see the curves since they are the union of those lines. The solution is to have 2 variables that store the start point and the end point, and be updated when necessary, then use that information to update the QGraphicsPathItem, not the QPainterPath. The same concept can be applied for QGraphicsLineItem with QLineF.
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, *args, **kwargs):
super(Scene, self).__init__(*args, **kwargs)
self.path_item = self.addPath(QtGui.QPainterPath())
self.start_point = QtCore.QPointF()
self.end_point = QtCore.QPointF()
def mousePressEvent(self, event):
self.start_point = event.scenePos()
self.end_point = self.start_point
self.update_path()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self.end_point = event.scenePos()
self.update_path()
super(Scene, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.end_point = event.scenePos()
self.update_path()
super(Scene, self).mouseReleaseEvent(event)
def update_path(self):
if not self.start_point.isNull() and not self.end_point.isNull():
path = QtGui.QPainterPath()
path.moveTo(self.start_point)
path.lineTo(self.end_point)
self.path_item.setPath(path)
Thx to #eyllanesc for anyone trying to achieve this using QLineF and QGraphicsLineItem
here is the code.
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, *args, **kwargs):
super(Scene, self).__init__(*args, **kwargs)
self.setSceneRect(QtCore.QRectF(0, 0, 500, 500))
self.line = None
self.graphics_line = None
self.start_point = QtCore.QPointF()
self.end_point = QtCore.QPointF()
def mousePressEvent(self, event):
self.start_point = event.scenePos()
self.end_point = self.start_point
self.line = QtCore.QLineF(self.start_point, self.end_point)
self.graphics_line = QtWidgets.QGraphicsLineItem(self.line)
self.update_path()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self.end_point = event.scenePos()
self.update_path()
super(Scene, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.end_point = event.scenePos()
self.update_path()
super(Scene, self).mouseReleaseEvent(event)
def update_path(self):
if not self.start_point.isNull() and not self.end_point.isNull():
self.line.setP2(self.end_point)
self.graphics_line.setLine(self.line)
self.addItem(self.graphics_line)
def main():
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QGraphicsView()
view.setRenderHint(QtGui.QPainter.Antialiasing)
view.setMouseTracking(True)
scene = Scene()
view.setScene(scene)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Today , I got stuck with QPixmap. My class is inheriting from QtWidgets.QGraphicsPixmapItem and like in example below.
class graphicButton(QtWidgets.QGraphicsPixmapItem):
def __init__(self):
pixmapA = QtGui.QPixmap(r"img.png")
QtWidgets.QGraphicsPixmapItem.__init__(self, pixmapA)
self.setFlags(
self.flags( )
| QtWidgets.QGraphicsItem.ItemIsSelectable
| QtWidgets.QGraphicsItem.ItemIsMovable
)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print("mouse left press")
event.accept()
elif event.button() == QtCore.Qt.RightButton:
print("mouse right press")
event.accept()
elif event.button() == QtCore.Qt.MidButton:
print("mouse middle press")
event.accept()
It's working fine, but what if I want to put more then one picture? In most cases which I found in google, you need to make multiple QGraphicsPixmapItems. In that case I can't anymore inherit from QGraphicsPixItem and I have to go for QGraphicsItem or am I missing something ?
The simple solution is to load an image associated with its position, for example in the following case I place the images in the vertices of a pentagon:
import random
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsButton(QtWidgets.QGraphicsPixmapItem):
def __init__(self, name, pixmap, parent=None):
super(GraphicsButton, self).__init__(pixmap, parent)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self._name = name
#property
def name(self):
return self._name
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print("mouse left press")
elif event.button() == QtCore.Qt.RightButton:
print("mouse right press")
elif event.button() == QtCore.Qt.MidButton:
print("mouse middle press")
print(self.name)
super(GraphicsButton, self).mousePressEvent(event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene()
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)
# coordinates of the pentagon
datas = [
("name1", "img0.png", QtCore.QPointF(0, -200)),
("name2", "img1.png", QtCore.QPointF(-190, -62)),
("name3", "img2.png", QtCore.QPointF(-118, 162)),
("name4", "img3.png", QtCore.QPointF(118, 162)),
("name5", "img0.png", QtCore.QPointF(190, -62)),
]
for name, path, position in datas:
item = GraphicsButton(name, QtGui.QPixmap(path))
scene.addItem(item)
item.setPos(position)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I'm new to PyQt5. I'm trying to draw on top of a loaded image lines that are tracing my mouse, i.e. pressing the mouse will result in drawing whatever your mouse moves to, while the mouse is still pressed, and stops when we release the mouse.
I saw This answer and it was very helpful, but I'm trying to do something a bit more complicated.
I think I'm messing up with the events.
What I wrote (which doesn't work):
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel
from PyQt5.QtGui import QPixmap, QPainter, QPen
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.drawing = False
self.lastPoint = QPoint()
self.image = QPixmap("picture.png")
self.setGeometry(100, 100, 500, 300)
self.resize(self.image.width(), self.image.height())
self.label = QLabel(self)
self.show()
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.image)
pen = QPen(Qt.red, 3)
painter.setPen(pen)
painter.drawLine(self.lastPoint, event.pos())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(Qt.red, 3, Qt.SolidLine))
self.lastPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = Menu()
sys.exit(app.exec_())
You want to use drawLine in mouseMoveEvent. When you move your mouse, it will invoke mouseMoveEvent which will draw a line from the last position, and then it will invoke by itself the paintEvent.
Try this:
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QPainter, QPen
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.drawing = False
self.lastPoint = QPoint()
self.image = QPixmap("picture.png")
self.setGeometry(100, 100, 500, 300)
self.resize(self.image.width(), self.image.height())
self.show()
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.image)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(Qt.red, 3, Qt.SolidLine))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = Menu()
sys.exit(app.exec_())
I'm trying to drag and drop a Qlabel on another Qlabel with PyQt5:
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton, QMessageBox, QHBoxLayout, QVBoxLayout, QGridLayout,QFrame, QComboBox, QLabel, QLineEdit
from PyQt5.QtGui import QIcon, QFont, QPixmap, QImage
import sys
class my_label(QLabel):
def __init__(self,title,parent):
super().__init__(title,parent)
self.setAcceptDrops(True)
def dragEnterEvent(self,event):
if event.mimeData().hasFormat("text/plain"):
event.accept()
else:
event.ignore()
def dropEvent(self,event):
self.setText(event.mimeData().text())
class application(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = my_label("drop there",self)
label.resize(100,100)
label.move(190,65)
label_to_drag = QLabel("drag this",self)
#label_to_drag.setDragEnabled(True) #doesn't work with QLabel
self.show()
def closeEvent(self,event):
message = QMessageBox.question(self,"Message","Quit ?",QMessageBox.Yes | QMessageBox.No,QMessageBox.No)
if message == QMessageBox.Yes:
event.accept()
else:
event.ignore()
app = QApplication(sys.argv)
fenetre = application()
sys.exit(app.exec_())
I wanted to make my first label "draggable",by calling the setDragEnabled(True) method (as i was doing for QLineEdit),and drop it on the second label to change his text. Dropping text from another app is working fine,but I can't find how to drag the QLabel inside my own app...
What did I miss ?
EDIT : modified the code to try to Drag&Drop Images :
class DraggableLabel(QLabel):
def __init__(self,parent,image):
super(QLabel,self).__init__(parent)
self.setPixmap(QPixmap(image))
self.show()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_start_position = event.pos()
def mouseMoveEvent(self, event):
if not (event.buttons() & Qt.LeftButton):
return
if (event.pos() - self.drag_start_position).manhattanLength() < QApplication.startDragDistance():
return
drag = QDrag(self)
mimedata = QMimeData()
mimedata.setText(self.text())
drag.setMimeData(mimedata)
pixmap = QPixmap(self.size())
painter = QPainter(pixmap)
painter.drawPixmap(self.rect(), self.grab())
painter.end()
drag.setPixmap(pixmap)
drag.setHotSpot(event.pos())
drag.exec_(Qt.CopyAction | Qt.MoveAction)
class my_label(QLabel):
def __init__(self,title,parent):
super().__init__(title,parent)
self.setAcceptDrops(True)
def dragEnterEvent(self,event):
if event.mimeData().hasFormat("text/plain"):
print("event accepted")
event.accept()
else:
print("event rejected")
event.ignore()
def dropEvent(self,event):
if event.mimeData().hasImage():
self.setPixmap(QPixmap.fromImage(QImage(event.mimeData().imageData())))
class application(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label_to_drag = DraggableLabel(self,"index.jpg")
label = my_label("drop there",self)
label.resize(100,100)
label.move(190,65)
self.show()
When I drop the DraggableLabel (displaying the image) on my_label,the event is accepted, but hasImage() always returns false...Is the way I set the image up wrong ?
In the case of QLabel you must create everything from the beginning, for this you can follow the examples of the docs.
In the following example I have placed an example where one class only accepts the drop and the other the drag so that you can see each part and understand better.
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtGui import QDrag, QPixmap, QPainter, QCursor
from PyQt5.QtCore import QMimeData, Qt
class DraggableLabel(QLabel):
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_start_position = event.pos()
def mouseMoveEvent(self, event):
if not (event.buttons() & Qt.LeftButton):
return
if (event.pos() - self.drag_start_position).manhattanLength() < QApplication.startDragDistance():
return
drag = QDrag(self)
mimedata = QMimeData()
mimedata.setText(self.text())
drag.setMimeData(mimedata)
pixmap = QPixmap(self.size())
painter = QPainter(pixmap)
painter.drawPixmap(self.rect(), self.grab())
painter.end()
drag.setPixmap(pixmap)
drag.setHotSpot(event.pos())
drag.exec_(Qt.CopyAction | Qt.MoveAction)
class DropLabel(QLabel):
def __init__(self, *args, **kwargs):
QLabel.__init__(self, *args, **kwargs)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
def dropEvent(self, event):
pos = event.pos()
text = event.mimeData().text()
self.setText(text)
event.acceptProposedAction()
class Widget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = DropLabel("drop there",self)
label.setGeometry(190, 65, 100,100)
label_to_drag = DraggableLabel("drag this",self)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Update:
Do not confuse the QDrag with the QMimeData, if you want to use imageData() you must set it with setImageData() as indicated by the docs:
class DraggableLabel(QLabel):
def __init__(self,parent,image):
super(QLabel,self).__init__(parent)
self.setPixmap(QPixmap(image))
self.show()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_start_position = event.pos()
def mouseMoveEvent(self, event):
if not (event.buttons() & Qt.LeftButton):
return
if (event.pos() - self.drag_start_position).manhattanLength() < QApplication.startDragDistance():
return
drag = QDrag(self)
mimedata = QMimeData()
mimedata.setText(self.text())
mimedata.setImageData(self.pixmap().toImage())
drag.setMimeData(mimedata)
pixmap = QPixmap(self.size())
painter = QPainter(pixmap)
painter.drawPixmap(self.rect(), self.grab())
painter.end()
drag.setPixmap(pixmap)
drag.setHotSpot(event.pos())
drag.exec_(Qt.CopyAction | Qt.MoveAction)
class my_label(QLabel):
def __init__(self,title,parent):
super().__init__(title,parent)
self.setAcceptDrops(True)
def dragEnterEvent(self,event):
if event.mimeData().hasImage():
print("event accepted")
event.accept()
else:
print("event rejected")
event.ignore()
def dropEvent(self,event):
if event.mimeData().hasImage():
self.setPixmap(QPixmap.fromImage(QImage(event.mimeData().imageData())))
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())