1

In qt you normally set the color of a QWidget with the QPalette.

Example:

QPalette palette = new QPalette();
palette.setBrush(QPalette::Base, this->palette().backgorund());

QLineEdit *line = new QLineEdit();
line->setPalette(palette);

Now I have a little problem. It is not possible to change the bordercolor of a QLineEdit with the QPalette. That means, that I have to use a QStyleSheet.

Example:

QLineEdit *line = new QLineEdit();
line.setStyleSheet("border: 1px solid green");

But now I can't set the basecolor of the QLineEdit with QPalette, because the background-color of QLineEdit is not longer connected to QPalette::base. That means, that the following code wouldn't change the background-color of the QLineEdit:

QPalette palette = new QPalette();
palette.setBrush(QPalette::Base, this->palette().backgorund());

QLineEdit *line = new QLineEdit();
line->setPalette(palette);
line->setStyleSheet("border: 1px solid green");

But it is not possible, to define the background-color of the QLineEdit in the StyleSheet, because the background-color of the QLineEdit have to be dynamically.

My question: How to connect the background-color of the QLineEdit with QPalette::base to define the background-color of QLineEdit dynamically with QPalette?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
NelDav
  • 785
  • 2
  • 11
  • 30
  • Why can't you simply create/format a `QString` containing the required background and border values? – G.M. Aug 17 '18 at 10:43
  • I don't know, what you mean. Do you mean, I should create a class inherit from QString with a field for background and a field for the border? - There is no background and border property in QString, is it? – NelDav Aug 17 '18 at 10:49

3 Answers3

14

Alternatively:

line->setStyleSheet(QStringLiteral(
    "border: 1px solid green;"
    "background-color: palette(base);"
));

Reference: http://doc.qt.io/qt-5/stylesheet-reference.html#paletterole

Using PaletteRole also lets the CSS be in a separate file/source.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22
  • 1
    This should be the correct answer. The currently accepted answer is more like "generate a style sheet with colors copied from a palette object". This is (according to me) quite far from how the question is stated (and what I want to do as well). This said, I can't seem to get this to work (all my colors come out black). Does anyone know of a working example somewhere? I couldn't find any... :( – pythonator May 16 '19 at 09:05
  • Slight correction, the colors don't come out black, but the palette colors I've set seem to be ignored and I get some default palette... – pythonator May 16 '19 at 09:28
  • @pythonator When using CSS on a widget, the widget's default or assigned palette may be ignored (esp. in the cases or backgrounds/borders/frames). In such cases the `PaletteRole` seems best used when referring to the default app `QPalette` (optionally set with QApplication::setPalette()`). The exact behavior, and especially how the palette colors propagate (or not) can depend on the Qt style being used (e.g. "fusion" or "windows", etc). "Fusion" is the most flexible and predictable in terms of QPalette inheritance, and also for mixing it with CSS. – Maxim Paperno May 17 '19 at 00:49
5

Just construct the required QString at runtime...

auto style_sheet = QString("border: 1px solid green;"
                           "background-color: #%1;")
  .arg(QPalette().color(QPalette::Base).rgba(), 0, 16);

The above should result in a QString such as...

border: 1px solid green;
background-color: #ffffffff;

Then...

line->setStyleSheet(style_sheet);
G.M.
  • 12,232
  • 2
  • 15
  • 18
0

I found a solution for my situation. Because I only want to mask the border, and don't want to color it, I can use the method QLineEdit::setFrame(bool). But what is, if I want to color the frame like in my example above? I didn't find a solution for that so far. I am happy about every answer.

NelDav
  • 785
  • 2
  • 11
  • 30