开发者

Set background text

Does anyone know how I can set back开发者_StackOverflow中文版ground text for a QListWidget?

I've previously used

self.setStyleSheet("background-image:myBgImg.png")

but I want to create the text within the app. i.e:

bgImage = QImage()
bgImage = bgImage.setText( "textKey" , "This is some background text." )
palette = QPalette()
palette.setBrush(self.listWidget.backgroundRole(), QBrush( bgImage ))
self.listWidget.setPalette(palette)

though this doesn't seem to be working. Any ideas?


The first problem you have is that you are overrideing bgImage with None, if you look at the setText method you will see that it returns void, since you are passing None to the QBrush there is nothing to draw. Try with:

bgImage = QImage()
bgImage = bgImage.setText( "textKey" , "This is some background text." )
palette = QPalette()
palette.setBrush(self.listWidget.backgroundRole(), QBrush( bgImage ))
self.listWidget.setPalette(palette)

An other approach that does work is to extend the list and implement the paint event:

import sys 
from PyQt4 import QtCore, QtGui


class MyList(QtGui.QListWidget):
    """A funny list."""

    def paintEvent(self, event):
        """Paint the widget."""
        # paint the widget
        painter = QtGui.QPainter(self.viewport())
        # paint here
        super(MyList, self).paintEvent(event)

You have to make sure that you use the viewport to paint and not self since you will get QPainter::begin: Widget painting can only begin as a result of a paintEvent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