Results 1 to 2 of 2

Thread: widget focus question

  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default widget focus question

    I have a gui with multiple LineEdits widget. I am trying to determine which lineedit got focus. I know that there is this hasFocus() method but I can't seems to get it to work.

    code:
    ...
    QLineEdit *Name_lineEdit = new QLineEdit();
    QLineEdit *Pass_lineEdit = new QLineEdit();

    Pass_lineEdit->setFocus();

    printf("P focus = %d\n\r", Pass_lineEdit->hasFocus());
    printf("N focus = %d\n\r", Name_lineEdit->hasFocus());

    app.exec();

    Output return
    P focus = 0
    N focus = 0

    Why is Pass_lineEdit->hasFocus() return 0? I would expect it to be 1
    Is there any way to determine which widget got focus without having to go through each widget focus property?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: widget focus question

    Quote Originally Posted by eva2002 View Post
    Why is Pass_lineEdit->hasFocus() return 0? I would expect it to be 1
    Because Qt is event-driven. You need to return control to the event loop for the focus to change.

    Is there any way to determine which widget got focus without having to go through each widget focus property?
    QApplication::focusWidget()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Window focus question
    By waynew in forum Newbie
    Replies: 3
    Last Post: 31st October 2009, 23:11
  2. widget focus ---------
    By jrodway in forum Qt Programming
    Replies: 11
    Last Post: 27th April 2009, 09:16
  3. Replies: 2
    Last Post: 27th March 2009, 12:25
  4. Widget Focus
    By navi1084 in forum Qt Programming
    Replies: 6
    Last Post: 29th September 2008, 10:22
  5. Customize on focus widget
    By vql in forum Qt Programming
    Replies: 9
    Last Post: 9th April 2008, 18:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.