[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: help on paint event
From:       Valentin Valls <valentin.valls () esrf ! fr>
Date:       2020-10-29 18:28:22
Message-ID: c8c84af7e4d32e9a491d8c0ba741aacf () esrf ! fr
[Download RAW message or body]

Hi, 

Did you try to use QPainter(self)? 

I have noticed you have commented painter.begin. 

Here you have 2 examples on how to use it with paintEvent
https://doc.qt.io/qt-5/qpainter.html#QPainter-1

Regards, 

On 29.10.2020 18:01, Luca Bertolotti wrote:

> Hello i'm not able to paint on my widget: 
> i wrote this but nothing is painted: 
> # -*- coding: utf-8 -*-
> 
> """
> Module implementing Form.
> """
> 
> from PyQt5.QtCore import pyqtSlot, QRect
> from PyQt5.QtWidgets import QWidget, QFileDialog
> from PyQt5.QtGui import QPainter, QBrush, QPen
> from PyQt5.QtCore import Qt
> import ezdxf
> #from ezdxf.addons.drawing import matplotlib
> #from ezdxf.groupby import groupby
> import math
> 
> from Ui_form import Ui_Form
> 
> class Form(QWidget, Ui_Form):
> """
> Class documentation goes here.
> """
> def __init__(self, parent=None):
> """
> Constructor
> 
> @param parent reference to the parent widget
> @type QWidget
> """
> super(Form, self).__init__(parent)
> self.setupUi(self)
> self.arco = False
> self.line = False
> 
> @pyqtSlot()
> def on_pushButton_clicked(self):
> """
> Slot documentation goes here.
> """
> # TODO: not implemented yet
> #raise NotImplementedError
> self.carico_file()
> 
> @pyqtSlot()
> def on_pushButton_2_clicked(self):
> """
> Slot documentation goes here.
> """
> # TODO: not implemented yet
> raise NotImplementedError
> 
> def carico_file(self):
> fname = QFileDialog.getOpenFileName(self, 'Open file', None, ("Drawing (*.dxf )"))
> doc = ezdxf.readfile(fname[0])
> msp = doc.modelspace()
> lista_disegno = []
> a = 0
> for entity in msp:
> self.arco = False
> self.linea =False
> print(entity.dxfattribs())
> if entity.dxftype() == 'LINE':
> linea_x = entity.dxf.end[0]-entity.dxf.start[0]
> linea_y = entity.dxf.end[1]-entity.dxf.start[1]
> lista_disegno.append('linea_x'+str(a)+': '+str(linea_x))
> lista_disegno.append('linea_y'+str(a)+': '+str(linea_y))
> self.x1 = entity.dxf.start[0]
> self.y1 = entity.dxf.start[1]
> self.x2 = entity.dxf.end[0]
> self.y2 = entity.dxf.end[1]
> print('linea', self.x1, self.y1, self.x2, self.y2)
> a = a+1
> self.linea = True
> self.update()
> if entity.dxftype()=='ARC':
> angolo_tot_gradi = entity.dxf.end_angle-entity.dxf.start_angle
> lung_arco= 2*math.pi*entity.dxf.radius/360*angolo_tot_gradi
> lista_disegno.append('arco'+str(a)+': '+str(lung_arco))
> self.startangle = entity.dxf.start_angle
> self.spanangle = lung_arco
> xr = entity.dxf.center[0]
> yr = entity.dxf.center[1]
> r = entity.dxf.radius
> self.rectangle = QRect(xr, yr, 2*r, 2*r)
> self.arco = True
> #self.update()
> #self.draw_arc(rectangle, startangle, spanangle)
> print(lista_disegno)
> self.update()
> 
> def paintEvent(self, event):
> if self.arco == True:
> qp = QPainter()
> #qp.begin(self)
> pen = QPen(Qt.red, 2, Qt.SolidLine)
> qp.setPen(pen)
> qp.drawArc(self.rectangle, self.startangle, self.spanangle)
> if self.line == True:
> qp = QPainter()
> pen = QPen(Qt.red, 2, Qt.SolidLine)
> qp.setPen(pen)
> qp.drawLine(self.x1, self.y1, self.x2, self.y2)
[Attachment #3 (unknown)]

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" \
/></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'> \
<p>Hi,</p> <p>Did you try to use QPainter(self)?</p>
<p>I have noticed you have commented painter.begin.</p>
<p>Here you have 2 examples on how to use it with paintEvent <a \
href="https://doc.qt.io/qt-5/qpainter.html#QPainter-1">https://doc.qt.io/qt-5/qpainter.html#QPainter-1</a></p>
 <div>&nbsp;</div>
<p>Regards,</p>
<p>On 29.10.2020 18:01, Luca Bertolotti wrote:</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; \
margin: 0"><!-- html ignored --><!-- head ignored --><!-- meta ignored --> <div \
dir="ltr">Hello i'm not able to paint on my widget: <div>i wrote this but nothing is \
painted:</div> <div># -*- coding: utf-8 -*-<br /><br />"""<br />Module implementing \
Form.<br />"""<br /><br />from PyQt5.QtCore import pyqtSlot, QRect<br />from \
PyQt5.QtWidgets import QWidget, QFileDialog<br />from PyQt5.QtGui import QPainter, \
QBrush, QPen<br />from PyQt5.QtCore import Qt<br />import ezdxf<br />#from \
ezdxf.addons.drawing import matplotlib<br />#from ezdxf.groupby import groupby<br \
/>import math<br /><br /><br />from Ui_form import Ui_Form<br /><br /><br />class \
Form(QWidget, Ui_Form):<br />&nbsp; &nbsp; """<br />&nbsp; &nbsp; Class documentation \
goes here.<br />&nbsp; &nbsp; """<br />&nbsp; &nbsp; def __init__(self, \
parent=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; """<br />&nbsp; &nbsp; &nbsp; &nbsp; \
Constructor<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; @param \
parent reference to the parent widget<br />&nbsp; &nbsp; &nbsp; &nbsp; @type \
QWidget<br />&nbsp; &nbsp; &nbsp; &nbsp; """<br />&nbsp; &nbsp; &nbsp; &nbsp; \
super(Form, self).__init__(parent)<br />&nbsp; &nbsp; &nbsp; &nbsp; \
self.setupUi(self)<br />&nbsp; &nbsp; &nbsp; &nbsp; self.arco = False<br />&nbsp; \
&nbsp; &nbsp; &nbsp; self.line = False<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; \
@pyqtSlot()<br />&nbsp; &nbsp; def on_pushButton_clicked(self):<br />&nbsp; &nbsp; \
&nbsp; &nbsp; """<br />&nbsp; &nbsp; &nbsp; &nbsp; Slot documentation goes here.<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; """<br />&nbsp; &nbsp; &nbsp; &nbsp; # TODO: not \
implemented yet<br />&nbsp; &nbsp; &nbsp; &nbsp; #raise NotImplementedError<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; self.carico_file()<br />&nbsp; &nbsp; &nbsp; &nbsp; <br \
/>&nbsp; &nbsp; <br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @pyqtSlot()<br />&nbsp; \
&nbsp; def on_pushButton_2_clicked(self):<br />&nbsp; &nbsp; &nbsp; &nbsp; """<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; Slot documentation goes here.<br />&nbsp; &nbsp; &nbsp; \
&nbsp; """<br />&nbsp; &nbsp; &nbsp; &nbsp; # TODO: not implemented yet<br />&nbsp; \
&nbsp; &nbsp; &nbsp; raise NotImplementedError<br /><br /><br />&nbsp; &nbsp; def \
carico_file(self):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fname = \
QFileDialog.getOpenFileName(self, 'Open file', None, ("Drawing (*.dxf )"))<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc = ezdxf.readfile(fname[0])<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msp = doc.modelspace()<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; lista_disegno = []<br />&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; a = 0<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for entity in \
msp:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.arco = \
False<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.linea \
=False<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
print(entity.dxfattribs())<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; if entity.dxftype() == 'LINE':<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; linea_x = entity.dxf.end[0]-entity.dxf.start[0]<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linea_y = \
entity.dxf.end[1]-entity.dxf.start[1]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; lista_disegno.append('linea_x'+str(a)+': \
'+str(linea_x))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; lista_disegno.append('linea_y'+str(a)+': '+str(linea_y))<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.x1 = \
entity.dxf.start[0]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; self.y1 = entity.dxf.start[1]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.x2 = entity.dxf.end[0]<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.y2 = \
entity.dxf.end[1]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; print('linea', self.x1, self.y1, self.x2, self.y2)<br />&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = a+1<br />&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.linea = True<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.update()<br />&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if entity.dxftype()=='ARC':<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
angolo_tot_gradi = entity.dxf.end_angle-entity.dxf.start_angle<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lung_arco= \
2*math.pi*entity.dxf.radius/360*angolo_tot_gradi<br />&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lista_disegno.append('arco'+str(a)+': \
'+str(lung_arco))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; self.startangle = entity.dxf.start_angle<br />&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.spanangle = lung_arco<br />&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xr = \
entity.dxf.center[0]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; yr = entity.dxf.center[1]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = entity.dxf.radius<br />&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rectangle = QRect(xr, yr, 2*r, \
2*r)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
self.arco = True<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; #self.update()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; #self.draw_arc(rectangle, startangle, spanangle)<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(lista_disegno)<br />&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.update()<br /><br />&nbsp; &nbsp; def \
paintEvent(self, event):<br />&nbsp; &nbsp; &nbsp; &nbsp; if self.arco == True:<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qp = QPainter()<br />&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; #qp.begin(self)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
pen = QPen(Qt.red, 2, Qt.SolidLine)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
qp.setPen(pen)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
qp.drawArc(self.rectangle, self.startangle, self.spanangle)<br />&nbsp; &nbsp; &nbsp; \
&nbsp; if self.line == True:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qp = \
QPainter()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pen = QPen(Qt.red, 2, \
Qt.SolidLine)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qp.setPen(pen)<br \
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qp.drawLine(self.x1, self.y1, self.x2, \
self.y2)</div> </div>
</blockquote>
</body></html>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic