[PyQt] Pixel size issues with QPainter, QLabel and QSvg

matameko info at matameko.de
Fri Aug 10 12:34:32 BST 2012


(For Qt version 4.8.2 on win7:)
+ The QSvgWidget does not seem to reproduce the "letter-spacing" of a svg
text at all.
+ The QWebView reacts on the letter-spacing. However,
+ the letter-spacing for the svg text in a QWebView seems to be about twice
as large as for a text on a QLabel.
+ It is possible to access the svg dom command getBBox() through javascript
with the QWebView ( reqires Qt version 4.8.2)
(+ Qt version 4.7.3 gives other results for the letter spacing that version
4.8.2. Both letter spacings are different to the spacing in the QLabel.)

Any comments on this? Are the size issues due to pyqt or due to Qt? Where
should I report the bugs to get some improvements?

Here is some code to show the differences:
=======================================================================================

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

import sys

from PyQt4.QtCore import QByteArray
from PyQt4.QtCore import QT_VERSION_STR
from PyQt4 import QtGui
from PyQt4 import QtSvg
from PyQt4 import QtCore, QtGui, QtXml
from PyQt4.QtWebKit import *
from PyQt4.QtSvg import QSvgGenerator

       

class BasicWidget(QtGui.QWidget):
    
    def __init__(self):
        super(BasicWidget, self).__init__()        
        self.initUI()   
        
        
    def initUI(self): 
        
                
        #print qt version
        print "Qt version:" + QT_VERSION_STR
        
        #get screen
resolution--------------------------------------------------
        desc = QtGui.QDesktopWidget()
        dpi = desc.physicalDpiX()
        print "dpi: " + str(dpi)
   
        #set window properties        
        self.setGeometry(300, 300, 400, 400)
        self.setWindowTitle('Ikwed')
              
        
        #Set BackgroundColor
        colorstr = 'white'
        widget = self
        pal = QtGui.QPalette(widget.palette())        
        pal.setColor(QtGui.QPalette.Background, QtGui.QColor(colorstr))
        widget.setPalette(pal) 
        
         #set svg text
properties------------------------------------------------
        self.TextString = "TeststringTeststring"
        FontSize = 30
        FontStyle = "normal"
        FontWeight = "normal"
        LetterSpacing = 1
        WordSpacing = 0
        FillColor = "#000000"
        FillOpacity = 0.5
        Stroke = "none"
        FontFamily = "Times"
        SvgScale=1.0
        
       
        #translate FontWeight from svg to
python--------------------------------      
        MyWeight = {
            "lighter": 25.0,
             "normal": 50.0,
               "bold": 75.0,
             "bolder": 87.0
            }[FontWeight]
            
        #translate FontStyle from svg to
python---------------------------------
        MyItalic = {
             "normal": False,
             "italic": True,
            }[FontStyle]  
            
        #translate LetterSpacing from svg to python
        MyLetterSpacing = LetterSpacing *2
          
            
        #create python
font-----------------------------------------------------            
        self.MyFont = QtGui.QFont(FontFamily)  
        self.MyFont.setPixelSize(FontSize)
        #self.MyFont.setPointSizeF(FontSize*72.0/dpi)
        
        self.MyFont.setWeight(MyWeight)
        self.MyFont.setItalic(MyItalic)
        self.MyFont.setLetterSpacing(1, MyLetterSpacing)
        self.MyFont.setWordSpacing(WordSpacing) 
        #self.MyFont.setStyleStrategy(QtGui.QFont.NoAntialias)
        
      
        #create text with QLabel
===============================================
        self.label = QtGui.QLabel(self)
        self.label.setTextFormat(0)
        self.label.setFont(self.MyFont)
        
        
        #self.label.setFixedSize(800,50)
        self.label.setText(self.TextString)
        self.label.move(10,200)
        
        #print self.label.width()
        #print self.label.__sizeof__()
        #print self.label.sizeHint()
        #print self.label.minimumSizeHint()
        #print self.label.textFormat()
        #print self.label.contentsRect()
        #print self.label.heightForWidth(800)
        #print self.label.sizePolicy().horizontalStretch()
        #print self.label.layout()
        #print self.layout()
        #self.label.adjustSize()
        #self.adjustSize()
        #print self.rect()
        #print self.size()
        #self.label.resize(300,10)
        #print self.sizeIncrement().height()
        #print self.label.sizePolicy()
               
        font = self.label.font()
        fontInfo = self.label.fontInfo()
        #print "------------------------"
        #print fontInfo.family()
        #print fontInfo.exactMatch()
        #print fontInfo.fixedPitch() 
        #print fontInfo.italic()
        #print fontInfo.bold()
        #print fontInfo.pixelSize() 
        #print fontInfo.pointSize() 
        #print fontInfo.pointSizeF()
        #print fontInfo.rawMode() 
        #print fontInfo.style() 
        #print fontInfo.styleHint() 
        #print fontInfo.weight()
                   
        html_string = "<html>"                                                                                                        
