Results 1 to 2 of 2

Thread: How to highlight a menu item by program input

  1. #1
    Join Date
    Jul 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to highlight a menu item by program input

    Hi,
    I'm working on a piece of software (on Linux/X11), which uses Qt application menus with simulated keyboard inputs and no mouse.

    Simulating keyboard inputs works with:

    Qt Code:
    1. QKeyEvent keyEvent(QEvent::KeyPress,Qt::Key_Tab, Qt::NoModifier);
    2. QWidget* pWidget = qApp->activeWindow()->focusWidget();
    3. qApp->sendEvent(pWidget, &keyEvent);
    To copy to clipboard, switch view to plain text mode 

    … for tab input or:

    Qt Code:
    1. QKeyEvent keyEvent(QEvent::KeyPress,Qt::Key_..., Qt::NoModifier);
    2. QWidget* pWidget = qApp->activePopupWidget();
    3. qApp->sendEvent(pWidget, &keyEvent);
    To copy to clipboard, switch view to plain text mode 

    … for other keys

    But I always have to use the mouse to get an initial highlighted menu item. I tried a lot of things to get this highlighted item by program input.
    With Qmenu::exec() I succeded to open a popup menu but could not get a highlighted item there, so the simulated inputs have no effect.
    What am I doing wrong?

  2. #2
    Join Date
    Aug 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to highlight a menu item by program input

    Have you tried to send the KeyRelease event, after sending the KeyPress? For example I used this when I've tried to highlight my QMenu like it's going in Windows:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent* event)
    2. {
    3. if(event->key() == Qt::Key_Alt)
    4. {
    5. if(menuBar()->isHidden())
    6. {
    7. menuBar()->show();
    8. QKeyEvent altPress(QEvent::KeyPress, Qt::Key_Alt, Qt::NoModifier);
    9. QKeyEvent altRelease(QEvent::KeyRelease, Qt::Key_Alt, Qt::NoModifier);
    10.  
    11. qApp->sendEvent(menuBar(), &altPress);
    12. qApp->sendEvent(menuBar(), &altRelease);
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 4th November 2011, 11:25
  2. Highlight Tree Item Viw
    By sajis997 in forum Qt Programming
    Replies: 4
    Last Post: 19th July 2011, 09:40
  3. Graphics item highlight
    By mukunda in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2011, 18:21
  4. Menu Item has no highlight
    By blackfox in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2008, 12:17
  5. How to highlight a letter in menu
    By npc in forum Newbie
    Replies: 2
    Last Post: 14th March 2006, 12:18

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.