Switch object style Sheet
-
@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 ?
-
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
-
@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.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.
-
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();
-
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.
@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?
-
@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?
@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.
-
@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.
@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());
-
@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());
-
@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());
-
@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?
@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.
-
@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.
@Loc888
Yes, that's fine, just checking you understood that.Actually, upon re-reading I'm not sure whether we're talking about the same thing. To be clear: by the time you read the
w1->styleSheet()
in the final assignment it will already have been "lost" via the firstw1->setStyleSheet(...)
assignment. You need to save that somewhere. Which you may be saying you are aware of... -
PS. Can anyone know, why i cant start the timer into any of loops?? I mean, while, for etc. I need to do that, because i dont find any "update function", so basicly it's gonna change the refire time but when the timer is already started.
When:
Number_Of_Cycles = 20;
Cycles_Time = 1000;
while(Number_Of_Cycles != 0)
{Number_Of_Cycles--; timer->start(Cycles_Time);
<<(Here i need to change the time from 1000 to 500ms, but it must be done, when the timer start's at refire time of 1000 and after 20 cycles, it doesn't stop but just continue to update by old time 1000.)
......Some code timer -> stop();
}
Sorry for typing it here, but i dont want to create new topic if it's simple.
I just need any way to start it in while or for loop(because i need any cycle counter), and then update it. -
PS. Can anyone know, why i cant start the timer into any of loops?? I mean, while, for etc. I need to do that, because i dont find any "update function", so basicly it's gonna change the refire time but when the timer is already started.
When:
Number_Of_Cycles = 20;
Cycles_Time = 1000;
while(Number_Of_Cycles != 0)
{Number_Of_Cycles--; timer->start(Cycles_Time);
<<(Here i need to change the time from 1000 to 500ms, but it must be done, when the timer start's at refire time of 1000 and after 20 cycles, it doesn't stop but just continue to update by old time 1000.)
......Some code timer -> stop();
}
Sorry for typing it here, but i dont want to create new topic if it's simple.
I just need any way to start it in while or for loop(because i need any cycle counter), and then update it. -
@Loc888
You cannot change the interval time afterstart()
and beforestop()
, if that is what you mean. You canstart
off a timer with a different interval after is hasstop
ped. This can be implicit by re-callingstart
with a different value.@JonB Yes i am doing it already in this way cuz i need it long time ago, but i thought there should be any better way to do that, or any update function so i asked.
But the problem with this method is that, something like that, doesn't work with loops, that's the problem and i mean start then stop, change the time, and restart.Can i do something about that? Why it's not working when the start function is called into any type of loops?
-
@Loc888 because your loop will not delayed by the timer - you just start and stop the timer inside a loop, which runs as fast as possible (depending on your CPU)
Maybe you are looking for the the timeout signal of QTimer class.
So just create a QTimer object and connect your slot (doing the rotation stuff) to the timeout signal.
Inside your slot you can also modify the interval of the timer. -
Everything is working good, i fix almost all my problems. Stylesheets rotate very good, exactly how i wanted it, i use this code:
w1->setStyleSheet(w2->styleSheet());
and progress the number of objects.
I fix the timer cycles issues just:
- by adding second timer
- stop old one
- start new
- then i add one global variable to controll the cycles number and decrement it when new timer runs the function
- when it's over, stop new timer and restart the old.
Thanks everyone for help.