Sacha_D
Sacha_D

Reputation: 100

Qt how to know the widget, in which elements user set focus last time

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?

Upvotes: 3

Views: 4334

Answers (2)

fasked
fasked

Reputation: 3645

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.

Upvotes: 6

G Sree Teja Simha
G Sree Teja Simha

Reputation: 505

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. :)

Upvotes: 0

Related Questions