2

I have a QGraphicsScene and add a QlineEdit but changing color just doesn't work.

QGridLayout *layout = new QGridLayout(this);
QGraphicsView *view = new QGraphicsView(this);
QGraphicsScene *scene = new QGraphicsScene(this);

QWidget *widget = new QWidget();
QGridLayout *widgetLayout = new QGridLayout(this);
QLineEdit *le1 = new QLineEdit(widget);
QLineEdit *le2 = new QLineEdit(widget);
widgetLayout->addWidget(le1,1,0);
widgetLayout->addWidget(le2,2,0);
widget->setLayout(widgetLayout);



QPalette paletteRed = le1->palette();
paletteRed.setColor(QPalette::Background,Qt::red);

QPalette paletteGreen = le1->palette();
paletteGreen.setColor(QPalette::Background,Qt::green);


le1->setAutoFillBackground(true);
le1->setPalette(paletteRed); // not working
widget->setPalette(paletteGreen); // working


view->setScene(scene);
scene->addWidget(widget);

ui->centralWidget->setLayout(layout);
layout->addWidget(view);

Do I have to trigger something like update() ( which is not working either to get another color ) if the widget is in a scene?

EDIT:

created new example Code.

I know this works in a normal QWidget. Actually the code works fine if i place the QLineEdit in a normal QFrameetc but its in a QGraphicsScene. And in this special case its not working. Text and highlight color etc is also working fine. But backgroud/base/etc ist not.

J.W.
  • 81
  • 10

3 Answers3

1

Setting background color via QPalette does not for my widget, why?

Usually it is autoFillBackground property not set to true to allow setting background on its own.

QPalette palette = pWidget->palette(); // fixed it (need to initialize)
palette.setColor(pWidget->backgroundRole(), bkgndColor); // for background (fixed)
palette.setColor(pWidget->foregroundRole(), fgrndColor); // for foreground
pWidget->setAutoFillBackground(true); // to allow to fill the background
pWidget->setPalette(palette);

Setting the background via stylesheet may also work because it forces autoFillBackground == true mode.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • `QPalette::Background` should be replaced by `pWidget->backgroundRole()`. Info here: http://doc.qt.io/qt-5/qwidget.html#backgroundRole – jonspaceharper May 26 '16 at 19:39
  • @JonHarper Right, that is better to get actual background role. Making fixes in my own code then. SO rules! – Alexander V May 26 '16 at 20:01
  • @AlexanderVX this istn working. It is working in normal cases like for `QFrame` and also for `QLineInput` if used in a normal widget. But if i put in a `QGraphicsScene` it is not working – J.W. May 26 '16 at 20:10
  • @J.W. There is one suspicious thing in your code, though: QPalette pal(QApplication::palette()); Can you try with QLineEdit own palette? – Alexander V May 26 '16 at 20:39
  • @J.W. Try capturing the pointer to the `QGraphicsProxyWidget` that is returned from `addWidget` and set the palette on that. – jonspaceharper May 27 '16 at 03:01
  • @AlexanderVX Its not making any difference. I tryed but its not working either. I posted a new example code. – J.W. May 27 '16 at 06:29
  • @JonHarper Thats not working either, neverless if it had worked i still need to change the background color for a special `QLineEdit` and not all – J.W. May 27 '16 at 06:30
  • @J.W. I cannot see le1->setAutoFillBackground(true) there. – Alexander V May 27 '16 at 07:07
  • @AlexanderVW cause its not working and doesnt make any difference – J.W. May 27 '16 at 09:22
1

I would like to suggest a slight modification on AlexanderVX's answer. In the first line I would write:

QPalette palette = pWidget->palette();

Just to make sure you tune just what you need of the base object palette.

Regards.

0

I had an similar issue where the background-color just colored the outline:

enter image description here

the problem was that there was an border-image set. After setting border-image: none; the background color showed up

wutzebaer
  • 14,365
  • 19
  • 99
  • 170