QPicture and Qpainter PyQt4

L

luca72

Hello at all
I have made this small tests:

this is the form implementation:

# -*- coding: utf-8 -*-

"""
Module implementing Form.
"""

from PyQt4.QtGui import QWidget
from PyQt4.QtCore import pyqtSignature
from PyQt4 import *
from disegni import PictureFrame

from Ui_form import Ui_Form

class Form(QWidget, Ui_Form):
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
QWidget.__init__(self, parent)
self.setupUi(self)

@pyqtSignature("")
def on_pushButton_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise "Not implemented yet"
#gr = QtGui.QPainter()
picture = QtGui.QPicture()
gr = QtGui.QPainter()
gr.begin(picture)
gr.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine))
gr.drawLine(70,100,150,100)
gr.end()
picture.save('dis.pic')
self.frame.update()

and this is the clas that implements the paint event:

from PyQt4.QtGui import QWidget
from PyQt4.QtCore import pyqtSignature
from PyQt4 import *

class PictureFrame(QtGui.QFrame):

def __init__(self, parent = None):
QFrame.__init__(self, parent)
picture = QtGui.QPicture()

def paintEvent(self, event):
picture.load('dis.pic')
gr = QtGui.QPainter()
gr.begin(self.frame)
gr.drawPicture(0, 0, picture)
gr.end()

But nothing is paint.
Can you give me some help

Regards

Luca
 
B

BlueBird

Hi,

It looks like you have several things wrong:

class Form(QWidget, Ui_Form):
[...]

@pyqtSignature("")
def on_pushButton_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise "Not implemented yet"
#gr = QtGui.QPainter()
picture = QtGui.QPicture()
gr = QtGui.QPainter()
gr.begin(picture)
gr.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine))
gr.drawLine(70,100,150,100)
gr.end()
picture.save('dis.pic')
self.frame.update()

Normally, you don't paint inside a slot. You paint inside the
paintEvent() method which is called automatically by Qt when an area
of your widget needs to be repainted.
from PyQt4.QtGui import QWidget
from PyQt4.QtCore import pyqtSignature
from PyQt4 import *

class PictureFrame(QtGui.QFrame):

def __init__(self, parent = None):
QFrame.__init__(self, parent)
picture = QtGui.QPicture()

def paintEvent(self, event):
picture.load('dis.pic')
gr = QtGui.QPainter()
gr.begin(self.frame)
gr.drawPicture(0, 0, picture)
gr.end()

Don't load a picture inside a paintevent. Loading is an expensive
process, you don't want to repeat it all the time.

Second, you should check that your picture was loaded. Sometimes, it's
a file problem. Check the size of your picture for example.

Third, I think you should have :
gr.begin( self )

Else, the painter is not painting on your widget.

Hope that helps.

PHilippe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top