+ "\n" +\
                      "  <head>"                                                                                                      
+ "\n" +\
                          "      "                                                                                           
+ "\n" +\
                          "  </head>"                                                                                                 
+ "\n" +\
                          "  <body style=\"margin: 0px; overflow:hidden;\">"                                                                                                 
+ "\n" +\
                          "      <svg id=\"mySVG\"
xmlns=\"http://www.w3.org/2000/svg\"
xmlns:xlink=\"http://www.w3.org/1999/xlink\">" + "\n" +\
                          "        <text"                                                                                             
+ "\n" +\
                          "                 id =
\"myText\""                                                                          
+ "\n" +\
                          "                  x = \"0\""                                                                               
+ "\n" +\
                          "                  y = \""+
str(FontSize) + "px\""                                                          
+ "\n" +\
                          "          transform = \"scale(" +
str(SvgScale) + "," + str(SvgScale) + ")\""                              
+ "\n" +\
                          "        >"                                                                                                 
+ "\n" +\
                          "        <tspan "                                              
+\
                                     "style=\""                                          
+\
                                     " font-size:" + str(FontSize)
+ "px;"                +\
                                     " font-family:" + FontFamily
+ ";"                   +\
                                     " font-style:" + FontStyle +
";"                     +\
                                     " font-weight:" + FontWeight
+ ";"                   +\
                                     " fill:" + FillColor +
";"                                +\
                                     " fill-opacity:" +
str(FillOpacity)+ ";"             +\
                                     " letter-spacing:" +
str(LetterSpacing) +"px;"       +\
                                     " word-spacing:" +
str(WordSpacing) + "px;"          +\
                                     " stroke:" + Stroke +
""                             +\
                                     " \""                                               
+\
                          "        >" + self.TextString + "</tspan>"                                                                       
+ "\n" +\
                          "  </text>"                                                                                                 
+ "\n" +\
                          "      </svg>"                                                                                              
+ "\n" +\
                          "  </body>"                                                                                                 
+ "\n" +\
                          "</html>"
                          
        webView = QWebView(self)
        webView.move(10,280)
        #desc = QtGui.QDesktopWidget()
        #dpi = desc.physicalDpiX()
        #webView.setTextSizeMultiplier(dpi/72.0)
        webView.settings().setAttribute(QWebSettings.JavascriptEnabled, 1)                                                                                                       
        webView.setHtml(html_string)
        webView.adjustSize()
        frame = webView.page().currentFrame()
        resultArray = frame.evaluateJavaScript("getSvgTextSize();").toList()
        width = resultArray[0].toDouble()[0]
        print "width: " + str(width)
        #print webView.zoomFactor()
        #webView.setStyleSheet("border: none;")
        webView.setStyleSheet("background-color: rgb(255,0,0); margin:0px;
border:0px;")
        #webView.setRenderHint(QtGui.QPainter.Antialiasing,1)
        #webView.setRenderHint(QtGui.QPainter.HighQualityAntialiasing,1)
        #webView.setRenderHint(QtGui.QPainter.NonCosmeticDefaultPen,1)
        #webView.setRenderHint(QtGui.QPainter.SmoothPixmapTransform,1)
        #webView.setRenderHint(QtGui.QPainter.TextAntialiasing,1)
        #webView.setRenderHint(QtGui.QPainter.Antialiasing,0)
        
        
        
        #QtGui.QPainter.Antialiasing		     Indicates that the engine should
antialias edges of primitives if possible.
        #QtGui.QPainter.TextAntialiasing		Indicates that the engine should
antialias text if possible. To forcibly disable antialiasing for text, do
not use this hint. Instead, set QFont::NoAntialias on your font's style
strategy.
        #QtGui.QPainter.SmoothPixmapTransform	Indicates that the engine
should use a smooth pixmap transformation algorithm (such as bilinear)
rather than nearest neighbor.
        #QtGui.QPainter.HighQualityAntialiasing	An OpenGL-specific rendering
hint indicating that the engine should use fragment programs and offscreen
rendering for antialiasing.
        #QtGui.QPainter.NonCosmeticDefaultPen	The engine should interpret
pens with a width of 0 (which otherwise enables QPen::isCosmetic()) as being
a non-cosmetic pen with a width of 1.
                                         
                        
        self.show()
        
        
              

        
def main():    
    app = QtGui.QApplication(sys.argv)
    #app.setOverrideCursor()

    dummyVariableForWaiting=BasicWidget()
    
    
    
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()



--
View this message in context: http://python.6.n6.nabble.com/Pixel-size-issues-with-QPainter-QLabel-and-QSvg-tp4984221p4984597.html
Sent from the PyQt mailing list archive at Nabble.com.


More information about the PyQt mailing list