how to make a function that takes pushbutton as argument
-
hi all,
I am very new to Qt creator and also to C++ so I cannot figure out how to do the following:
I have a number of pushbuttons that are animated based on some external signals. I am animating these based on a TimerSlot, like this:void keypad::TimerSlot() { if (button1_inhibit) == true) { ui->button1->setEnabled(false); } else { ui->button1->setEnabled(true); } if (button1_feedback)== true) { ui->button1->setStyleSheet("background-color: white"); ui->button1->update(); } else { ui->button1->setStyleSheet("background-color: grey"); ui->button1->update(); } // same thing for button_2, 3, 4 and so on. }I have many buttons and I wanted to create a function that takes arguments
bool inhibit,bool feedbackandbuttonand that I then can call repeatedly for each button. Instead of copy and paste theif..elsefor each button, something like this:void animateButton (bool inhibit, bool feedback, keypad::ui::button) { if (inhibit == true) { ui->button->setEnabled(false); } else { ui->button->setEnabled(true); } } //...however I try I cannot get this to work and I have not been able to find any information when searching, probably because I don't know how to search for it.. I get errors that ui is not declared in this scope and the button is not a member of keypad and various other errors depeding on what I try. Can someone please point me in the right direction. Thanks a lot.
-
Hi and welcome to devnet,
Can you explain how you manage these external signals ?
-
Hi and thanks. Yes, I can try. They are external signals, sniffed from the network. They can change value at any time so I think I have to check them periodically based on a timer. if they change value they should animate the button. I have not implemented the actual fetching of the signals so I don't know exactly how it works but I hope that explanation is enough.
-
hi all,
I am very new to Qt creator and also to C++ so I cannot figure out how to do the following:
I have a number of pushbuttons that are animated based on some external signals. I am animating these based on a TimerSlot, like this:void keypad::TimerSlot() { if (button1_inhibit) == true) { ui->button1->setEnabled(false); } else { ui->button1->setEnabled(true); } if (button1_feedback)== true) { ui->button1->setStyleSheet("background-color: white"); ui->button1->update(); } else { ui->button1->setStyleSheet("background-color: grey"); ui->button1->update(); } // same thing for button_2, 3, 4 and so on. }I have many buttons and I wanted to create a function that takes arguments
bool inhibit,bool feedbackandbuttonand that I then can call repeatedly for each button. Instead of copy and paste theif..elsefor each button, something like this:void animateButton (bool inhibit, bool feedback, keypad::ui::button) { if (inhibit == true) { ui->button->setEnabled(false); } else { ui->button->setEnabled(true); } } //...however I try I cannot get this to work and I have not been able to find any information when searching, probably because I don't know how to search for it.. I get errors that ui is not declared in this scope and the button is not a member of keypad and various other errors depeding on what I try. Can someone please point me in the right direction. Thanks a lot.
@mgdfp
I don't mean to sound rude because you have asked your question nicely, but you need to understand C++ and particularly classes before you will get far with Qt. You would benefit from taking a pause and reading up on those before continuing.void keypad::TimerSlot()This is a method defined in class
keypad. Theuivariable is a member of that class, and that's why you can access it inside this function.void animateButton (bool inhibit, bool feedback, keypad::ui::button)This method is not defined in class
keypad. Unless it is supposed to be, you won't be able to access variableui, hence your "I get errors that ui is not declared in this scope and the button is not a member of keypad".In fact, this function is not defined in any class. It is a "global" function, like in C. In C++, at least as a rule of thumb, just about all of your code should be written inside classes.
keypad::ui::buttonin your argument list is wrong.What you probably intend is something like:
void SomeClassEitherKeypadOrSomethingElse::animateButton (bool inhibit, bool feedback, QPushbutton *button) { if (inhibit == true) { button->setEnabled(false); } else { button->setEnabled(true); } }To write code for doing this sort of thing on your multiple buttons, you want put all your buttons into an array/list, such as
QList<QPushButton*> buttons = { ui->button1, ui->button2, ui->button3, ... };Then instead of having to write code for each button explicitly, you can perform operations on all the buttons by looping over the items in the list:
for (QPushButton *button : buttons) { button->setStyleSheet("..."); ... }