Peque
Peque

Reputation: 14831

Python Qt bindings: How to increase the width of lines with width 0

With the following simple example (which works well with either PySide or PyQt4 in my computer):

import sys
import random
import numpy
from PySide import QtGui, QtCore

class Window(QtGui.QWidget):

    def __init__(self):
        super(Window, self).__init__()

        self.resize(800, 500)
        self.view = QtGui.QGraphicsView()
        self.scene = QtGui.QGraphicsScene()
        self.view.setScene(self.scene)
        self.setWindowTitle('Example')

        # Layout
        layout = QtGui.QGridLayout()
        layout.addWidget(self.view, 0, 0)
        self.setLayout(layout)

        # Styles
        self.pen = QtGui.QPen(QtCore.Qt.black, 0, QtCore.Qt.SolidLine)
        self.brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 255))

    def addLine(self, x0, y0, x1, y1):
        line = QtCore.QLineF(x0, -y0, x1, -y1)
        pen = QtGui.QPen(QtGui.QColor(250, 0, 0, 255), 0, QtCore.Qt.SolidLine)
        pen.setStyle(QtCore.Qt.CustomDashLine)
        pen.setDashPattern([1, 4, 5, 4])
        l = self.scene.addLine(line, pen)

    def addRect(self, left, top, width, height):
        rect = QtCore.QRectF(left, -top, width, abs(height))
        r = self.scene.addRect(rect, self.pen, self.brush)

    def fit(self):
        self.view.fitInView(self.scene.sceneRect())

    def resizeEvent(self, event = None):
        self.fit()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    window.addRect(0, 1, 1, 1)
    window.addLine(-1, -1, 2, 2)
    window.addLine(0, 1, 1, 0)
    window.fit()
    sys.exit(app.exec_())

I am able to paint a two red lines that have constant width; which means that they do not change no matters the size (coordinates) of the square and lines and no matters if I re-size the window:

enter image description here enter image description here

This is because I am using a QtGui.Qpen with width 0. However, if I use another width > 0, then the observed line width will change on window re-size (or will change if the square and lines have other dimensions too):

enter image description here enter image description here

Is it possible to change (increase) the line width so that the observed lines are thicker than those obtained when the width is set to 0, while maintaining the same "observed" width on window resize or when the dimensions of the square/lines vary?

EDIT

Using setCosmetic(True), as suggested by o11c, has an unexpected behavior (at least I would not expect that to happen); it adds margins to the image (increases the size of scene.sceneRect()). These margins seem to be proportional to the width of the QPen when isCosmetic() == True:

enter image description here

A new question has been created related to this issue. See question 26231374 for more details:

Upvotes: 3

Views: 2918

Answers (1)

o11c
o11c

Reputation: 16116

According to the Qt C++ documentation, use the setCosmetic function on QPen. I assume the python wrapper exposes this.

Upvotes: 1

Related Questions