[PyQt] Custom Control again

Fernando Cosso ferdemza at gmail.com
Mon Jan 31 18:18:24 GMT 2011


Hi all

I am having new issues with that Rich Text Push Button and I tried
everything but so far I have not succeed.

I wan my push button to have the icon and the text aligned left. I know how
to change the text position since is in the reimplementation of the
paintEvent. I just change that little bit.
I should also change the sizeHint but I will come to that latter.

The thing is that I don't know how to align the icon. I know I should change
something related to this line:
p.drawControl(QtGui.QStyle.CE_PushButton, self.getStyleOption())

but I don't know what. I've read the Qt documentation but I am more confused
that before.

Any help, hint, whatever would be much appreciated!

Best Regards

    def paintEvent(self, event):

        p = QtGui.QStylePainter(self)
        buttonRect = QtCore.QRect(self.rect())

        richTextLabel = QtGui.QTextDocument()
        richTextLabel.setHtml(self._htmlText)

        richTextPixmap = QtGui.QPixmap(richTextLabel.size().width(),
                                 richTextLabel.size().height())
        richTextPixmap.fill(QtCore.Qt.transparent)
        richTextPainter = QtGui.QPainter(richTextPixmap)
        richTextLabel.drawContents(richTextPainter,
                                   QtCore.QRectF(richTextPixmap.rect()))

        richTextPainter.end()

        if not self.icon().isNull():
            point = QtCore.QPoint(buttonRect.x() +
                           self.iconSize().width() + RPushButton.SIZEADJ,
                           buttonRect.y() + buttonRect.height() / 2);
        else:
            point = QtCore.QPoint(buttonRect.x(),
                           buttonRect.y() + buttonRect.height() / 2);

        buttonRect.translate(point.x() +
                             RPushButton.SIZEADJ,
                             point.y() - richTextPixmap.height() / 2);

        p.drawControl(QtGui.QStyle.CE_PushButton, self.getStyleOption())
        p.drawPixmap(buttonRect.left(), buttonRect.top(),
                     richTextPixmap.width(), richTextPixmap.height(),
                     richTextPixmap);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110131/fe0d8f66/attachment.html>


More information about the PyQt mailing list