Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is there a way to send a key press to a widget without having to move the mouse over it?



  • Hi,

    The app I am using has many qt widgets, but unfortunately they didn't expose all of the functionality in code, so I need to invoke the hotkey to perform specific actions, such as clear a tree view in a qt widget.

    The issue is, the whole app is designed to receive the key events based where the mouse is. So if the mouse is over widget 1, then only widget one will receive all the inputs.

    Is there a way to bypass this and send the key straight to the widget like some sort of lower level access?

    Otherwise even when I move the cursor before sending a key event, the inputs are sent to where my mouse cursor is currently.

    Thanks a lot in advance :)


  • Lifetime Qt Champion



  • Thanks I tried that before but no luck:

    perfMonWidget = QtWidgets.QApplication.widgetAt(355, 467)
    keypress = QtGui.QKeyEvent(QtGui.QKeyEvent.KeyPress, QtCore.Qt.Key_L, QtCore.Qt.NoModifier, "L")
    
    oldpos = QtGui.QCursor().pos()
    QtGui.QCursor().setPos(355, 467)
    for i in range(10):
        QtGui.QGuiApplication.sendEvent(perfMonWidget, keypress)
        QtWidgets.QApplication.sendEvent(perfMonWidget, keypress)


  • @lachdanan Did you try giving focus to the widget?



  • Yes I called focusWidget before on it but still the same. I also moved my mouse over the widget before executing the code, but I am not getting any key event sent, because it doesn't trigger the hotkey action. If I press myself, the widget reacts.



  • Strangely I tested this with another widget in that app, that one receives the event when I send mouse event and keyboard event but the one I am interested somehow doesn't do anything.


  • Lifetime Qt Champion

    Hi,

    What kind of widget is that ?
    What are you trying to do with it ?



  • @lachdanan said in Is there a way to send a key press to a widget without having to move the mouse over it?:

    send the key straight to the widget

    What if you use QTest::keyClicks? From documentation:

    Simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test before each key click.


  • Lifetime Qt Champion

    @Pablo-J-Rogina the QTest module is meant to be used only for testing not in production code.



  • @SGaist

    It's the widget on screen left. I need to send the keypress to delete a profile, otherwise there is no delete function exposed for me to do it using the provided python API.

    The hard coded behaviour is that if there are no profiles loaded into this widget, and you load one, it will show it. But if you load a profile when there are other profiles in the dropdown list, it won't load them, it will only add them to the list. But because I want to always show my most current profile data, I need to clear everything and load my profile.

    That's why I need to invoke this delete profile key press multiple times to make sure the widget is empty.

    alt text


  • Lifetime Qt Champion

    Hi
    It looks like a QToolButton
    How do you get the pointer to it ?

    sendEvent normally just works so i think there is something else up.


  • Lifetime Qt Champion

    Before going further:

    • What Python API ?
    • Where does that widget you want to send event to come from ?


  • @mrjj actually it's not a button. The entire tree view is the widget I need to send the key press. The action is inside the menu.

    @SGaist said in Is there a way to send a key press to a widget without having to move the mouse over it?:

    Before going further:

    • What Python API ?
    • Where does that widget you want to send event to come from ?

    The python API of the app I posted the screen shot of. It exposes many functionality:

    https://www.sidefx.com/docs/houdini/hom/index.html

    The widget is part of the whole app.


  • Lifetime Qt Champion

    The action is inside the menu.

    So you try send a keypress to a context menu (QAction) inside a TreeView ?



  • No I tried to send the keypress to the main widget, and if I manually press the same key over the widget anywhere, it will perform that action in the menu. So I don't have to click the menu itself or send to it. The whole panel can/should accept the key event as I can do it manually but in code nothing happens.

    Also there is still the issue of having to have the mouse over the widget. The whole app's hotkeys work based on what panel has the focus.


  • Lifetime Qt Champion

    @lachdanan
    Hi
    Ok so its more like a QShortcut you try to trigger.
    One thing comes to mind if you in Qt uses eventfilters on a View, you set it on its viewport as thats the widget you actually click on/interact with.

    Could it be something like that ? I mean if you get the actual view widget it might not care as such for the key.

    Do you have acces to most of Qt' api or just a selection ?
    Like https://doc.qt.io/qt-5/qmetaobject.html



  • @mrjj
    By viewport do you mean the panel on the screen left or screen center? Because there is an actual 3d viewport and that one has its own set of hotkeys. If you send it to that, it will do some other action for the same key.

    Basically I have access to all of qt and pyside2 AFAIK, except maybe things that are not exposed like key scan codes. Otherwise I can get/call anything on qwidget qapplication etc. It allows full qt programming.


  • Lifetime Qt Champion

    @lachdanan
    Hi
    Actually i mean the viewport object that all Qt view has. (from QScrollArea)
    (ui->treeView->viewport();)
    Not a 3d viewport :)

    Ok so it does work for other keys / Widgets, but that key and that widget will not?

    oh thats neat.
    You can try call dumpObjectTree() on the widget you think it is and see how its structured. Might give us
    hints on what is going on.



  • @mrjj said in Is there a way to send a key press to a widget without having to move the mouse over it?:

    dumpObjectTree

    Thanks, I have to check that viewport method, I didn't see that before.

    Basically I tried another widget I normally work on and that accepted the key. But this widget, no key I sent, were accepted.

    I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?

    But I acquire the widget currently using widgetAt method and after that I recursively gathered all of its children which are these:

    <PySide2.QtWidgets.QWidget object at 0x000000006CDA9DC8>
    [<PySide2.QtWidgets.QWidget object at 0x000000006CDA9DC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B3FC508>, <PySide2.QtWidgets.QWidget object at 0x000000006CDA9E88>, <PySide2.QtWidgets.QWidget object at 0x000000009B5E6A88>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42B948>, <PySide2.QtWidgets.QWidget object at 0x000000009B42B7C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42B108>, <PySide2.QtWidgets.QWidget object at 0x000000009B42B1C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42BFC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B2A2D88>, <PySide2.QtWidgets.QWidget object at 0x000000009B316CC8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316D08>, <PySide2.QtWidgets.QWidget object at 0x000000009B40FDC8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316BC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B316C08>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316A08>, <PySide2.QtWidgets.QWidget object at 0x000000009B3169C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B3174C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B3170C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317848>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B317388>, <PySide2.QtWidgets.QWidget object at 0x000000009B3173C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317A48>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B3175C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317148>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B317D88>]

    Not sure if that helps. Unfortunately in this app, this is how you have to blindly find the widget you are looking for.


  • Lifetime Qt Champion

    Hi

    But this widget, no key I sent, was accepted.

    Ok so that often would mean that its not the right widget we send to.
    It should work the same (event wise) regardless of the widget but
    if nothing happens it smell like it's not hooked up as we think or similar.

    • I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?
      Oh, i assumed it uses the qDebug method as it comes the same place but you could be right.

    Hmm,. it only shows QVBoxLayout names.
    Do/can you call widgetprt->metaObject()->className()
    so we maybe could see the it better ?

    Also just to be sure. We are talking about this one
    alt text

    One thing I might try if all else fails is to install an event filter (if that works in python)
    on the ListView (or what it is )
    and see what is sent to it when you press the key.



  • @mrjj Thanks, when I call metaObject().className() I get these:

    QWidget
    QWidget
    QWidget
    QWidget
    QVBoxLayout
    QWidget
    QVBoxLayout
    QWidget
    QVBoxLayout
    QWidget
    QWidget
    QVBoxLayout
    QWidget
    QVBoxLayout
    QWidget
    QVBoxLayout
    QWidget
    QWidget
    QVBoxLayout
    QWidget
    QVBoxLayout
    QWidget
    QWidget
    QVBoxLayout
    QWidget
    QVBoxLayout

    Yes that's the panel we are talking about.

    Ok I will look into installing an event filter. I am not sure if that's supported so will have to check.

    I also just tried sending the same key events to all the child widgets but same result.


  • Lifetime Qt Champion

    Hi
    Hmm ok, that normally gives the concreate class names and not just QWidget. :)

    Ok so is that panel. and we talk about the actual tree view there?
    you have it selected (item selected) and press a key to fire the function?
    Just try to understand what/where exactly you try to trigger

    • I also just tried sending the same key events to all the child widgets but same result.

    Hmm If it is a QShortCut key, it might be registered to other parent but you say each part has
    different keys so it's valid to assume its indeed added to list directly and not higher up in the panel/area.

    Im not a python user so I cant say if event filters works. but why not. :)

    Since this a commercial project, its not possible just to ask them how to trigger this ?



  • @mrjj said in Is there a way to send a key press to a widget without having to move the mouse over it?:

    Hi
    Hmm ok, that normally gives the concreate class names and not just QWidget. :)

    Ok so is that panel. and we talk about the actual tree view there?
    you have it selected (item selected) and press a key to fire the function?
    Just try to understand what/where exactly you try to trigger

    • I also just tried sending the same key events to all the child widgets but same result.

    Hmm If it is a QShortCut key, it might be registered to other parent but you say each part has
    different keys so it's valid to assume its indeed added to list directly and not higher up in the panel/area.

    Im not a python user so I cant say if event filters works. but why not. :)

    Since this a commercial project, its not possible just to ask them how to trigger this ?

    Actually not just tree view but the whole panel because anywhere you invoke the key it will trigger the action.

    The action I chose is called event log that will hide the tree view but show a console like control.

    I guess I could ask them also. But they might not reply also as it's more like qt which sometimes they know how to help.

    Because there qt support in this app is very slim, everything is raw.


  • Lifetime Qt Champion

    @lachdanan
    Ok, and you also tried to send to the top parent ? / the widget that holds the panel.
    It sounds like a QShortcut since you dont need to click menu or button.

    Yeah, they might not be able to help in support but any info regarding who owns
    the QShortcut would help.


  • Lifetime Qt Champion

    Hi,

    Since the application might be built with Qt, did you consider using GammaRay to inspect it ? Using it you might find the right widget you need and activate it directly.



  • @mrjj said in Is there a way to send a key press to a widget without having to move the mouse over it?:

    @lachdanan
    Ok, and you also tried to send to the top parent ? / the widget that holds the panel.
    It sounds like a QShortcut since you dont need to click menu or button.

    Yeah, they might not be able to help in support but any info regarding who owns
    the QShortcut would help.

    Yes I just tried sending it to the mainQtWindow and placed my cursor on the right widget before running the code, same thing.

    @SGaist said in Is there a way to send a key press to a widget without having to move the mouse over it?:

    Hi,

    Since the application might be built with Qt, did you consider using GammaRay to inspect it ? Using it you might find the right widget you need and activate it directly.

    Thanks, I just looked it up, seems like a massive download but I might give this a try.



  • @SGaist
    Hmm, does this KDAB GammaRay play with my Python/PySide2 Qt development instead of C++? Also, no Qt Creator for development, everything run through MS VSCode in Python mode?



  • This post is deleted!

Log in to reply