无油漆画 [英] Painting without paintEvent

查看:232
本文介绍了无油漆画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有画布窗口小部件的GUI。所以我需要用点感觉这个画布。问题是我需要创建一个独立的进程来运行GUI,因为这些点的(x,y)是从其他类生成的。所以我无法弄清楚我如何从 paintEvent 外部绘制点,或者我如何(如有必要)触发 paintEvent 再次.-。



编辑:



我需要运行一个遗传算法,每一代的结果,并将其在画布区域作为折线图表示!但是我需要在飞行中绘制/绘制结果。



所以每次GA完成健身周期时,我需要将这个结果发送到画布区域。



我从 http ://zetcode.com/tutorials/pyqt4/drawing/ 并修改了一点!

 #!/ usr / bin / python 
# - * - 编码:utf-8 - * -


ZetCode PyQt4教程

在示例中,我们在窗口上随机抽取1000个红点


作者:Jan Bodnar
网站:zetcode.com
最后编辑:2011年9月


import sys,random,time
从多进程导入进程
从PyQt4导入QtGui,QtCore

类示例(QtGui.QWidget):

def status(self,text):

print'[GUI]',text

def __init __(self):

super(Example,self).__ init __()

self.initUI()

def initUI(self):

self。 status('init ui')

self.setGeometry(300,300,280,170)
self.setWindowTitle('Points')

self.status('显示小部件')

self.show()

def paintEvent(self ,e):

self.status('using with types:'+ str(e))

self.status('paint event was called')$ b $ ($)

#self.drawPoints(qp)

#self.drawRectangles() qp)

self.draw_all_axis(qp,300)

self.draw_dot(20,20)

qp.end()

def draw_all_axis(self,qp,length):

self.draw_x_axis(qp,length)
self.draw_y_axis(qp,length)

def draw_x_axis(self,qp,length):

color = QtGui.QColor(0,0,0)
color.setNamedColor('#d49EBD8')
qp。 setPen(color)

qp.setBrush(QtGui.QColor(73,235,216))
qp.drawLine(10,length,l

def draw_y_axis(self,qp,length):

color = QtGui.QColor(0,0,0)
color.setNamedColor '#d49EBD8')
qp.setPen(color)

qp.setBrush(QtGui.QColor(73,235,216))
qp.drawLine(10,10, 10,length)

def draw_dot(self,x,y):

qp = QtGui.QPainter()
qp.begin(self)

color = QtGui.QColor(0,0,0)
color.setNamedColor('#d4d4d4')
qp.setPen(color)

qp。 setBrush(QtGui.QColor(200,0,0))
qp.drawRect(x,y,x + 0.25,y + 0.25)

def drawPoints(self,qp) b
$ b qp.setPen(QtCore.Qt.red)
size = self.size()

对于范围(1000)中的i:

x = random.randint(1,size.width() - 1)

y = random.randint(1,size.height() - 1)

qp。 drawPoint(x,y)

d ef drawRectangles(self,qp):

color = QtGui.QColor(0,0,0)
color.setNamedColor('#d4d4d4')
qp.setPen )

qp.setBrush(QtGui.QColor(200,0,0))
qp.drawRect(10,15,90,60)

qp。 setBrush(QtGui.QColor(255,80,0,160))
qp.drawRect(130,15,90,60)

qp.setBrush(QtGui.QColor(25,0 ,90,200))
qp.drawRect(250,15,90,60)

def external_dot_drawer(main_window):

$ b在范围(20,100):

main_window.draw_dot(i,i)


def main():

打印'启动主窗口obj作为应用程序'
sys.exit(app.exec_())
打印'完成!'

如果__name__ =='__main__':

打印'加载应用程序抽象'
app = QtGui.QApplication(sys.argv)

打印'构建主窗口obj'
gui = Example()

pri nt'准备启动窗口作为一个分离的进程'
p_main =进程(name ='window',target = main,args =())

打印'运行新进程'
p_main.start()

time.sleep(3)

打印'试图触发paintEvent'
p_external_dot_drawer = Process(name ='extern_dot_drawer' target = external_dot_drawer,args =(gui))


解决方案

调用paintEvent的正确方法是使用QWidget.update()。



我还建议使用任何现有的Python绘图库,而不是编写自己的图书馆。 PyQtGraph有一些不错的多进程功能:

  import pyqtgraph as pg 
app = pg.QtGui.QApplication([ ])
import pyqtgraph.multiprocess as mp

##启动子进程
proc = mp.QtProcess()

## import pyqtgraph in the子进程,创建一个绘图窗口
remotepg = proc._import('pyqtgraph')
win = remotepg.plot()

