Trying_hard
Trying_hard

Reputation: 9501

PyQt4 Image as Background

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

Upvotes: 1

Views: 18806

Answers (3)

Vicrobot
Vicrobot

Reputation: 3988

I did same as @rainer, but with the central widget:-

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

Upvotes: 1

rainer
rainer

Reputation: 3411

This worked for me:

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

Upvotes: 5

cdonts
cdonts

Reputation: 9599

Like this:

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

Hope it helps!

Upvotes: 2

Related Questions