push buttons are in large numbers
-
Hi,
I have many push buttons (10 nos) and I need to change its style based various conditions, as follows.
I'm wondering , Is there a easy way to do this?
if(value == 0) { ui->btn1->setStyleSheet(StyleSheetOn); ui->btn2->setStyleSheet(StyleSheetOff); ..... ui->btn10->setStyleSheet(StyleSheetOff); } else if(value == 1) { ui->btn2->setStyleSheet(StyleSheetOn); ui->btn1->setStyleSheet(StyleSheetOff); ..... ui->btn10->setStyleSheet(StyleSheetOff); } else if(value == 2) { ui->btn3->setStyleSheet(StyleSheetOn); ui->btn2->setStyleSheet(StyleSheetOff); ..... ui->btn10->setStyleSheet(StyleSheetOff); } -
Hi
Well if the issue is to turn off all other than the On one, you could doQList<QPushButton *> List = findChildren<QPushButton *>(); for (QPushButton * button : List) { button->setStyleSheet(StyleSheetOff); }Then turn On the single one you want.
If you make sure buttons comes in right order then you can also use this way to do the value part.
List[value]->setStyleSheet(StyleSheetOn);
But you have to make sure it matches up or it will crash :)
so button 1 will be in index zero etc.If you have more Buttons on the form than you want in list then you can add a widget to hold the buttons you want to affect and use
QList<QPushButton *> List = TheWidgetHolder->findChildren<QPushButton *>();
So we only ask that widget about buttons and not the whole of mainwindow.
-
@mrjj Thanks a lot for your suggestion.
How do I make sure the button comes in the right order? what's the correct way to name the push button in this case? -
@mrjj Thanks a lot for your suggestion.
How do I make sure the button comes in the right order? what's the correct way to name the push button in this case?Hi
That is the kinda tricky part as they seem to come in creation order.
Meaning in that order that you dragged them to the form.The name is not important in this context.
It might be ok already.
just dump the list and see.
If you use
List[value]->then value must never be bigger than list.size()-1 ( -1 as it starts with zero)
-
@mrjj said in push buttons are in large numbers:
QList<QPushButton *> List = findChildren<QPushButton *>();
Instead getting all the pushbuttons on the screen to a list , how do I add selected push buttons into the list, say push button names are
btn_1,btn_2,btn_3etc? -
@mrjj said in push buttons are in large numbers:
QList<QPushButton *> List = findChildren<QPushButton *>();
Instead getting all the pushbuttons on the screen to a list , how do I add selected push buttons into the list, say push button names are
btn_1,btn_2,btn_3etc?@russjohn834
Hi
As i wrote, just put them in a extra QWidget and do
QList<QPushButton *> List = TheWidgetHolder->findChildren<QPushButton *>();
that way you get only the children of the TheWidgetHolder.
If that is too annoying you could also just make the list manuallyQList<QPushButton *> list{ ui->bt1, ui->bt2 .... };