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. Switch object style Sheet
QtWS25 Last Chance

Switch object style Sheet

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 5 Posters 5.5k Views
  • 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.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by
    #1

    Hi, i am trying to switch styles when i click the button.

    So i use this code:

    ui->Red_To_Black->QLineEdit::styleSheet() = ui->Black_To_Red->QLineEdit::styleSheet();
    

    But it doesn't work....
    Any help?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jorgen
      wrote on last edited by
      #2

      you try to assign and return value to an return value.

      try the setter method instead:

      ui->Red_To_Black->setStyleSheet(ui->Black_To_Red->styleSheet());
      
      1 Reply Last reply
      5
      • L Offline
        L Offline
        Loc888
        wrote on last edited by
        #3

        It doesn't help me... I need to pick it from the object number 2..

        mrjjM 1 Reply Last reply
        0
        • L Loc888

          It doesn't help me... I need to pick it from the object number 2..

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Loc888
          What is object number 2 ?
          @Jorgen shows how to set stylesheet from Black_To_Red to the
          Red_To_Black button.
          If that is not what you try, can you tell what it is you want to archive ?

          1 Reply Last reply
          2
          • L Offline
            L Offline
            Loc888
            wrote on last edited by
            #5

            I wanna create a loop. Something like:

            text_edit_black = text_edit_red

            text_edit_red = text_edit_green;

            text_edit_green = text_edit_blue; Etc.

            Don't ask me why anyone can need something like that. I need to do it in this way.

            mrjjM 1 Reply Last reply
            0
            • L Loc888

              I wanna create a loop. Something like:

              text_edit_black = text_edit_red

              text_edit_red = text_edit_green;

              text_edit_green = text_edit_blue; Etc.

              Don't ask me why anyone can need something like that. I need to do it in this way.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Loc888
              Hi
              But if you override one buttons stylesheet with other,
              the first style is lost so u can not reuse or switch back.

              What about having the stylesheet as variables ?

              auto st_red
                = R"(
                  background-color: rgb(255, 0, 0);
                  )";
              
              auto st_blue
                = R"(
                  background-color: rgb(0, 0, 255);
                  )";
              
              void MainWindow::on_SomeButton_released() {
              ui->SomeButton->setStyleSheet(st_red);
              }
              
              
              1 Reply Last reply
              1
              • L Offline
                L Offline
                Loc888
                wrote on last edited by
                #7

                We dont understand.. I know how to set the style.

                The problem is how to do a loop of styles...Again dont ask me why i need it..

                I need to find any way to switch the styles from mulitple objects. And the object type is line edit.
                That's it, just setStyle doesn't help.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  Loc888
                  wrote on last edited by
                  #8

                  I dont know if variables can help. I need 3 styles, and multiple objects.

                  I need to get the style from object b, set it to object a. Then;

                  Get the style from b, set to c;

                  c to d;

                  d to f; Etc. That's my problem.

                  mrjjM 1 Reply Last reply
                  0
                  • L Loc888

                    I dont know if variables can help. I need 3 styles, and multiple objects.

                    I need to get the style from object b, set it to object a. Then;

                    Get the style from b, set to c;

                    c to d;

                    d to f; Etc. That's my problem.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    @Loc888
                    But if you let the buttons keep the stylesheet then
                    in your example
                    Get the style from b, set to c;
                    c to d;
                    d to f;

                    then all get stylesheet from B
                    just like
                    QString bstylesheet=ui->b->stylehseet();
                    ui->c-setStylesheet(bstylesheet);
                    ui->d-setStylesheet(bstylesheet);
                    ui->f-setStylesheet(bstylesheet);

                    Do you need to sort of rotate the style sheets ?

                    QString Bst=ui->b->stylehseet();
                    QString Cst=ui->C->stylehseet();
                    QString Dst=ui->D->stylehseet();

                    ui->c-setStylesheet(Bst);
                    ui->d-setStylesheet(Cst);
                    ui->f-setStylesheet( Dst);

                    kinda ?

                    L 1 Reply Last reply
                    3
                    • J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      Hi,
                      setting stylesheets is actually a rather "heavy" operation so using setStylesheet in a loop with small intervalls is something you should not do.

                      I would suggest, Create your own LineEdit, dervied from QLineEdit. Give it a custom property and simply extend original stylesheet to handle the new property

                      Link Dynamic Properties and Stylesheets


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      L 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @Loc888
                        But if you let the buttons keep the stylesheet then
                        in your example
                        Get the style from b, set to c;
                        c to d;
                        d to f;

                        then all get stylesheet from B
                        just like
                        QString bstylesheet=ui->b->stylehseet();
                        ui->c-setStylesheet(bstylesheet);
                        ui->d-setStylesheet(bstylesheet);
                        ui->f-setStylesheet(bstylesheet);

                        Do you need to sort of rotate the style sheets ?

                        QString Bst=ui->b->stylehseet();
                        QString Cst=ui->C->stylehseet();
                        QString Dst=ui->D->stylehseet();

                        ui->c-setStylesheet(Bst);
                        ui->d-setStylesheet(Cst);
                        ui->f-setStylesheet( Dst);

                        kinda ?

                        L Offline
                        L Offline
                        Loc888
                        wrote on last edited by
                        #11

                        @mrjj Something like that, but not exacly.

                        The best way, is if i could

                        a = b;

                        b = c;

                        c=d; Etc. This is mutch more easy. Is it possible in any way??

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          Hi,
                          setting stylesheets is actually a rather "heavy" operation so using setStylesheet in a loop with small intervalls is something you should not do.

                          I would suggest, Create your own LineEdit, dervied from QLineEdit. Give it a custom property and simply extend original stylesheet to handle the new property

                          Link Dynamic Properties and Stylesheets

                          L Offline
                          L Offline
                          Loc888
                          wrote on last edited by
                          #12

                          @J.Hilk Ye, i would know how to do that.. I am not so good in that Qt and programming.
                          If i dont find any way to do that simply, i go probably to do that.

                          mrjjM 1 Reply Last reply
                          0
                          • L Loc888

                            @J.Hilk Ye, i would know how to do that.. I am not so good in that Qt and programming.
                            If i dont find any way to do that simply, i go probably to do that.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @Loc888

                            you could create function

                            void setSS(QWidget *w1, QWidget *w2) {
                             w1->setStyleSheet(w2->styleSheet());
                            }
                            
                            and call it like
                            setSS( ui->Red_To_Black, ui->Black_To_Red );
                            

                            You cannot use = as it will assign the object variable.

                            L 1 Reply Last reply
                            2
                            • J Offline
                              J Offline
                              Jorgen
                              wrote on last edited by
                              #14

                              maybe you can try the QStateMachine if you want to toggle through defined styles and order:

                              Code not tested - just as idea...

                              auto leToggleStatesStyleSheet = R"(
                                    QLineEdit[state1=true] {
                                      background: red;
                                    }
                                    QLineEdit[state2=true] {
                                      background: yellow;
                                    }
                                    QLineEdit[state3=true] {
                                      background: green;
                                    }
                                  )";
                              
                              ui->le1->setStyleSheet(leToggleStatesStyleSheet);
                              ui->le2->setStyleSheet(leToggleStatesStyleSheet);
                              ui->le3->setStyleSheet(leToggleStatesStyleSheet);
                              
                              QStateMachine *machine = new QStateMachine(this);
                              QState *state1 = new QState();
                              state1->assignProperty(ui->le1, "state1", true);
                              state1->assignProperty(ui->le2, "state2", true);
                              state1->assignProperty(ui->le3, "state3", true);
                              QState *state2 = new QState();
                              state2->assignProperty(ui->le1, "state2", true);
                              state2->assignProperty(ui->le2, "state3", true);
                              state2->assignProperty(ui->le3, "state1", true);
                              // if you using timer you can automatically go to next state
                              // state1->addTransition(state1, SIGNAL(finished()), state2);
                              QState *state3 = new QState();
                              state3->assignProperty(ui->le1, "state3", true);
                              state3->assignProperty(ui->le2, "state1", true);
                              state3->assignProperty(ui->le3, "state2", true);
                              // if you using timer you can automatically go to next state
                              // state2->addTransition(state2, SIGNAL(finished()), state3);
                              // state3->addTransition(state3, SIGNAL(finished()), state1);
                              
                              machine->addState(state1);
                              machine->addState(state2);
                              machine->addState(state3);
                              machine->setInitialState(state1);
                              machine->start();
                              
                              1 Reply Last reply
                              2
                              • mrjjM mrjj

                                @Loc888

                                you could create function

                                void setSS(QWidget *w1, QWidget *w2) {
                                 w1->setStyleSheet(w2->styleSheet());
                                }
                                
                                and call it like
                                setSS( ui->Red_To_Black, ui->Black_To_Red );
                                

                                You cannot use = as it will assign the object variable.

                                L Offline
                                L Offline
                                Loc888
                                wrote on last edited by
                                #15

                                @mrjj The problem is, i have around 30 objects, and to achieve what i want, they need to switch 30 styles at the same moment.. I dont know if that can work.
                                Can i do something like that?

                                w1->setStyleSheet(w2->styleSheet());

                                w2->setStyleSheet(w3->styleSheet());

                                w3->setStyleSheet(w4->styleSheet());

                                And progress?

                                mrjjM 1 Reply Last reply
                                0
                                • L Loc888

                                  @mrjj The problem is, i have around 30 objects, and to achieve what i want, they need to switch 30 styles at the same moment.. I dont know if that can work.
                                  Can i do something like that?

                                  w1->setStyleSheet(w2->styleSheet());

                                  w2->setStyleSheet(w3->styleSheet());

                                  w3->setStyleSheet(w4->styleSheet());

                                  And progress?

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @Loc888
                                  Well yes that could work 1 time. then some of the stylesheets will be overwritten.

                                  You could also use
                                  QList<QPushButton*> buttonList = parent->findChilden<QPushButton*>();
                                  To get a list of all pushbuttons and then loop over and rotate the stylesheets or
                                  what you are trying to really do.

                                  If you dream of it becoming some sort of animation effect, it wont happen.
                                  you will only see the last setting of stylesheet.

                                  I think i would keep the stylesheets in a list and the assign to the buttons.

                                  L 1 Reply Last reply
                                  3
                                  • mrjjM mrjj

                                    @Loc888
                                    Well yes that could work 1 time. then some of the stylesheets will be overwritten.

                                    You could also use
                                    QList<QPushButton*> buttonList = parent->findChilden<QPushButton*>();
                                    To get a list of all pushbuttons and then loop over and rotate the stylesheets or
                                    what you are trying to really do.

                                    If you dream of it becoming some sort of animation effect, it wont happen.
                                    you will only see the last setting of stylesheet.

                                    I think i would keep the stylesheets in a list and the assign to the buttons.

                                    L Offline
                                    L Offline
                                    Loc888
                                    wrote on last edited by
                                    #17

                                    @mrjj I have a timer, and he is re-calling that function. So, the stylesheet switch by one textlinedit for one cycle.

                                    This can work?

                                    w1->setStyleSheet(w2->styleSheet());

                                    w2->setStyleSheet(w3->styleSheet());

                                    w3->setStyleSheet(w4->styleSheet());

                                    w4->setStyleSheet(w5->styleSheet());

                                    w5->setStyleSheet(w6->styleSheet());

                                    w6->setStyleSheet(w1->styleSheet());

                                    mrjjM JonBJ 2 Replies Last reply
                                    0
                                    • L Loc888

                                      @mrjj I have a timer, and he is re-calling that function. So, the stylesheet switch by one textlinedit for one cycle.

                                      This can work?

                                      w1->setStyleSheet(w2->styleSheet());

                                      w2->setStyleSheet(w3->styleSheet());

                                      w3->setStyleSheet(w4->styleSheet());

                                      w4->setStyleSheet(w5->styleSheet());

                                      w5->setStyleSheet(w6->styleSheet());

                                      w6->setStyleSheet(w1->styleSheet());

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Loc888

                                      Hi
                                      Timer is a good idea.
                                      Yes that should be fine but im not sure what effect you are after.
                                      But it will rotate the style sheets for a while.

                                      1 Reply Last reply
                                      1
                                      • L Loc888

                                        @mrjj I have a timer, and he is re-calling that function. So, the stylesheet switch by one textlinedit for one cycle.

                                        This can work?

                                        w1->setStyleSheet(w2->styleSheet());

                                        w2->setStyleSheet(w3->styleSheet());

                                        w3->setStyleSheet(w4->styleSheet());

                                        w4->setStyleSheet(w5->styleSheet());

                                        w5->setStyleSheet(w6->styleSheet());

                                        w6->setStyleSheet(w1->styleSheet());

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #19

                                        @Loc888
                                        Your code:

                                        w1->setStyleSheet(w2->styleSheet());
                                        ...
                                        w6->setStyleSheet(w1->styleSheet());
                                        

                                        Do you understand that this way of writing code will always "get it wrong" when you try to set the last to the first one?

                                        L 1 Reply Last reply
                                        2
                                        • JonBJ JonB

                                          @Loc888
                                          Your code:

                                          w1->setStyleSheet(w2->styleSheet());
                                          ...
                                          w6->setStyleSheet(w1->styleSheet());
                                          

                                          Do you understand that this way of writing code will always "get it wrong" when you try to set the last to the first one?

                                          L Offline
                                          L Offline
                                          Loc888
                                          wrote on last edited by
                                          #20

                                          @JonB I have one editline to avoid this error. Yes, i see that, because at the same time when i am rotating the styles, i am rotating the text too, so i fix that error just by adding one "empty" edit line, and then hiding it. Ye, maybe this is not the best way of creating tools, but in future i am gonna fix that, at this point it's fine, hopefull it's gonna work for the style sheets too. When i learn a little bit more, then i come back and maybe change it.

                                          JonBJ 1 Reply Last reply
                                          1

                                          • Login

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