import sys, csv
from PySide import QtGui, QtCore
from mainStrato import *
X_START = 10
Y_START = 15
MAX_WIDTH = 1350
MAX_LENGH = 1650
ZOOM = 2.5
WIDTH_PEZZO = 150
LENGH_PEZZO = 600
CENTER_OFFSET_X = 15
CENTER_OFFSET_Y = 0
class Strato(QtGui.QMainWindow, Ui_MainWindow):
#Apre il file CSV e copia le singole righe in una lista
def __init__(self, parent=None):
super(Strato, self).__init__(parent)
self.setupUi(self)
def paintEvent(centralwidget, e):
qp = QtGui.QPainter()
qp.begin(centralwidget)
print "paint event"
qp.end()
self.drawRectangles(qp)
def drawRectangles(self, qp):
color = QtGui.QColor(0, 0, 0)
color.setNamedColor('#d4d4d4')
qp.setPen(color)
qp.setBrush(QtGui.QColor(200, 0, 0))
coordCarro = QtCore.QRectF(X_START, Y_START, MAX_WIDTH/ZOOM, MAX_LENGH/ZOOM)
qp.drawRect(coordCarro)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Strato()
window.show()
sys.exit(app.exec_())
mainstrato
is a file generated from pyside-uic
. I got an error Object not defined
on calling self.drawRectangles
or any other function in Strato called inside paint event? If I copy the drawRectangles
code in paint event it works!
Suggestion? What's wrong?
You are missing self
in the paintEvent
definition - and I assume that centralwidget
is part of your UI class, so that should be accessible through self.centralwidget
(since you are inheriting from your UI class). Besides self
, paintEvent
has only one parameter, the event object. Also, you must not call qp.end()
before you have drawn your rectangles. Finally, you need to properly indent your code - but that could also be a copy&paste issue when posting the question. Try
def paintEvent(self, e):
qp = QtGui.QPainter()
qp.begin(self.centralwidget)
print "paint event"
self.drawRectangles(qp)
qp.end()
And, finally, you should not paint on other widgets from within one Widget's paintEvent
. Instead, subclass QWidget
and overide its paint event. The following sscce works (all non-relevant code removed):
#!/usr/bin/python
import sys
from PySide import QtGui, QtCore
X_START = 10
Y_START = 15
MAX_WIDTH = 1350
MAX_LENGH = 1650
ZOOM = 2.5
WIDTH_PEZZO = 150
LENGH_PEZZO = 600
CENTER_OFFSET_X = 15
CENTER_OFFSET_Y = 0
class PaintWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(PaintWidget, self).__init__(parent)
def paintEvent(self, e):
qp = QtGui.QPainter(self)
print("paint event")
self.drawRectangles(qp)
def drawRectangles(self, qp):
color = QtGui.QColor(0, 0, 0)
color.setNamedColor('#d4d4d4')
qp.setPen(color)
qp.setBrush(QtGui.QColor(200, 0, 0))
coordCarro = QtCore.QRectF(X_START, Y_START, MAX_WIDTH/ZOOM, MAX_LENGH/ZOOM)
qp.drawRect(coordCarro)
class Strato(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Strato, self).__init__(parent)
self.centralwidget = PaintWidget(self)
self.setCentralWidget(self.centralwidget)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Strato()
window.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.