Hi everyone, I’m new here. Like the title says, I have a seemingly simple problem. I have created a Qlabel to insert an image into, and want to draw a red cross on that image.
If I put the init_cross() function in init like the code below, when I run the program, a red cross will be drawn as shown in the picture.
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
#!Load the UI Page
uic.loadUi('myGUI.ui', self)
# Init cross
self.init_cross()
def init_cross(self):
#! Set the target image
self.pixmap = self.label.pixmap()
self.painter = QtGui.QPainter(self.pixmap)
#! set colour and width of line
self.painter.setPen(QtGui.QPen(QtGui.QColor('red'), 5))
#! Draw the cross
self.painter.drawLine(550, 550, 550 + CROSS_SIZE, 550 + CROSS_SIZE)
self.painter.drawLine(550, 550 + CROSS_SIZE, 550 + CROSS_SIZE, 550)
self.label.update()
#self.label.setPixmap(pixmap)
However, I want to call the draw_cross() function after completing the calculation to draw the red cross at the desired coordinates like the code below. There is absolutely no red cross drawn.
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
#!Load the UI Page
uic.loadUi('myGUI.ui', self)
# Init cross
self.init_cross()
def init_cross(self):
#! Set the target image
self.pixmap = self.label.pixmap()
self.painter = QtGui.QPainter(self.pixmap)
#! set colour and width of line
self.painter.setPen(QtGui.QPen(QtGui.QColor('red'), 5))
def draw_cross(self):
#! draw cross
self.painter.drawLine(100, 100, 110 + CROSS_SIZE, 110 + CROSS_SIZE)
self.painter.drawLine(100, 100 + CROSS_SIZE, 110 + CROSS_SIZE, 110)
self.label.setPixmap(self.pixmap)
def some_calculation(self):
self.draw_cross()
A user on Stackoverflow suggested me with the following code but it also doesn’t work
def init_cross(self):
#! Set the target image
self.pixmap = self.label.pixmap()
def draw_cross(self):
#! draw cross
self.painter = QtGui.QPainter(self.pixmap)
self.painter.setPen(QtGui.QPen(QtGui.QColor('red'), 5))
self.painter.drawLine(100, 100, 110 + CROSS_SIZE, 110 + CROSS_SIZE)
self.painter.drawLine(100, 100 + CROSS_SIZE, 110 + CROSS_SIZE, 110)
self.painter.end()
self.label.setPixmap(self.pixmap)
Can someone help me please.