Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing QRubberBand look while using cleanlooks style

What I try is to customize the look and feel of my custom QRubberBand a bit. I want to achieve a blue selection edge with a transparent blue selection rectangle background. Problem is that the background is always transparent when I use CleanLooks style but when I switch to for example WindowsVista style everything works fine.

I'm using PyQt 10.4.3 on a Windows machine.

Here is a small code sample from which you can see this strange behavior.

from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import Qt


class Selector(QtGui.QRubberBand):
    """
    Custom QRubberBand
    """

    def __init__(self,*arg,**kwargs):

        super(Selector,self).__init__(*arg,**kwargs)


    def paintEvent(self, QPaintEvent):


        painter = QtGui.QPainter(self)

        # set pen
        painter.setPen(QtGui.QPen(Qt.blue,4))

        # set brush
        color = QtGui.QColor(Qt.blue)
        painter.setBrush(QtGui.QBrush(color))

        # set opacity
        painter.setOpacity(0.3)

        # draw rectangle
        painter.drawRect(QPaintEvent.rect())



class Panel(QtGui.QWidget):

    def __init__(self,parent = None):

        super(Panel,self).__init__(parent)

        self.rubberBand = Selector(QtGui.QRubberBand.Rectangle,self)

    def mousePressEvent(self, QMouseEvent):

        self.clickPosition = QMouseEvent.pos()
        self.rubberBand.setGeometry(QtCore.QRect(self.clickPosition,QtCore.QSize()))
        self.rubberBand.show()


    def mouseMoveEvent(self, QMouseEvent):

        pos = QMouseEvent.pos()
        self.rubberBand.setGeometry(QtCore.QRect(self.clickPosition,pos).normalized())


    def mouseReleaseEvent(self, QMouseEvent):

        self.rubberBand.hide()



if __name__ == "__main__":

    import sys

    app = QtGui.QApplication([])
    QtGui.QApplication.setStyle("cleanlooks")

    pn = Panel()
    pn.show()

    sys.exit(app.exec_())
like image 843
pegaz Avatar asked Nov 11 '22 02:11

pegaz


1 Answers

I've seen a similar behavior for other GUI elements in Qt (e.g. in Is it possible to change the colour of a QTableWidget row label?) and my impression was that the style in Qt can be tricky in that it can override user specific customizations.

In principle you could implement your own QStyle and put all customizations there.

The easier way here is probably to set the style of the element (here QRubberBand) to a style that does not interfere.

This should work:

class Selector(QtGui.QRubberBand):
    def __init__(self,*arg,**kwargs):
        super().__init__(*arg,**kwargs)
        self.setStyle(QtGui.QStyleFactory.create('windowsvista'))
like image 133
Trilarion Avatar answered Nov 14 '22 22:11

Trilarion