Qwidget Keyboard Event Focus (Qapplication source)

Source: Internet
Author: User

    • In Qt, keyboard events are inseparable from Qwidget's focus: In general, a qwidget with focus Qwidget or grabkeyboard () can accept keyboard events.
Who is the keyboard event sent to?

How to determine who will receive keyboard events, may wish to see a little qapplication Source:

X11 under

    Qetwidget *keywidget=0;    BOOL Grabbed=false;    if (event->type==xkeypress | | event->type==xkeyrelease) {        keywidget = (qetwidget*) qwidget::keyboardgrabber ();        if (keywidget) {            grabbed = true;        } else if (!keywidget) {            if (D->inpopupmode ())//No focus widget, see if We have a popup                Keywidget = (qetwidget*) (Activepopupwidget ()->focuswidget ()? Activepopupwidget () Focuswidget (): Activepopupwidget ());            else if (qapplicationprivate::focus_widget)                keywidget = (qetwidget*) qapplicationprivate::focus_widget;            else if (widget)                keywidget = (qetwidget*) Widget->window ();        }    }

Windows under

            Qwidget *g = Qwidget::keyboardgrabber (); if (g && qt_get_tablet_widget () && hwnd = = Qt_get_tablet_widget ()->winid ()) {//If we g                Et an event for the internal Tablet widget,/and then don ' t send it to the keyboard grabber, but                Send it to the widget itself (we don ' t use it right//now, just in case).            g = 0;            } if (g) widget = (qetwidget*) g; else if (Qapplication::activepopupwidget ()) widget = (qetwidget*) qapplication::activepopupwidget ()->FOCU Swidget ()? (qetwidget*)             Qapplication::activepopupwidget ()->focuswidget (): (qetwidget*) Qapplication::activepopupwidget ();            else if (Qapplication::focuswidget ()) widget = (qetwidget*) qapplication::focuswidget (); else if (!widget | | Widget->internalwinid () = = GetFocus ())//We faked the Message to go to exactly this widget. Widget = (qetwidget*) Widget->window ();

Approximate order:

    • Qwidget::keyboardgrabber ()
    • Qapplication::activepopupwidget ()
    • Qapplication::focuswidget ()
    • Qwidget::window () [Note: For native the widget that receives the keyboard event, QT is sent to its owning window]
Switch focus between Qwidget

In the article on QT keyboard events We mentioned that this is related to Focuspolicy. We can use the TAB key or the mouse click to make a Qwidget get the focus.

question: When we press the TAB key (or up or down ARROW keys), how is the next or focusable Qwidget determined?

We re-post the source code for the Qwidget::event () that was last posted above:

    Case qevent::keypress: {        qkeyevent *k = (qkeyevent *) event;        BOOL res = false;        if (! ( K->modifiers () & (Qt::controlmodifier | Qt::altmodifier)) {  //### Add metamodifier?            if (k->key () = = Qt::key_backtab                | | (K->key () = = Qt::key_tab && (k->modifiers () & Qt::shiftmodifier)))                res = Focusnextprevchild (false);            else if (k->key () = = Qt::key_tab)                res = Focusnextprevchild (true);            if (res) break                ;        }        Keypressevent (k);

Always feel qwidget::focusnextprevchild () This function is a bit of a misnomer (or a bit awkward), because:

BOOL Qwidget::focusnextprevchild (bool next) {    q_d (qwidget);    qwidget* p = parentwidget ();    BOOL Issubwindow = (windowtype () = = Qt::subwindow);    if (!iswindow () &&!issubwindow && p)        return P->focusnextprevchild (next); ...}

When we invoke the member of a widget, it will eventually be called recursively to the Focusnextprevchild member of the window in which it resides. (However, this is a protected virtual function that can be overridden in a derived class to control the focus movement in the derived class instance.) )

Prev/next

There are 3 member variables in the qwidgetprivate:

Class Q_gui_export Qwidgetprivate:public qobjectprivate{    ... Qwidget *focus_next;    Qwidget *focus_prev;    Qwidget *focus_child;

These 3 variables can be used separately:

    • Qwidget::nextinfocuschain ()
    • Qwidget::p Reviousinfocuschain ()
    • Qwidget::focuswidget () [Note distinction: qapplication::focuswidget ()]

To obtain.

The first two can be used to form a list of focus chains.

void Qwidgetprivate::init (Qwidget *parentwidget, Qt::windowflags f) {    Q_Q (qwidget);    Focus_next = Focus_prev = Q;...void qwidgetprivate::reparentfocuswidgets (qwidget * oldtlw) {...

The order of the widgets in the focus list can be adjusted by Qwidget::settaborder ()

How about placing a large number of buttons on the widget?

Something like a soft keyboard, for example, puts a lot of Qpushbutton on a qwidget. At this point, the upper and lower left and right arrows can also be used to move the focus except Tab/shift+tab.

This is because:

void Qabstractbutton::keypressevent (Qkeyevent *e) {    bool next = true;    Switch (E->key ()) {case    qt::key_up: Case    qt::key_left:        next = false;        Fall through case    qt::key_right: Case    qt::key_down:            ... Focusnextprevchild (next);}
Focus Proxy
    • Qwidget::setfocusproxy ()
    • Qwidget::focusproxy ()

Manual said in the more clear:

    • If There is a focus proxy, SetFocus () and Hasfocus () operate on the focus proxy.

Simply list the source code:

void Qwidget::setfocus (Qt::focusreason reason) {    Qwidget *f = this;    while (F->d_func ()->extra && f->d_func ()->extra->focus_proxy)        f = f->d_func () Extra->focus_proxy;bool qwidget::hasfocus () const{    const qwidget* w = this;    while (W->d_func ()->extra && w->d_func ()->extra->focus_proxy)        w = W->d_func () extra->focus_proxy;
Other
    • For the Qt for Embedded Linux, Symbian, and Windows CE, there is also a
Qapplication::setnavigationmode ()

Settings can be controlled by the arrow keys to move the focus up or down.

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

SetFocus () is to have a form get focus

Setfocuspolicy is how to set the form to get focus

He focus policy is qt::tabfocus if the widget accepts keyboard focus by tabbing, qt::clickfocus if T He widget accepts focus by clicking, qt::strongfocus if it accepts both, and Qt::nofocus (the default) I F It does not accept focus at all.

voidQwidget::setfocusproxy (Qwidget * w) [virtual] 就是把窗体获得焦点时的处理,委托给窗体w处理

Sets this widget's focus proxy to W. If W is 0, the This function resets the this widget has an any focus proxy.

Some widgets, such as Qcombobox, can "has focus," but create a child widgets to actually handle the focus. Qcombobox, for example, creates a qlineedit.

Setfocusproxy () sets the widget which would actually get focus when "this widget" gets it. If There is a focus proxy, Focuspolicy (), Setfocuspolicy (), SetFocus () and Hasfocus () all operate on the focus proxy.

See also Focusproxy ().

Set the widget's focus proxy to W. If W is 0, the function sets the widget to have no focus proxy.

Some widgets, such as Qcombobox, can "have focus," but they create a child widget that actually handles the focus. For example, Qcombobox created called Qlineedit.

Setfocusproxy () is used to specify who will actually handle the focus when the widget gets the focus. If a widget has focus proxy,focuspolicy (), Setfocuspolicy (), SetFocus (), and Hasfocus () are all working with the focus proxy.

Http://blog.sina.com.cn/s/blog_a401a1ea0101ec9z.html

Qwidget Keyboard Event Focus (Qapplication source)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.