1

Using PyQt, I'm trying to use a stylesheet to set the background color of a QLineEdit that I've added to a QGraphicsScene. Problem is the background color is the only property I can't seem to change. For example:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGraphicsView, QGraphicsScene, QLineEdit

if __name__ == '__main__':
  app = QApplication(sys.argv)
  myWidget = QWidget()
  myWidget.graphicsView = QGraphicsView(myWidget)
  scene = QGraphicsScene()

  lineEdit = QLineEdit()
  lineEdit.setStyleSheet("QLineEdit { background-color : black; color : gray; }")
  widgetItem = scene.addWidget(lineEdit)

  myWidget.graphicsView.setScene(scene)
  myWidget.show()
  sys.exit(app.exec_())    

will set the text color of the line edit to gray, but the background color remains white.

What am I doing wrong?

  • I just made a QLineEdit, and used `QGraphicsScene.addWidget(QLineEdit)` and it worked fine. I recommend posting an example that reproduces it. In general, posting a simple example that reproduces the problem (a SSCCE) will let you get help much faster here (see sscce.org). In general it is much easier to fix a fully working piece of code that displays the problem. – eric Apr 18 '16 at 13:51
  • @neuronet I just updated my question with a complete example that reproduces the problem – Nathan Barnabas Apr 20 '16 at 01:05
  • Strangely, in pyqt4 it works fine. I don't have pyqt5.... – eric Apr 20 '16 at 02:55

0 Answers0