##创建散点图
plot = win.plot([1,4,2,3],[4,6,3,4],pen = None,symbol ='o')

## ..after一些处理,更新情节数据
plot.setData(x = [1,2,3,4],y = [5,7,2,3])


I've a GUI with a canvas widget. So i need to feel this canvas with points. The problem is that i need to create a separated process to run GUI because the (x,y) for the points are generated from other class. So i cant figure out how can i draw this points from outside of the paintEvent or how can i (if necessary) trigger the paintEvent again .-.

edit :

I need to run a genetic algorithm and get the fitness result of each generation and represent it on the canvas area as a line chart! But i need to plot/draw the results on the fly.

So every time the GA completes a fitness cycle i need to send this results to the canvas area.

I've got this example code from http://zetcode.com/tutorials/pyqt4/drawing/ and modified a little!

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In the example, we draw randomly 1000 red points 
on the window.

author: Jan Bodnar
website: zetcode.com 
last edited: September 2011
"""

import sys, random , time
from multiprocessing import Process
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def status( self , text ):

        print '[GUI] ', text

    def __init__(self):

        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.status('init ui')

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Points')

        self.status('showing widgets')

        self.show()

    def paintEvent(self, e):

        self.status( 'playing with types : '+str(e) )

        self.status('paint event was called')

        qp = QtGui.QPainter()
        qp.begin(self)

        #self.drawPoints(qp)

        #self.drawRectangles(qp)

        self.draw_all_axis(qp, 300)

        self.draw_dot( 20, 20 )

        qp.end()

    def draw_all_axis( self , qp , length ):

        self.draw_x_axis( qp , length )
        self.draw_y_axis( qp , length )

    def draw_x_axis( self , qp , length ):

        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d49EBD8')
        qp.setPen(color)

        qp.setBrush(QtGui.QColor(73, 235, 216))
        qp.drawLine( 10 , length , length , length )

    def draw_y_axis( self , qp , length):

        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d49EBD8')
        qp.setPen(color)

        qp.setBrush(QtGui.QColor(73, 235, 216))
        qp.drawLine( 10, 10, 10, length )

    def draw_dot( self , x , y ):

        qp = QtGui.QPainter()
        qp.begin(self)

        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d4d4d4')
        qp.setPen(color)

        qp.setBrush(QtGui.QColor(200, 0, 0))
        qp.drawRect( x , y , x + 0.25, y + 0.25 )

    def drawPoints(self, qp):

        qp.setPen(QtCore.Qt.red)
        size = self.size()

        for i in range(1000):

            x = random.randint(1, size.width()-1)

            y = random.randint(1, size.height()-1)

            qp.drawPoint(x, y)     

    def drawRectangles(self, qp):

        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d4d4d4')
        qp.setPen(color)

        qp.setBrush(QtGui.QColor(200, 0, 0))
        qp.drawRect(10, 15, 90, 60)

        qp.setBrush(QtGui.QColor(255, 80, 0, 160))
        qp.drawRect(130, 15, 90, 60)

        qp.setBrush(QtGui.QColor(25, 0, 90, 200))
        qp.drawRect(250, 15, 90, 60)      

def external_dot_drawer( main_window ):


    for i in range(20, 100):

        main_window.draw_dot( i , i )


def main( ):

    print 'launching main window obj as app'
    sys.exit(app.exec_())
    print 'done!'

if __name__ == '__main__':

    print 'loading application abstraction'
    app = QtGui.QApplication(sys.argv)

    print 'building main window obj'
    gui = Example()

    print 'preparing to launch window as a separated process'
    p_main = Process( name='window' , target=main , args=( )  )

    print 'running new process'
    p_main.start()

    time.sleep(3)

    print 'trying to triggering paintEvent'
    p_external_dot_drawer = Process( name='extern_dot_drawer' , target=external_dot_drawer , args=( gui ) )

解决方案

The correct way to schedule a call to paintEvent is with QWidget.update().

I would also recommend using any of the existing python plotting libraries rather than writing your own. PyQtGraph has some nice multi-process features:

import pyqtgraph as pg
app = pg.QtGui.QApplication([])
import pyqtgraph.multiprocess as mp

## start child process
proc = mp.QtProcess()

## import pyqtgraph in the child process, create a plot window there
remotepg = proc._import('pyqtgraph')
win = remotepg.plot()

## create a scatter plot
plot = win.plot([1,4,2,3], [4,6,3,4], pen=None, symbol='o')

## ..after some processing, update the plot data
plot.setData(x=[1,2,3,4], y=[5,7,2,3])

这篇关于无油漆画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