[PyQt] Custom Control again
Fernando Cosso
ferdemza at gmail.com
Tue Feb 1 03:43:04 GMT 2011
Well in the end I was able to solve that
just in case anyone needs it:
def sizeHint(self):
richTextLabel = QtGui.QTextDocument()
richTextLabel.setHtml(self._htmlText)
sizeHint = QtCore.QSize(richTextLabel.size().width()
+ 2*RPushButton.SIZEADJ,
richTextLabel.size().height()
+ 2*RPushButton.SIZEADJ)
if not self.icon().isNull():
sizeHint = QtCore.QSize(sizeHint.width()
+ self.iconSize().width()
+ 3*RPushButton.SIZEADJ,
max(sizeHint.height()
+ 2*RPushButton.SIZEADJ,
self.iconSize().height()
+ 2*RPushButton.SIZEADJ))
return sizeHint
## Reimplementation of paintEvent
#
# See http://doc.trolltech.com/latest/qwidget.html#paintEvent
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_PushButtonBevel,
self.getStyleOption())
p.drawPixmap(buttonRect.left(), buttonRect.top(),
richTextPixmap.width(), richTextPixmap.height(),
richTextPixmap)
if not self.icon().isNull():
p.drawPixmap(RPushButton.SIZEADJ, RPushButton.SIZEADJ,
self.iconSize().width(), self.iconSize().height(),
self.icon().pixmap(self.iconSize()))
On 31 January 2011 13:18, Fernando Cosso <ferdemza at gmail.com> wrote:
> 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/52e50112/attachment.html>
More information about the PyQt
mailing list