Qt. I have a form. There is 2 widgets on it, that contains other widgets, buttons, line edits etc. I need: When user sets focus, clicks by mouse, or does something with first widget or elements, that it contain - I need to set variable to 0. If he do something same with second widget - variable must be set in 1. How to do that?
Asked
Active
Viewed 4,209 times
2 Answers
6
QApplication::focusWidget()
returns pointer to widget which has focus at this moment. Also there is QApplication::focusChanged(QWidget *old, QWidget *now)
signal and you can connect it to slot to change variable.

fasked
- 3,555
- 1
- 19
- 36
-
Thank you its really simple, and it works. void FormBackupPower::application_focusChanged (QWidget*old, QWidget*now){ const QWidget *widget = now; const QWidget *step1 = dynamic_cast
(this->ui->groupBoxStep1); const QWidget *step2 = dynamic_cast – Sacha_D Oct 19 '12 at 11:12(this->ui->groupBoxStep2); while (widget != NULL) { if (widget == step1) { this->setCurrentStep(FormBackupPower::currentStep_low); break; } if (widget == step2) { this->setCurrentStep(FormBackupPower::currentStep_low + 1); break; } widget = widget->parentWidget(); } }
0
You can always redefine/(create subclasses by inheritance) the widgets with your own slots and signals.
As far as I can understand your requirement, you can do.
QObject::connect(wid1,SIGNAL(clicked()),yourvariableclass,SLOT(setMyVariable_wid1())); QObject::connect(wid2,SIGNAL(clicked()),yourvariableclass,SLOT(setMyVariable_wid2()));
If my answer isn't apt for your question, please explain the problem a little more. I can help you with it. :)

G Sree Teja Simha
- 495
- 1
- 6
- 15
-
I dont want to redefine each component. There is must be a more human way. – Sacha_D Oct 18 '12 at 17:54
-
Then simply use the signal slot mechanism to connect the focus event of each widget to a slot which takes care of flipping the variable's value between 0 and 1. The syntax goes like what I've mentioned. – G Sree Teja Simha Oct 19 '12 at 06:23
-
Also, its no where as difficult as you think to inherit a widget class. You just inherit a widget class, add new/custom signals and slots. In order to use it in the UI, you can add a normal widget and then promote it into your custom widget type. But as far as I can see from your requirement, you can simply use the signal slot mechanism. – G Sree Teja Simha Oct 19 '12 at 06:25