ThatComputerGuy
ThatComputerGuy

Reputation: 175

Background For QCombobox inheriting background image - PyQt5

I have a problem where my QComboBox is not using the background I defined, well it is, except only for the "selected-item box". Here is a screenshot:

What I want is for the background to be white for everything.

Here is the code:

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox
import sys


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.xpos, self.ypos = 200, 200
        self.width = 1920 / 2
        self.height = 1080 / 2
        self.setFixedSize(self.width, self.height)
        self.setWindowTitle("Test")
        self.move(200, 200)
        self.setStyleSheet("background-image: url(images/background.jpg)")
        self.initUI()

    def initUI(self):
        self.font13 = QtGui.QFont()
        self.font13.setPointSize(13)

        self.combobox = QComboBox(self)
        self.options = ['Option #1', 'Option #2', 'Option #3',
                        'Option #4', 'Option #5', 'Option #6']
        self.combobox.addItems(self.options)
        self.combobox.setFont(self.font13)
        self.combobox.setStyleSheet(
            'background: white')
        self.updateSize(self.combobox)

    def updateSize(self, object):
        object.adjustSize()


def window():
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())


window()

And here is the link to the background I used: https://pixabay.com/es/illustrations/azul-de-fondo-degradado-colores-1142743/

How can I change it so that all of the options have a white background and not just the selected one? Thanks!

Upvotes: 0

Views: 353

Answers (1)

ThatComputerGuy
ThatComputerGuy

Reputation: 175

I figured out the problem! The problem was that I was setting the style sheet of the entire main window to have the png as the background, instead of just the main window. The solution was to change this line:

self.setStyleSheet("background-image: url(images/background.jpg)")

to

self.setStyleSheet("QMainWindow {background-image: url(images/background.jpg)}")

Hopes this helps someone!

Upvotes: 1

Related Questions