Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [PySide2] How do I send backspace as a QKeyEvent?
Forum Updated to NodeBB v4.3 + New Features

[PySide2] How do I send backspace as a QKeyEvent?

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lolcocks
    wrote on last edited by lolcocks
    #1

    Hello,

    I have created a virtual keyboard in my application using PySide2.

    And I am using the QKeyEvent to pass the key that is being pressed using the following code:

            button = self.sender()
            widget = QApplication.focusWidget()
            
            if (button.text() == "\u232b"):
                widget.backspace()
            elif (button.text() == "Space"):
                text = " "
            elif (button.text() == "&&"):
                text = "&"
            else:
                text = button.text()
            
            
            if(button.text() != "\u232b"):
                key = button.property("_key_")
                
                if hasattr(key, "toPyObject"):
                    key = key.toPyObject()
                
                if widget:
                    event = QtGui.QKeyEvent(
                        QtCore.QEvent.KeyPress, key, QtCore.Qt.NoModifier, text
                    )
                    QtCore.QCoreApplication.postEvent(widget, event)
    

    My question is that how can I pass the backspace key?
    I have a backspace button which runs the function of .backspace() which works on LineEdits but is limited to that. Other widgets do not have this option, such as the dateEdit widget.

    Any suggestions?

    1 Reply Last reply
    0
    • eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @lolcocks mmm, Your code is unclear so it would be great if you provide a Minimum Reproducible Example (MRE), on the other hand you must send key = QtCore.Qt.Key_Backspace

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      L 1 Reply Last reply
      1
      • eyllanescE eyllanesc

        @lolcocks mmm, Your code is unclear so it would be great if you provide a Minimum Reproducible Example (MRE), on the other hand you must send key = QtCore.Qt.Key_Backspace

        L Offline
        L Offline
        lolcocks
        wrote on last edited by
        #3

        @eyllanesc said in [PySide2] How do I send backspace as a QKeyEvent?:

        @lolcocks mmm, Your code is unclear so it would be great if you provide a Minimum Reproducible Example (MRE), on the other hand you must send key = QtCore.Qt.Key_Backspace

        @Denni-0 said in [PySide2] How do I send backspace as a QKeyEvent?:

        @lolcocks sending QtCore.Qt.Key_Backspace simply equates to what you are doing using the actual key-codes so it is not a must use -- now grant you these key-codes have already been enumerated for you via this method but it all kind of depends on why you are doing what you are doing as to what the best solution would be for your program so use the option you feel is best.

        Thank you both of you.
        I am going to go with eyllanesc's solution as it works the best for me. You know, because it sends an actual backspace key.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved