Results 1 to 8 of 8

Thread: QShortcut not always working

  1. #1
    Join Date
    Jul 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QShortcut not always working

    I use QShortcut inside a widget to close the widget by pressing esc.

    shortcut_esc = new QShortcut(this);
    shortcut_esc->setKey(Qt::Key_Escape);
    connect(shortcut_esc,SIGNAL(activated ()),this,SLOT( myslot() ));

    there are 2 differnet ways for a user to open the widget. in the first way everything works fine, the secound the shortcut doesn't work.

    in the paint -event of the widget i put a debug-message to detect which widget as the focus:

    qDebug() << "focus widget: " << QApplication::focusWidget() << focusWidget() << shortcut_esc->isEnabled() << local->hasFocus() << isActiveWindow() << hasFocus();

    the output is the same for both ways, so i think the focus is positioned right.
    when the user clicks anywhere in widget, the shortcut works fine even for the secound way.

    am i missing something?

    where is the difference between clicking on a widget and use the setFocus() Member?
    Last edited by jörg; 31st May 2011 at 13:41.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QShortcut not always working

    Quote Originally Posted by jörg View Post
    there are 2 differnet ways for a user to open the widget. in the first way everything works fine, the secound the shortcut doesn't work.
    (...) am i missing something?
    Perhaps explaining what the two ways are and what is the way that works and what is the other one that doesn't work.

  3. #3
    Join Date
    Jul 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QShortcut not always working

    i have a list view, the user has the posibility the get a detail-view of an item of the list.

    First way:
    the user can click on an item to get a detailed-view of the item.
    in the detail-view there is the shortcut to leave the detail-view back to the list view by pressing 'ESC' - that works.

    Second way:
    above the list, there is a button 'Detail-View' that switches the list do the detail of the current item
    in the detail-view there is the shortcut to leave the detail-view back to the list view by pressing 'ESC' - that doesn't work.
    When i use the second way and just click somewhere in the app, than the shortcut works.

    The think i do not understand is that for a shortcut to work, it is important, that the widget with the shortcut has the focus (or on of its childs) and that the toplevel-widget is active.
    The detail-view is the same for both ways, and the debug output says that the focused widget are even the same for both ways. So there must be something more importent for the shortcut to work. What is it?

    This line in the 'OnPaint'-Member says for both ways the same widgets, shortcut is enabled, has focus, is active
    qDebug() << "focus widget: " << QApplication::focusWidget() << focusWidget() << shortcut_esc->isEnabled() << local->hasFocus() << isActiveWindow() << hasFocus();

  4. #4
    Join Date
    Jul 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QShortcut not always working

    no ideas anyone?

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QShortcut not always working

    Do you get a message on the console if you click the shortcut multiple times saying that the shortcut ambiguous?

    By the way its really hard to image what you are doing without real code (or at least a shortcut)

  6. #6
    Join Date
    Jul 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QShortcut not always working

    i use the following shortcuts:

    shortcut_esc = new QShortcut(this);
    shortcut_esc->setKey(Qt::Key_Escape);
    connect(shortcut_esc,SIGNAL(activated ()),PlayerExtraScreen::instance(),SLOT( showListView() ));

    shortcut_pgup = new QShortcut(this);
    shortcut_pgup->setKey(Qt::Key_PageUp);
    connect(shortcut_pgup,SIGNAL(activated ()),this,SLOT( backItem() ));
    connect(shortcut_pgup,SIGNAL(activatedAmbiguously ()),this,SLOT( backItem() ));

    shortcut_pgdown = new QShortcut(this);
    shortcut_pgdown->setKey(Qt::Key_PageDown);
    connect(shortcut_pgdown,SIGNAL(activated ()),this,SLOT( nextItem() ));
    connect(shortcut_pgdown,SIGNAL(activatedAmbiguousl y()),this,SLOT( nextItem() ));

    as you can see a also use the Ambigously Signal for testing. but the shortcvut is not ambiuge, i already checked that.
    it seems the the key-pressed event is 'swallowed' by an other widget (cause of the fakt that after i clicked somewhere in the app, the shortcut works).
    and that is the point wich i do not understand:
    the widget is the active widget (the one with the focus) - so why does it get the key event one the first way and not on the second. there must be something beside the attribut 'hasFocus' that the shortcut depends on. what else can it be???? please help

  7. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QShortcut not always working

    What is "this" in the QShortcut Contstructor?

  8. #8
    Join Date
    Jul 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QShortcut not always working

    the code snipped is part of the constructor of the detailview class, whitch is the parent of the shortcut.
    so the 'this' pointer is ther instance of the detailview containing the shortcut

Similar Threads

  1. QShortcut not work!
    By leeeryan in forum Newbie
    Replies: 1
    Last Post: 16th December 2010, 09:27
  2. QShortcut SIGNAL must be of 0 arguments?
    By Wasabi in forum Newbie
    Replies: 1
    Last Post: 17th August 2010, 21:26
  3. QShortcut not working
    By berinder in forum Qt Programming
    Replies: 0
    Last Post: 24th November 2008, 10:20
  4. QShortcut
    By SailinShoes in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 10:42
  5. QShortcut - doesn't work with Qt::Key_Return
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2006, 12:00

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.