Adding to a stylesheet in the cpp file without overriding previous functions
-
wrote on 22 Sept 2022, 09:44 last edited by
Hi. I'm writing some cpp code that changes the color of the text. I have some preexisting stylesheet code (about 200 lines) in my ui file. The code in the ui file for QWidget, QLabel, QGroupBox and QPushButton are as follows:
QWidget { font: 25px "DejaVu Sans"; background-color: #e0e0e0; } QLineEdit { background-color: white; border: 1px solid #8f8f91; border-style: solid; border-radius: 4px; font: 30px; padding-left: 10px; } QPushButton { border: 2px solid #8f8f91; border-radius: 9px; background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde); font: 25px; outline: none; } QLabel { font: 25px; background: transparent; } QGroupBox { border: 2px solid #8f8f91; border-radius: 9px; }
This is the code in the cpp file I used to change the color of the text.
setStyleSheet( "QWidget { color: red }" "QLabel { color: red }" "QGroupBox { color: red }" "QPushButton { color: red }" );
What happens right now is that the cpp file seems to completely override the ui file. I was wondering whether there's a function in stylesheets that allow for "adding" commands to the stylesheet without completely overriding preexisting commands.
-
Hi. I'm writing some cpp code that changes the color of the text. I have some preexisting stylesheet code (about 200 lines) in my ui file. The code in the ui file for QWidget, QLabel, QGroupBox and QPushButton are as follows:
QWidget { font: 25px "DejaVu Sans"; background-color: #e0e0e0; } QLineEdit { background-color: white; border: 1px solid #8f8f91; border-style: solid; border-radius: 4px; font: 30px; padding-left: 10px; } QPushButton { border: 2px solid #8f8f91; border-radius: 9px; background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde); font: 25px; outline: none; } QLabel { font: 25px; background: transparent; } QGroupBox { border: 2px solid #8f8f91; border-radius: 9px; }
This is the code in the cpp file I used to change the color of the text.
setStyleSheet( "QWidget { color: red }" "QLabel { color: red }" "QGroupBox { color: red }" "QPushButton { color: red }" );
What happens right now is that the cpp file seems to completely override the ui file. I was wondering whether there's a function in stylesheets that allow for "adding" commands to the stylesheet without completely overriding preexisting commands.
wrote on 22 Sept 2022, 09:53 last edited by JonB@Dummie1138 said in Adding to a stylesheet in the cpp file without overriding previous functions:
What happens right now is that the cpp file seems to completely override the ui file.
Absolutely. You use
setStyleSheet()
, so it completely replaces whatever was there.I was wondering whether there's a function in stylesheets that allow for "adding" commands to the stylesheet without completely overriding preexisting commands.
No there is not, there is no "merging" available. You would have to fetch existing
widget->styleSheet()
, parse it and do whatever you want yourself. I'm afraid there is no help with this from Qt, they are just strings you have to deal with yourself. If all you want to do is just add new stuff you can add it at end of existing, likewidget->setStyleSheet(widget->styleSheet() + ';' + newStuff);
but needs adjusting for the existing terminating
}
which will already be there. -
@Dummie1138 said in Adding to a stylesheet in the cpp file without overriding previous functions:
What happens right now is that the cpp file seems to completely override the ui file.
Absolutely. You use
setStyleSheet()
, so it completely replaces whatever was there.I was wondering whether there's a function in stylesheets that allow for "adding" commands to the stylesheet without completely overriding preexisting commands.
No there is not, there is no "merging" available. You would have to fetch existing
widget->styleSheet()
, parse it and do whatever you want yourself. I'm afraid there is no help with this from Qt, they are just strings you have to deal with yourself. If all you want to do is just add new stuff you can add it at end of existing, likewidget->setStyleSheet(widget->styleSheet() + ';' + newStuff);
but needs adjusting for the existing terminating
}
which will already be there.wrote on 22 Sept 2022, 10:32 last edited by@JonB Does setStyleSheet override everything? Say I am only setting QLabel in a new setStyleSheet, would it completely remove the stylesheet arguments on everything else?
-
@JonB Does setStyleSheet override everything? Say I am only setting QLabel in a new setStyleSheet, would it completely remove the stylesheet arguments on everything else?
wrote on 22 Sept 2022, 13:08 last edited by@Dummie1138 said in Adding to a stylesheet in the cpp file without overriding previous functions:
Say I am only setting QLabel in a new setStyleSheet, would it completely remove the stylesheet arguments on everything else?
Absolutely not. Calling
setStyleSheet()
on one widget does not in itself alter any other style sheets you might have assigned to other widgets. Of course, the effect of the change may affect other widget descendants in the hierarchy, but that is a quite different matter.
1/4