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 914 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on 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.

    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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.

      Dummie1138D 1 Reply Last reply
      1
      • JonBJ 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, like

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

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

        Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on 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?

        JonBJ 1 Reply Last reply
        0
        • Dummie1138D Dummie1138

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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

          • Login

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