How to draw a line from mouse to a point in PyQt5? - python

Here is my code:
import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class MouseTracker(QWidget):
distance_from_center = 0
def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)
def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()
def mouseMoveEvent(self, event):
distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)
self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(distance_from_center))
q = QPainter() #Painting the line
q.begin(self)
q.drawLine(event.x(), event.y(), 250, 500)
q.end()
def drawPoints(self, qp, x, y):
qp.setPen(Qt.red)
qp.drawPoint(x, y)
app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())
What I'm trying to do is paint a simple line from the position of the mouse to the middle of the widget using this:
q = QPainter() #Painting the line
q.begin(self)
q.drawLine(event.x(), event.y(), 250, 500)
q.end()
But when I run it, no line is visible. What do I need to do?

You must implement the function QPaintEvent, in this function you must draw the line, in addition you must call the function update() to update the drawing.
import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class MouseTracker(QWidget):
distance_from_center = 0
def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)
def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()
self.pos = None
def mouseMoveEvent(self, event):
distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)
self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(distance_from_center))
self.pos = event.pos()
self.update()
def paintEvent(self, event):
if self.pos:
q = QPainter(self)
q.drawLine(self.pos.x(), self.pos.y(), 500, 250)
app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())
Output:

You can only use a QPainter inside the paintEvent method. So one way to fix is to record the x and y coordinates inside the class and update the root widget. This then calls paintEvent and you will see the line.
example
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
class MouseTracker(QWidget):
distance_from_center = 0
def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)
self.x = -1
self.y = -1
def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()
def paintEvent(self, e):
if not (self.x == -1 or self.y == -1):
q = QPainter() #Painting the line
q.begin(self)
q.drawLine(self.x, self.y, 250, 500)
q.end()
def mouseMoveEvent(self, event):
distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)
self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(distance_from_center))
self.x = event.x()
self.y = event.y()
self.update()
def drawPoints(self, qp, x, y):
qp.setPen(Qt.red)
qp.drawPoint(x, y)
app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())
I wasn't sure about how self.x and self.y would be set initially. The -1 and the check in the paintEvent feels a bit hacky, but at least it paints.

For the previous answer, I tried it under Python3.7 and PyQt5. The result was program crash
'Process finished with exit code -1073740791 (0xC0000409)'.
And in the comments, there were someone else also mentioned the crash.
I find the solution to this crash:
self.x and self.y must be initialized before the calling of self.show()
So I simply modified the code to as follows:
def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.x = 100
self.y = 100
self.show()

Related

create a transparent grid on top of QImage

I am trying to build a paint app that has a grid, but I don't want to grid to be included in the saved photo.
I tried to draw on a transparent image, but I got a black background instead!
Here is the full code:
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import QPoint, Qt
from PyQt5.QtGui import QBrush, QGuiApplication, QImage, QPainter, QPen, QIcon, QColor
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QTextEdit, QAction, QFileDialog,
QGraphicsScene, QGraphicsProxyWidget, QGraphicsView
import pyautogui
import matplotlib.pyplot as plt
class Drawer(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._drawing = False
self.last_point = QPoint()
self.image_layer = QImage(self.size(), QImage.Format_RGB32)
self.image_layer.fill(Qt.gray)
self.brushSize = 2
self.brushColor = Qt.black
#paint = QPainter(self.image_layer)
#paint.setCompositionMode(QtGui.QPainter.CompositionMode_Clear)
# paint.drawLine(0,0,self.size().width(),0)
# paint.drawLine(0,10,200,10)
#paint.drawLine(0,0,0,200)
#paint.drawLine(10,0,10,200)
self.update()
def mousePressEvent(self, event):
self._drawing = True
self.last_point = event.pos()
def mouseMoveEvent(self, event):
if self._drawing and event.buttons() & Qt.LeftButton:
painter = QPainter(self.image_layer)
painter.setPen(
QPen(
self.brushColor,
self.brushSize,
Qt.SolidLine,
Qt.RoundCap,
Qt.RoundJoin,
)
)
painter.drawLine(self.last_point, event.pos())
self.last_point = event.pos()
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.drawImage(QPoint(), self.image_layer)
painter.end()
def resizeEvent(self, event):
if (
self.size().width() > self.image_layer.width()
or self.size().height() > self.image_layer.height()
):
qimg = QImage(
max(self.size().width(), self.image_layer.width()),
max(self.size().height(), self.image_layer.height()),
QImage.Format_RGB32,
)
qimg.fill(Qt.gray)
painter = QPainter(qimg)
painter.drawImage(QPoint(), self.image_layer)
painter.drawLine(0, 0, qimg.size().width(), 0)
painter.drawLine(0, 10, qimg.size().width(), 10)
painter.drawLine(0, 600, qimg.size().width(), 600)
print(qimg.size().height())
painter.end()
self.image_layer = qimg
self.update()
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
[x, y] = pyautogui.size()
self.setGeometry(0, 0, x, y)
self.drawer = Drawer()
textbox = QTextEdit("Converted text will show here")
central_widget = QWidget()
self.setCentralWidget(central_widget)
vlay = QVBoxLayout(central_widget)
vlay.addWidget(textbox)
vlay.addWidget(self.drawer, stretch=1)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu("File")
saveAction = QAction(QIcon("icons/save.png"), "Save", self)
saveAction.setShortcut("Ctrl+S")
fileMenu.addAction(saveAction)
saveAction.triggered.connect(self.save)
def save(self):
filePath, _ = QFileDialog.getSaveFileName(self.drawer, "Save Image", "",
"PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
if filePath == "":
return
self.drawer.image_layer.save(filePath)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
Note: there are only two lines that mimics the grid, I can draw it later, but for now I want the saved image to not include the grind lines.
You should paint the grid in the paintEvent, instead of continuously painting it onto the image.
def paintEvent(self, event):
painter = QPainter(self)
painter.drawImage(QPoint(), self.image_layer)
gridSize = 10
x = y = 0
width = self.width()
height = self.height()
while y <= height:
# draw horizontal lines
painter.drawLine(0, y, width, y)
y += gridSize
while x <= width:
# draw vertical lines
painter.drawLine(x, 0, x, height)
x += gridSize

custom QGraphicsItem unwanted behaviour after overriding mouse event

I am trying to model a chess board and i want the pawn GraphicsItem object to be dropped in the center of the case GraphicsItem object .
i only implemented a mouseRelease event for the pawn object, it checks the list of items in the position, if a case is present i drop the pawn at the case position .
now when trying it in the window it works fine for the first move, but after that when i try to move the pawn it goes back to its original position i can still move it but its not under the mouse cursor
here is the code:
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QGraphicsView, QGraphicsScene, QGraphicsItem)
from PyQt5.QtGui import QPen, QBrush, QTransform
from PyQt5.QtCore import Qt, QRectF, QPointF
class Pawn(QGraphicsItem):
def __init__(self, parent = None):
super().__init__(parent)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setPos(0, 0)
self.setZValue(1)
self.originalPos = self.scenePos()
def paint(self, painter, options, widget):
painter.setBrush(Qt.white)
painter.drawEllipse(0, 0, 30, 30)
def boundingRect(self):
return QRectF(0, 0, 30, 30)
def mouseReleaseEvent(self, event):
dropPos = self.mapToScene(event.pos())
dropCase = None
for item in self.scene().items(dropPos.x(), dropPos.y(), 0.0001, 0.0001,
Qt.IntersectsItemShape,
Qt.AscendingOrder):
if isinstance(item, Case):
dropCase = item
if dropCase:
newP = dropCase.scenePos()
self.setPos(newP)
self.originalPos = newP
else:
self.setPos(self.originalPos)
class Case(QGraphicsItem):
def __init__(self, parent = None):
super().__init__(parent)
self.setPos(100, 0)
self.setZValue(0)
def paint(self, painter, options, widget):
painter.setPen(Qt.black)
painter.setBrush(Qt.black)
painter.drawRect(0, 0, 40, 40)
def boundingRect(self):
return QRectF(0, 0, 40, 40)
def getCenter(self):
x, y = self.scenePos().x() + 10, self.scenePos().y() + 10
return QPointF(x, y)
class MainWin(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
scene = QGraphicsScene()
view = QGraphicsView(scene, self)
view.setGeometry(0, 0, 290, 290)
case = Case()
pawn = Pawn()
scene.addItem(case)
scene.addItem(pawn)
self.setWindowTitle('doodling')
self.setGeometry(200, 200, 300, 300)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWin()
sys.exit(app.exec_())
I did not implement the mouseMoveEvent as i confused it with hover behaviour.
this code works as intended
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QGraphicsView, QGraphicsScene, QGraphicsItem)
from PyQt5.QtGui import QPen, QBrush, QTransform
from PyQt5.QtCore import Qt, QRectF, QPointF
class Pawn(QGraphicsItem):
def __init__(self, parent = None):
super().__init__(parent)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setPos(0, 0)
self.setZValue(1)
self.originalPos = self.scenePos()
def paint(self, painter, options, widget):
painter.setBrush(Qt.white)
painter.drawEllipse(0, 0, 30, 30)
def boundingRect(self):
return QRectF(0, 0, 30, 30)
def mouseMoveEvent(self, event):
movePos = self.mapToScene(event.pos())
self.setPos(movePos.x(), movePos.y())
def mouseReleaseEvent(self, event):
dropPos = self.mapToScene(event.pos())
dropCase = None
for item in self.scene().items(dropPos.x(), dropPos.y(), 0.0001, 0.0001,
Qt.IntersectsItemShape,
Qt.AscendingOrder):
if isinstance(item, Case):
dropCase = item
if dropCase:
newP = dropCase.scenePos()
self.setPos(newP)
self.originalPos = newP
else:
self.setPos(self.originalPos)
class Case(QGraphicsItem):
def __init__(self, x_coord, y_coord, parent = None):
super().__init__(parent)
self.setPos(x_coord, y_coord)
self.setZValue(0)
def paint(self, painter, options, widget):
painter.setPen(Qt.black)
painter.setBrush(Qt.black)
painter.drawRect(0, 0, 40, 40)
def boundingRect(self):
return QRectF(0, 0, 40, 40)
def getCenter(self):
x, y = self.scenePos().x() + 10, self.scenePos().y() + 10
return QPointF(x, y)
class MainWin(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
scene = QGraphicsScene()
view = QGraphicsView(scene, self)
view.setGeometry(0, 0, 290, 290)
case1 = Case(-100, 0)
case2 = Case(100, 0)
pawn = Pawn()
scene.addItem(case1)
scene.addItem(case2)
scene.addItem(pawn)
self.setWindowTitle('doodling')
self.setGeometry(200, 200, 300, 300)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWin()
sys.exit(app.exec_())

how to upgrade from pyqt4 to pyqt5 in python

I want to upgrade or convert this code from pyqt4 to pyqt5 as this code is not compatible with latest pyqt5.
So can someone tell me what major changes i can make in this code to run it in pyqt5.
import sys
from PyQt4.QtCore import Qt
from PyQt4.QtCore import QRectF
from PyQt4.QtWidgets import QApplication
from PyQt4.QtGui import QColor
from PyQt4.QtGui import QFont
from PyQt4.QtGui import QPainter
from PyQt4.QtGui import QPixmap
from PyQt4.QtGui import QTextOption
from PyQt4.QtGui import QToolTip
from PyQt4.QtGui import QWidget
this are all the imported libraries for this code
class AreaSelector(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, None, Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setWindowState(Qt.WindowFullScreen)
self.setAutoFillBackground(False)
self.parent = parent
self.start_x = 0
self.start_y = 0
self.end_x = 0
self.end_y = 0
self.current_x = 0
self.current_y = 0
def showEvent(self, event):
self.bg = QPixmap.grabWindow(QApplication.desktop().winId())
self.screen_geometry = QApplication.desktop().screenGeometry(self)
def mousePressEvent(self, event):
self.start_x = event.globalX()
self.start_y = event.globalY()
def mouseReleaseEvent(self, event):
self.end_x = event.globalX()
self.end_y = event.globalY()
please view the full code here full code
Translating a PyQt4 code to PyQt5 is not a trivial task:
PyQt4 and PyQt5 are wrappers of Qt4 and Qt5, respectively, so both are affected by the changes of that transition, and one of the transitions is that the QtGui sub-module of Qt4 was divided into the QtGui and QtWidgets sub-modules of Qt5.
Some classes and methods are deprecated so you will have to find an equivalent if it exists.
In this case both things happen, the solution for the first case is simple: You must look in the Qt docs and check to which sub-module it belongs, for example QToolTip, at the top there is a table:
And the part of QT += widgets that indicates that it belongs to the QtWidgets sub-module is observed.
But the second case is somewhat more complicated since it involves looking for an equivalent that may or may not be in the same class, in this case it happens with the QPixmap.grabWindow() method which is deprecates (see here for more information). After doing a search you can replace that code with QApplication.primaryScreen().grabWindow(0).
Considering all of the above, the translation is:
import sys
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtGui import QColor, QFont, QPainter, QPixmap, QTextOption, QScreen
from PyQt5.QtWidgets import QApplication, QToolTip, QWidget
class AreaSelector(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, None, Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setWindowState(Qt.WindowFullScreen)
self.setAutoFillBackground(False)
self.parent = parent
self.start_x = 0
self.start_y = 0
self.end_x = 0
self.end_y = 0
self.current_x = 0
self.current_y = 0
def showEvent(self, event):
self.bg = QApplication.primaryScreen().grabWindow(0)
self.screen_geometry = QApplication.primaryScreen().geometry()
def mousePressEvent(self, event):
self.start_x = event.globalX()
self.start_y = event.globalY()
def mouseReleaseEvent(self, event):
self.end_x = event.globalX()
self.end_y = event.globalY()
def mouseMoveEvent(self, event):
self.current_x = event.globalX()
self.current_y = event.globalY()
self.repaint()
text = "Start: %sx%s \nEnd: %sx%s" % (
self.start_x,
self.start_y,
self.current_x,
self.current_y,
)
QToolTip.showText(event.pos(), text)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return:
self._acceptSelection()
elif event.key() == Qt.Key_Escape:
self.close()
def _acceptSelection(self):
if self.parent is not None:
self.parent.areaSelectEvent(
self.start_x, self.start_y, self.end_x, self.end_y
)
self.close()
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.fillRect(self.screen_geometry, QColor(10, 10, 10, 125))
self._paint_selection(painter)
self._paint_usage_text(painter)
painter.end()
def _paint_usage_text(self, painter):
font = QFont("Helvetica [Cronyx]", 26, QFont.Bold)
painter.setFont(font)
painter.setPen(QColor(255, 255, 255, 255))
screen_width = self.screen_geometry.width()
text_width = 800
text_start_x = screen_width / 2 - text_width / 2
screen_height = self.screen_geometry.height()
text_height = 200
text_start_y = screen_height / 2 - text_height / 2
textoption = QTextOption(Qt.AlignCenter)
textbox = QRectF(text_start_x, text_start_y, text_width, text_height)
painter.drawText(
textbox,
"Click & Drag to select an area\n" "ENTER to confirm or ESC to cancel",
textoption,
)
painter.drawRoundedRect(textbox, 20, 20)
def _paint_selection(self, painter):
"""Draws the current user selection"""
rectangle = QRectF()
if self.start_x > self.current_x:
rectangle.setLeft(self.current_x)
rectangle.setRight(self.start_x)
else:
rectangle.setLeft(self.start_x)
rectangle.setRight(self.current_x)
if self.start_y > self.current_y:
rectangle.setTop(self.current_y)
rectangle.setBottom(self.start_y)
else:
rectangle.setTop(self.start_y)
rectangle.setBottom(self.current_y)
painter.drawPixmap(rectangle, self.bg, rectangle)
painter.drawRect(rectangle)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = AreaSelector()
main.show()
sys.exit(app.exec_())

Update the Opacity of QGraphicsItem

I want to update the opacity of some QGraphicsItem after the mouse clicking. As suggested from other solution, the QGraphicScene manually update the GraphicsItem after the mouser press event. I have tried different setOpacity() and update() in QGraphicsScene and QGraphicsItem. But none works and do not know what is wrong.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
CUBE_POS = {
"a":( 8.281, 18.890),
"b":( 8.668, 23.692),
"c":( 21.493, 23.423),
"d":( 21.24, 15.955),
}
class CubeItem(QGraphicsItem):
def __init__(self, x, y, parent=None):
super(CubeItem,self).__init__(parent)
self.x = x
self.y = y
self.polygon = QPolygonF([
QPointF(self.x-10, self.y-10), QPointF(self.x-10, self.y+10),
QPointF(self.x+10, self.y+10), QPointF(self.x+10, self.y-10),
])
self._painter = QPainter()
##Estimate the drawing area
def boundingRect(self):
return QRectF(self.x-10, self.y-10, 20, 20)
##Real Shape of drawing area
def shape(self):
path = QPainterPath()
path.addRect(self.x-10, self.y-10, 20, 20)
return path
##paint function called by graphicview
def paint(self, painter, option, widget):
painter.setBrush(Qt.red)
painter.setOpacity(0.2)
painter.drawRect(self.x-10, self.y-10, 20, 20)
self._painter = painter
def activate(self):
try:
#self._painter.setOpacity(1.0)
self.setOpacity(1.0)
self.update()
except ValueError as e:
print(e)
class TagScene(QGraphicsScene):
def __init__(self, parent=None):
super(TagScene, self).__init__(parent)
self.cubes_items_ref = {}
self.addCubes()
def addCubes(self):
for cube in CUBE_POS:
newCube = CubeItem(CUBE_POS[cube][0]*15,
CUBE_POS[cube][1]*15)
self.addItem(newCube)
self.cubes_items_ref[cube] = newCube
def mousePressEvent(self, event):
print("mouse pressed")
#for cube in self.cubes_items_ref:
# self.cubes_items_ref[cube].setOpacity(1.0)
# #self.cubes_items_ref[cube].activate()
#self.update(QRectF(0,0,500,500))
for cube in self.items():
cube.setOpacity(1.0)
self.update(QRectF(0,0,500,500))
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
layout = QHBoxLayout()
self.scene = TagScene()
self.view = QGraphicsView(self.scene)
self.scene.setSceneRect(QRectF(0,0,500,500))
layout.addWidget(self.view)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
test = MainWindow()
test.show()
sys.exit(app.exec_())
The problem is that when you overwrite the paint method of the QGraphicsItem you are setting a constant opacity
def paint(self, painter, option, widget):
painter.setBrush(Qt.red)
painter.setOpacity(0.2) # <-- this line is the problem
painter.drawRect(self.x-10, self.y-10, 20, 20)
self._painter = painter
And you will not use the opacity that the QPainter already passes paint() method.
If you want to set an initial opacity you must do it in the constructor.On the other hand the setOpacity() method already calls update() so it is not necessary to make an explicit call.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
CUBE_POS = {
"a": (8.281, 18.890),
"b": (8.668, 23.692),
"c": (21.493, 23.423),
"d": (21.24, 15.955),
}
class CubeItem(QtWidgets.QGraphicsItem):
def __init__(self, x, y, parent=None):
super(CubeItem, self).__init__(parent)
self.x = x
self.y = y
self.polygon = QtGui.QPolygonF(
[
QtCore.QPointF(self.x - 10, self.y - 10),
QtCore.QPointF(self.x - 10, self.y + 10),
QtCore.QPointF(self.x + 10, self.y + 10),
QtCore.QPointF(self.x + 10, self.y - 10),
]
)
self.setOpacity(0.2) # initial opacity
##Estimate the drawing area
def boundingRect(self):
return QtCore.QRectF(self.x - 10, self.y - 10, 20, 20)
##Real Shape of drawing area
def shape(self):
path = QtGui.QPainterPath()
path.addRect(self.boundingRect())
return path
##paint function called by graphicview
def paint(self, painter, option, widget):
painter.setBrush(QtCore.Qt.red)
painter.drawRect(self.x - 10, self.y - 10, 20, 20)
class TagScene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(TagScene, self).__init__(parent)
self.cubes_items_ref = {}
self.addCubes()
def addCubes(self):
for cube in CUBE_POS:
newCube = CubeItem(CUBE_POS[cube][0] * 15, CUBE_POS[cube][1] * 15)
self.addItem(newCube)
self.cubes_items_ref[cube] = newCube
def mousePressEvent(self, event):
for cube in self.items():
cube.setOpacity(1.0) # update opacity
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
layout = QtWidgets.QHBoxLayout()
self.scene = TagScene()
self.view = QtWidgets.QGraphicsView(self.scene)
self.scene.setSceneRect(QtCore.QRectF(0, 0, 500, 500))
layout.addWidget(self.view)
self.widget = QtWidgets.QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
test = MainWindow()
test.show()
sys.exit(app.exec_())

PyQt crashing when trying to round variable in label

I'm trying to create a widget that prints into a label how far the mouse is from the center. Here is my code:
import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class MouseTracker(QWidget):
distance_from_center = 0
def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)
def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()
def mouseMoveEvent(self, event):
distance_from_center = ((event.y() - 250) + (event.x() - 500))**0.5
self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(round(distance_from_center)))
def drawPoints(self, qp, x, y):
qp.setPen(Qt.red)
qp.drawPoint(x, y)
app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())
This is where the problem lies:
def mouseMoveEvent(self, event):
distance_from_center = ((event.y() - 250) + (event.x() - 500))**0.5
self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(round(distance_from_center)))
If I remove the round function around distance_from_center, it prints a value but it's not pretty. How can I make this work? Why can't I simply round?
If you want to calculate the Euclidean distance you must change:
((event.y() - 250) + (event.x() - 500))**0.5
to
((event.y() - 250)**2 + (event.x() - 500)**2)**0.5

Categories