[PyQt] Absolute size on screen
    David Boddie 
    david at boddie.org.uk
       
    Sat May  7 18:06:15 BST 2011
    
    
  
On Fri, 6 May 2011 11:38:49 +0000, Pietro Moras wrote:
>  In need to ?break the ice? with PyQt4, so I dare to ask you where I could
> look for such basic a task as to express a length in absolute pixel size on
> screen, so to draw a square not subject to re-sizing and zooming. Thanks!
Take a look at the logicalDpiX() and logicalDpiY() methods of QWidget to get
a pair of coefficients. You can use these to calculate the number of pixels
needed to draw a square with dimensions defined in real-world units (like
inches or millimetres).
import sys
from PyQt4.QtGui import *
class Widget(QWidget):
    def paintEvent(self, event):
        p = QPainter()
        p.begin(self)
        # Draw a 2cm x 2cm square.
        width = self.logicalDpiX() * 2 / 2.54
        height = self.logicalDpiY() * 2 / 2.54
        p.drawRect(10, 10, width, height)
        p.end()
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
David
    
    
More information about the PyQt
mailing list