S
S
shrimo2013-01-26 02:43:35
Python
shrimo, 2013-01-26 02:43:35

Display generated objects on the fly

How to implement (pyqt) display of generated objects (drawEllipse) at the time of their creation (on the fly)? This code draws objects in memory and then displays an image:

import sys
import random
from PyQt4 import QtGui, QtCore

width=800
height=600

class DrawExample(QtGui.QWidget):
    
    def __init__(self):
        super(DrawExample, self).__init__()
        
        self.initUI()
        
    def initUI(self):      

        self.setGeometry(300, 300, width, height)
        self.setWindowTitle('Draw')
        self.show()

    def paintEvent(self, e):
        
        qp = QtGui.QPainter()
        qp.begin(self)
        
        for i in range(10000):
            x = random.randint(1, width-1)
            y = random.randint(1, height-1)
            color = QtGui.QColor(250,100,100)
            qp.setPen(color)            
            qp.drawEllipse(x-3, y-3, 6, 6)
            
        qp.end()
               
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = DrawExample()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()


Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alz, 2013-01-26
@alz

#!/usr/bin/env python

import sys
import random
from PyQt4 import QtCore, QtGui

WIDTH = 800
HEIGHT = 600

class DrawExample(QtGui.QWidget):
    def __init__(self):
        super(DrawExample, self).__init__()
        
        self.points = []
        
        self.init_ui()
        
        self.startTimer(0)
    
    def init_ui(self):
        self.setGeometry(300, 300, WIDTH, HEIGHT)
        self.setWindowTitle('Draw')
        self.show()
    
    def timerEvent(self, e):
        if len(self.points) < 10000:
            self.points.append((
                random.randint(1, WIDTH-1),
                random.randint(1, HEIGHT-1),
            ))
            self.update()
    
    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        
        color = QtGui.QColor(250, 100, 100)
        qp.setPen(color)
        
        for point in self.points:
            qp.drawEllipse(point[0]-3, point[1]-3, 6, 6)
        
        qp.end()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = DrawExample()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

L
Lakewake, 2013-01-26
@vedmaka

I will assume that qp.begin(self) and qp.end() should be placed in a loop

S
shrimo, 2013-01-26
@shrimo

I will assume that qp.begin (self) and qp.end () should be placed in a loop
Checked: does not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question