I have some trouble using QVariantAnimation. I've been searching exactly how to use it, but I just don't understand what to do. The documentation and the examples that I read are confusing me more.
I have a list of png images, and I want them to move from point A to point B and change the image depending on the step that I define, within a certain time.
An example would be, Point A is (0, 0) - Point B is (6, 0) and step is (2, 0).
From (0, 0) to (2, 0) display Image1
From (2, 0) to (4, 0) display Image2
From (4, 0) to (6, 0) display Image3
Giving this example as I don't understand QVariantAnimation past giving them the start and the end points.
There are several ways to implement what you point out(the distance is very small so I have changed the dimensions).
This method for each section is a QVariantAnimation that establishes a new QPixmap.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
import random
def create_pixmap():
pixmap = QtGui.QPixmap(QtCore.QSize(20, 20))
pixmap.fill(QtGui.QColor(*random.sample(range(255), 3)))
return pixmap
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.m_scene = QtWidgets.QGraphicsScene(
QtCore.QRectF(-200, -200, 400, 400), self
)
view = QtWidgets.QGraphicsView(self.m_scene)
self.setCentralWidget(view)
self.m_pixmap_item = QtWidgets.QGraphicsPixmapItem()
self.m_scene.addItem(self.m_pixmap_item)
datas = [
(QtCore.QPointF(0, 40), create_pixmap()),
(QtCore.QPointF(0, 80), create_pixmap()),
(QtCore.QPointF(0, 120), create_pixmap()),
]
self.data_iter = iter(datas)
self.move()
def move(self):
try:
end_point, pixmap = next(self.data_iter)
self.m_pixmap_item.setPixmap(pixmap)
animation = QtCore.QVariantAnimation(
duration=500,
valueChanged=self.m_pixmap_item.setPos,
finished=self.move,
startValue=self.m_pixmap_item.pos(),
endValue=end_point,
parent=self,
)
animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
except StopIteration:
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
In this method, the itemChange method is overridden so that it changes from QPixmap to each section.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
import random
def create_pixmap():
pixmap = QtGui.QPixmap(QtCore.QSize(20, 20))
pixmap.fill(QtGui.QColor(*random.sample(range(255), 3)))
return pixmap
class GraphicsPixmapItem(QtWidgets.QGraphicsPixmapItem):
def __init__(self, parent=None):
super(GraphicsPixmapItem, self).__init__(parent)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)
self.m_pixmaps = [create_pixmap() for _ in range(3)]
def itemChange(self, change, value):
if change == QtWidgets.QGraphicsItem.ItemPositionChange:
y = self.pos().y()
if 0 <= y < 40:
self.change_pixmap(self.m_pixmaps[0])
elif 40 <= y < 80:
self.change_pixmap(self.m_pixmaps[1])
elif 80 <= y < 120:
self.change_pixmap(self.m_pixmaps[2])
return super(GraphicsPixmapItem, self).itemChange(change, value)
def change_pixmap(self, pixmap):
if self.pixmap() != pixmap:
self.setPixmap(pixmap)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.m_scene = QtWidgets.QGraphicsScene(
QtCore.QRectF(-200, -200, 400, 400), self
)
view = QtWidgets.QGraphicsView(self.m_scene)
self.setCentralWidget(view)
self.m_pixmap_item = GraphicsPixmapItem()
self.m_scene.addItem(self.m_pixmap_item)
animation = QtCore.QVariantAnimation(
duration=3000,
valueChanged=self.m_pixmap_item.setPos,
startValue=self.m_pixmap_item.pos(),
endValue=QtCore.QPointF(0, 120),
parent=self,
)
animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Related
In below figure I have QGraphicsPathItem on scene as red portion and override it's shape as blue portion. I want when the red space is dragged and moved then the item is lengthened or shortened linearly, and when the blue space is dragged then the entire item must be moved.
Here is what I tried...
import sys
from PyQt5.QtCore import QRectF, Qt, QPointF
from PyQt5.QtGui import QPainterPath, QPen, QPainterPathStroker, QPainter
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsPathItem, QGraphicsItem
class Item(QGraphicsPathItem):
circle = QPainterPath()
circle.addEllipse(QRectF(-5, -5, 10, 10))
def __init__(self):
super(Item, self).__init__()
self.setPath(Item.circle)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
def paint(self, painter, option, widget):
color = Qt.red if self.isSelected() else Qt.black
painter.setPen(QPen(color, 2, Qt.SolidLine))
painter.drawPath(self.path())
# To paint path of shape
painter.setPen(QPen(Qt.blue, 1, Qt.SolidLine))
painter.drawPath(self.shape())
def shape(self):
startPoint = self.mapFromScene(self.pos())
endPoint = self.mapFromScene(QPointF(10, 10))
path = QPainterPath(startPoint)
path.lineTo(endPoint)
stroke = QPainterPathStroker()
stroke.setWidth(10)
return stroke.createStroke(path)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
window.show()
scene = QGraphicsScene()
scene.setSceneRect(0, 0, 200, 200)
view = QGraphicsView()
view.setScene(scene)
window.setCentralWidget(view)
scene.addItem(Item())
sys.exit(app.exec_())
I am getting output as disturbed path
Handling the task of resizing and stretching in the same item is complicated, so to avoid it I have used 2 items: A handle and a Pipe. Thus each one manages his own task and updates the position of the other elements:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class HandleItem(QtWidgets.QGraphicsPathItem):
def __init__(self, parent=None):
super().__init__(parent)
path = QtGui.QPainterPath()
path.addEllipse(QtCore.QRectF(-5, -5, 10, 10))
self.setPath(path)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)
self._pipe_item = None
#property
def pipe_item(self):
return self._pipe_item
#pipe_item.setter
def pipe_item(self, item):
self._pipe_item = item
def itemChange(self, change, value):
if change == QtWidgets.QGraphicsItem.ItemPositionChange and self.isEnabled():
ip = self.pipe_item.mapFromScene(value)
self.pipe_item.end_pos = ip
elif change == QtWidgets.QGraphicsItem.ItemSelectedChange:
color = QtCore.Qt.red if value else QtCore.Qt.black
self.setPen(QtGui.QPen(color, 2, QtCore.Qt.SolidLine))
return super().itemChange(change, value)
class PipeItem(QtWidgets.QGraphicsPathItem):
def __init__(self, parent=None):
super().__init__(parent)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)
self._end_pos = QtCore.QPointF()
self._handle = HandleItem()
self.handle.pipe_item = self
self.end_pos = QtCore.QPointF(10, 10)
self.handle.setPos(self.end_pos)
self.setPen(QtGui.QPen(QtCore.Qt.blue, 1, QtCore.Qt.SolidLine))
#property
def handle(self):
return self._handle
#property
def end_pos(self):
return self._end_pos
#end_pos.setter
def end_pos(self, p):
path = QtGui.QPainterPath()
path.lineTo(p)
stroke = QtGui.QPainterPathStroker()
stroke.setWidth(10)
self.setPath(stroke.createStroke(path))
self._end_pos = p
def paint(self, painter, option, widget):
option.state &= ~QtWidgets.QStyle.State_Selected
super().paint(painter, option, widget)
def itemChange(self, change, value):
if change == QtWidgets.QGraphicsItem.ItemSceneHasChanged:
if self.scene():
self.scene().addItem(self.handle)
elif change == QtWidgets.QGraphicsItem.ItemPositionChange and self.isEnabled():
p = self.mapToScene(self.end_pos)
self.handle.setPos(p)
return super().itemChange(change, value)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene(sceneRect=QtCore.QRectF(0, 0, 200, 200))
item = PipeItem()
scene.addItem(item)
view = QtWidgets.QGraphicsView(scene)
window = QtWidgets.QMainWindow()
window.setCentralWidget(view)
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
UPDATE:
If you want the logic you want to be implemented then it is more complicated. The cause of the error is that the paint() method uses the boundingRect() to set the paint area, but in your case it does not take into account that it varies, a possible solution is the following:
class Item(QGraphicsPathItem):
circle = QPainterPath()
circle.addEllipse(QRectF(-5, -5, 10, 10))
# ...
def boundingRect(self):
return self.shape().boundingRect()
I want to display an image in a QGraphicsView, actually in QGraphicsScene, this is the easy part, bu, when I move the cursor over the image, I want to see the X and Y coordinates lines (the yellow lines), like in this image, can anyone explain me how to do this?
To implement what you want there are 2 tasks:
Obtain the position of the cursor, for this case the flag mouseTracking is enabled so that mouseMoveEvent() is called where the position is obtained.
Paint on the top layer, for this we use the drawForeground() function.
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsScene(QtWidgets.QGraphicsScene):
def drawForeground(self, painter, rect):
super(GraphicsScene, self).drawForeground(painter, rect)
if not hasattr(self, "cursor_position"):
return
painter.save()
pen = QtGui.QPen(QtGui.QColor("yellow"))
pen.setWidth(4)
painter.setPen(pen)
linex = QtCore.QLineF(
rect.left(),
self.cursor_position.y(),
rect.right(),
self.cursor_position.y(),
)
liney = QtCore.QLineF(
self.cursor_position.x(),
rect.top(),
self.cursor_position.x(),
rect.bottom(),
)
for line in (linex, liney):
painter.drawLine(line)
painter.restore()
def mouseMoveEvent(self, event):
self.cursor_position = event.scenePos()
self.update()
super(GraphicsScene, self).mouseMoveEvent(event)
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
self.setMouseTracking(True)
scene = GraphicsScene(QtCore.QRectF(-200, -200, 400, 400), self)
self.setScene(scene)
if __name__ == "__main__":
import sys
import random
app = QtWidgets.QApplication(sys.argv)
w = GraphicsView()
for _ in range(4):
r = QtCore.QRectF(
*random.sample(range(-200, 200), 2),
*random.sample(range(50, 150), 2)
)
it = w.scene().addRect(r)
it.setBrush(QtGui.QColor(*random.sample(range(255), 3)))
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
I want to move the QLabel with the mouse movement (not like Drag&drop, 'object' disappears while moving). Clicked - moved - released. I did it to some extent, but I ran into a problem. QLabel shrinks as I move it or even disappears (like shrinks to 0 width). How to fix it or what more correct approach to do it?
(self.label_pos is needed to keep the mouse position relative inside self.label)
Or its just monitor's refresh rate issue? But in photoshop's gradient editor, that little color stop isn't shrikns. It's choppy because of refresh rate, but always the same size.
This is what I want to see, recorded using a screen capture program. The same thing I see in Photoshop
This is what I see, recorded on my phone. The quality is poor, but the difference is clearly visible anyway.
This Photoshop is also captured on my phone, here the “object” remains the same size, as in the example made using screen capture
Here is code from eyllanesc's answer, 'object' still shrinks :(
self.label = QLabel(self)
self.label.move(100, 100)
self.label.mousePressEvent = self.mouse_on
self.label.mouseReleaseEvent = self.mouse_off
def mouse_on(self, event):
self.bool = True
self.label_pos = event.pos()
def mouse_off(self, event):
self.bool = False
def mouseMoveEvent(self, event):
if self.bool:
self.label.move(event.x()-self.label_pos.x(), event.y()-self.label_pos.y())
Instead of using a QLabel I recommend using QGraphicsRectItem with a QGraphicsView since it is specialized in this type of tasks:
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setScene(QtWidgets.QGraphicsScene(self))
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
brush = QtWidgets.QApplication.palette().brush(QtGui.QPalette.Window)
self.setBackgroundBrush(brush)
rect_item = self.scene().addRect(
QtCore.QRectF(QtCore.QPointF(), QtCore.QSizeF(40, 80))
)
rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
rect_item.setBrush(QtGui.QBrush(QtGui.QColor("red")))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.setFixedSize(640, 480)
w.show()
sys.exit(app.exec_())
If you want to just scroll horizontally then overwrite the itemChange method of QGraphicsItem:
from PyQt5 import QtCore, QtGui, QtWidgets
class HorizontalItem(QtWidgets.QGraphicsRectItem):
def __init__(self, rect, parent=None):
super(HorizontalItem, self).__init__(rect, parent)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)
def itemChange(self, change, value):
if (
change == QtWidgets.QGraphicsItem.ItemPositionChange
and self.scene()
):
return QtCore.QPointF(value.x(), self.pos().y())
return super(HorizontalItem, self).itemChange(change, value)
class Widget(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setScene(QtWidgets.QGraphicsScene(self))
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
brush = QtWidgets.QApplication.palette().brush(QtGui.QPalette.Window)
self.setBackgroundBrush(brush)
rect_item = HorizontalItem(
QtCore.QRectF(QtCore.QPointF(), QtCore.QSizeF(40, 80))
)
rect_item.setBrush(QtGui.QBrush(QtGui.QColor("red")))
self.scene().addItem(rect_item)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.setFixedSize(640, 480)
w.show()
sys.exit(app.exec_())
In the following code there is an example similar to what you want:
from PyQt5 import QtCore, QtGui, QtWidgets
class HorizontalItem(QtWidgets.QGraphicsRectItem):
def __init__(self, rect, parent=None):
super(HorizontalItem, self).__init__(rect, parent)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)
def itemChange(self, change, value):
if (
change == QtWidgets.QGraphicsItem.ItemPositionChange
and self.scene()
):
return QtCore.QPointF(value.x(), self.pos().y())
return super(HorizontalItem, self).itemChange(change, value)
class Widget(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setScene(QtWidgets.QGraphicsScene(self))
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
brush = QtWidgets.QApplication.palette().brush(QtGui.QPalette.Window)
self.setBackgroundBrush(brush)
self.setFixedSize(640, 480)
size = self.mapToScene(self.viewport().rect()).boundingRect().size()
r = QtCore.QRectF(QtCore.QPointF(), size)
self.setSceneRect(r)
rect = QtCore.QRectF(
QtCore.QPointF(), QtCore.QSizeF(0.8 * r.width(), 80)
)
rect.moveCenter(r.center())
rect_item = self.scene().addRect(rect)
rect_item.setBrush(QtGui.QBrush(QtGui.QColor("salmon")))
item = HorizontalItem(
QtCore.QRectF(
rect.bottomLeft() + QtCore.QPointF(0, 20), QtCore.QSizeF(20, 40)
)
)
item.setBrush(QtGui.QBrush(QtGui.QColor("red")))
self.scene().addItem(item)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
I have the following PyQtGraph program, which makes a red square "move" when moving a slider:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QSlider
from pyqtgraph import (
mkBrush,
mkPen,
GraphicsObject,
QtGui,
PlotWidget,
)
class SquareItem(GraphicsObject):
def __init__(self):
super().__init__()
self.position_picture = QtGui.QPicture()
def paint(self, p, *args):
p.drawPicture(0, 0, self.position_picture)
def boundingRect(self):
return QtCore.QRectF(-5, -5, 20, 10)
def update_position(self, x):
self.position_picture = QtGui.QPicture()
painter = QtGui.QPainter(self.position_picture)
painter.scale(1, -1)
painter.setBrush(mkBrush('r'))
painter.setPen(mkPen(None))
painter.drawRect(QtCore.QRectF(x, 0, 1, 1))
painter.end()
self.informViewBoundsChanged()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = SquareItem()
self.graphics.addItem(self.item)
self.slider = QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QSlider.TicksAbove)
self.slider.valueChanged.connect(self.slider_value_changed)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
def slider_value_changed(self, value):
self.item.update_position(value)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
Everything seems to work fine, but if I zoom in/out and then move the slider again the square position is no longer updated (i.e.: the square is not re-drawn).
How can I fix that?
Updates
I am using a square to simplify the problem. In reality, I do not only change position, but I can also draw different shapes, so using setPos() is not really an option.
You should not update the painting if you want to change position, you should only use setPos(). the paint() function takes boundingRect() as a reference so when moving the graph you are moving it in that coordinate system instead of the coordinate system of PlotWidget.
class SquareItem(GraphicsObject):
def paint(self, p, *args):
p.setBrush(mkBrush('r'))
p.setPen(mkPen(None))
p.drawRect(self.boundingRect())
def boundingRect(self):
return QtCore.QRectF(0, 0, 1, 1)
def update_position(self, x):
self.setPos(x, 0)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = SquareItem()
self.graphics.addItem(self.item)
self.graphics.setRange(rect=QtCore.QRectF(-10, -10, 20, 20))
self.slider = QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QSlider.TicksAbove)
self.slider.valueChanged.connect(self.slider_value_changed)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
def slider_value_changed(self, value):
self.item.update_position(value)
If you are not going to use signals it is advisable to use objects that inherit from QGraphicsItem instead of QGraphicsObject, for example you could use QGraphicsRectItem:
import sys
from PyQt5 import QtCore, QtWidgets
from pyqtgraph import (
mkBrush,
mkPen,
GraphicsObject,
QtGui,
PlotWidget,
)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = QtWidgets.QGraphicsRectItem(0, 0, 1, 1)
self.item.setBrush(mkBrush('r'))
self.item.setPen(mkPen(None))
self.graphics.addItem(self.item)
self.graphics.setRange(rect=QtCore.QRectF(-10, -10, 20, 20))
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QtWidgets.QSlider.TicksAbove)
self.slider.valueChanged.connect(self.slider_value_changed)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
def slider_value_changed(self, value):
self.item.setPos(value, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
update:
If you want to redraw you should call update():
import sys
from PyQt5 import QtCore, QtWidgets
from pyqtgraph import (
mkBrush,
mkPen,
GraphicsObject,
QtGui,
PlotWidget,
)
class SquareItem(GraphicsObject):
colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'w', 'FF0', 'AA0', '0AA']
def __init__(self):
super().__init__()
self.mColor = SquareItem.colors[0]
def paint(self, p, *args):
p.setBrush(mkBrush(self.mColor))
p.setPen(mkPen(None))
p.drawRect(self.boundingRect())
def boundingRect(self):
return QtCore.QRectF(0, 0, 1, 1)
def update_draw(self, x):
self.mColor = SquareItem.colors[x]
self.update()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = SquareItem()
self.graphics.addItem(self.item)
self.graphics.setRange(rect=QtCore.QRectF(-10, -10, 20, 20))
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QtWidgets.QSlider.TicksAbove)
self.slider.valueChanged.connect(self.item.update_draw)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
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_())