Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding to a stylesheet in the cpp file without overriding previous functions
Forum Updated to NodeBB v4.3 + New Features

Adding to a stylesheet in the cpp file without overriding previous functions

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 911 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dummie1138
    wrote on 22 Sept 2022, 09:44 last edited by
    #1

    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.

    J 1 Reply Last reply 22 Sept 2022, 09:53
    0
    • D Dummie1138
      22 Sept 2022, 09:44

      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.

      J Offline
      J Offline
      JonB
      wrote on 22 Sept 2022, 09:53 last edited by JonB
      #2

      @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, like

      widget->setStyleSheet(widget->styleSheet() + ';' + newStuff);
      

      but needs adjusting for the existing terminating } which will already be there.

      D 1 Reply Last reply 22 Sept 2022, 10:32
      1
      • J JonB
        22 Sept 2022, 09:53

        @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, like

        widget->setStyleSheet(widget->styleSheet() + ';' + newStuff);
        

        but needs adjusting for the existing terminating } which will already be there.

        D Offline
        D Offline
        Dummie1138
        wrote on 22 Sept 2022, 10:32 last edited by
        #3

        @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?

        J 1 Reply Last reply 22 Sept 2022, 13:08
        0
        • D Dummie1138
          22 Sept 2022, 10:32

          @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?

          J Offline
          J Offline
          JonB
          wrote on 22 Sept 2022, 13:08 last edited by
          #4

          @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 Reply Last reply
          0

          3/4

          22 Sept 2022, 10:32

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved