PyQt4 Image as Background

时间:2015-08-07 02:30:37

标签: python pyqt4

I am using the code below from designer and I am trying to make the Qwidget have a image as background and then have the list widget and the text edit widget be transparent.

The main issue I am having is when I make the image as background using the stylesheet for 'Form' it carries over as the background for the list widget and text edit widget as well. How do I make those retain there own stylesheets.

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.setEnabled(True)
        Form.resize(854, 667)
        Form.setAutoFillBackground(False)
        Form.setStyleSheet("background-image: url(test.jpg);")
        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(20, 400, 821, 251))
        self.textEdit.setStyleSheet(_fromUtf8("background-color: rgba(255, 255, 255, 102);"))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.listWidget = QtGui.QListWidget(Form)
        self.listWidget.setGeometry(QtCore.QRect(360, 10, 471, 371))
        self.listWidget.setAutoFillBackground(True)
        self.listWidget.setStyleSheet(_fromUtf8("background-color: rgba(255, 255, 255, 102);"))
        self.listWidget.setObjectName(_fromUtf8("listWidget"))
        item = QtGui.QListWidgetItem()
        item.setCheckState(QtCore.Qt.Checked)
        self.listWidget.addItem(item)
        item = QtGui.QListWidgetItem()
        item.setCheckState(QtCore.Qt.Checked)
        self.listWidget.addItem(item)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        __sortingEnabled = self.listWidget.isSortingEnabled()
        self.listWidget.setSortingEnabled(False)
        item = self.listWidget.item(0)
        item.setText(_translate("Form", "blah blh sdfsaasdfdsfasf", None))
        item = self.listWidget.item(1)
        item.setText(_translate("Form", "test", None))
        self.listWidget.setSortingEnabled(__sortingEnabled)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

Thanks

3 个答案:

答案 0 :(得分:2)

这对我有用:

    self.text_field = QtGui.QPlainTextEdit(self)
    self.text_field.setMinimumSize (480,150)
    self.text_field.setStyleSheet("background-image: url(FILENAME); background-attachment: fixed")

答案 1 :(得分:1)

Like this:

QWidget#Form {background-image: url(test.jpg);}

Hope it helps!

答案 2 :(得分:0)

我和@rainer一样,但是使用中央小部件:-

self.centralwidget.setStyleSheet("background-image: url(test.jpg); background-attachment: fixed")
相关问题