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

PySide find out last clicked QLineEdit



  • Hi! I'm new to PySide and this is my first complex project.
    I have main widget (inherited from QWidget, not QMainWindow), where are three widgets. Two custom QLineEdit widgets in separate *.py files and one QItemList in main widget.

    This is what I want to do:
    There are some text Items in ItemList and I want to click on first or second LineEdit and then click on the Items. Items that were clicked must be added to first or second LineEdit, depending on what was selected last.

    The problem is that I can't get information what LineEdit was selected last. I tried to use hasFocus() function, but it always return false, because it become false after I click on the Item. Also I tried to use mousePressEvent in custom LineEdit widget, this way I can track what LineEdit was clicked, but I can't use this info either, because I don't know how to send it to QListWidget in main window. Only solution that I found is to use *.json file and save there what LineEdit was clicked last and then use this info in method in main widget. But I'm sure there is a correct way to do it. I'd be glad for any help, thanks!



  • @To-RGB
    To achieve this, simplest for you is: define two slots for QLineEdit::clicked, in each one set some outside variable (probably a member variable of the containingQWidget) to the corresponding QLineEdit whose clicked signal you will attach to that slot. So now you know which one was last clicked. Use that for whatever you want to do in your item list.

    There are other ways of doing this --- including using a Python lambda to avoid having to write two separate slots --- but for just 2 this is the simplest as a newbie.



  • @JonB , thanks for the answer! If I understood it correctly, do you mean to use clicked() signal? I tried to find it in documentation, but unfortunately QLineEdit has not clicked() signal, that's why I created custom LineEdit and tried to do something with mousePressEvent.



  • @To-RGB
    You are right, I assumed it had a clicked signal :) You could use any of the suggestions from e.g. https://stackoverflow.com/questions/6452077/how-to-get-click-event-of-qlineedit-in-qt.

    Whatever you use --- whether subclassing, event filter, whatever --- the principle still remains that you will need to save up which line edit (e.g. if you subclass that would be self in the event) was "clicked" in some persistent variable, and use that later when user interacts with your QItemList. Can I leave it to you?



  • @JonB argh! I spend a whole day yesterday to figure out how to make it work, and all I needed to do is to write mySignal=Signal() not in the init, but in the class body! Jeeeez.
    Thanks a lot! Your advices and link helped!