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 Spacebar press
Forum Updated to NodeBB v4.3 + New Features

PySide2 Spacebar press

Scheduled Pinned Locked Moved Unsolved Qt for Python
17 Posts 3 Posters 1.8k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Caeden
    wrote on last edited by
    #3

    @SGaist

    1. class MainWindow(QWidget):
    2. def keyPressEvent(self, event) -> None: inside of MainWindow (subclass)
    3. Linux
    4. 5.15.2

    Also, for some weird reason with the code im using Spacebar press is counted as a 7...? Doesn't trigger keyPressEvent at all though, I printed hi after and the print didnt trigger... curious

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Please show your code.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Caeden
        wrote on last edited by
        #5

        @SGaist It's very messy, so excuse the code

        import sys, os, time
        import simpleeval as simple
        
        from calc_ui import Ui_Dialog
        from functools import partial
        
        from PySide2.QtWidgets import *
        from PySide2 import *
        from PySide2.QtTest import QTest
        
        superscript = {
            '1': '¹',
            '2': '²',
            '3': '³',
            '4': '⁴',
            '5': '⁵',
            '6': '⁶',
            '7': '⁷',
            '8': '⁸', 
            '9': '⁹',
            '0': '⁰',
        }
        osuperscript = {
            '¹': '1',
            '²': '2',
            '³': '3',
            '⁴': '4',
            '⁵': '5',
            '⁶': '6',
            '⁷': '7',
            '⁸': '8', 
            '⁹': '9',
            '⁰': '0',
        }
        
        class MainWindow(QWidget):
            def __init__(self) -> None:
                """Okay, unnecessary as hell"""
        
                super().__init__()
                self.initUi()
                self.createVars()
                self.setWindowTitle("Calculator")
        
        
            def initUi(self) -> None:
                """Ui, why is there a docstring here?"""
        
                self.ui = Ui_Dialog()
                self.ui.setupUi(self) 
        
            def createVars(self) -> None:  
                """At least try and make it look neat"""
        
                self._X = 1
                self._Y = 5
                self.label = self.ui.label  
        
                for i in [  self.ui.pushButton_9, self.ui.pushButton_10, self.ui.pushButton_11, self.ui.pushButton_12, self.ui.pushButton_13, 
                            self.ui.pushButton_14, self.ui.pushButton_15, self.ui.pushButton_16, self.ui.pushButton_17,
                            self.ui.pushButton_19, self.ui.pushButton_20, self.ui.pushButton_21, self.ui.pushButton_22, self.ui.pushButton_24,
                            self.ui.pushButton_25, self.ui.pushButton_26, self.ui.pushButton_27, self.ui.pushButton_28  ]:
        
                    i.clicked.connect(partial(self.writeTo, i))
        
                self.ui.pushButton_23.clicked.connect(self.evaluate)
                self.ui.pushButton_30.clicked.connect(self.clear)
                self.ui.pushButton_18.clicked.connect(lambda: self.label.setText(self.label.text() + "'") if not self.label.text() == '0' else self.label.setText("'"))
        
            def keyPressEvent(self, event) -> None:
                """Key press"""
        
                label = self.label
                text = event.text().upper()
                key = event.key() 
                ltext = label.text().upper()
        
                if text.isalpha() and text not in ['X', 'Y']:
                    """Alphabetical"""
                    return
        
                if key in [96, 91, 93, 16777248, 35, 39, 59, 47, 44, 16781571, 92, 16777216]:
                    """Excess keys like `,./#; """
                    return 
        
                if key == 16777220:
                    """Enter clicked"""
                    return self.evaluate()
        
                if key == 16777219: 
                    """Backspace"""
                    return self.clear(label)
        
                if ltext == '0':
                    """Nothing currently in terminal"""
                    return label.setText(text) 
                
                if "'" in ltext[-1]:
                    return label.setText(ltext.replace("'", "") + superscript[text])
                
                if ltext[-1] in osuperscript:
                    return label.setText(ltext + superscript[text])
        
                """Add to label"""
                return label.setText(ltext + text)
        
            def evaluate(self) -> None:
                """Just returns expression, evaluated"""
        
                text = self.label.text()
                text = text.replace(' ', '')
                script = 0
        
                for iteration, character in enumerate(text):
                    if character in osuperscript:
                        print(iteration)
                        print(text[iteration-2:-1])
                        if text[iteration-2:-1]  == '**':
                            text = text.replace(character, osuperscript[character])
                            #script += 1
                            continue
        
                        if text[iteration-1] not in osuperscript:
                            text = text.replace(character, "**" + osuperscript[character])
                            #script += 1
                            continue
        
                        script += 1
                print('\n\n\n\n')
                print(text)
                print(script)
        
                expression = text
                try:
                    output = simple.simple_eval(expression, names={"X": self._X, "Y": self._Y})    
                    self.label.setText(str(output))
        
                except Exception:
                    self.label.setText('Error: Too high number output')
        
            def writeTo(self, func) -> None:
                """Because I am lazy and I like neat code"""
                self.label.setText(self.label.text() + func.text()) if not self.label.text() == '0' else self.label.setText(func.text())
        
            def clear(self, label) -> None:
                if len(label.text()) == 1:
                    """Add a 0"""
                    return label.setText('0')
                return label.setText(label.text()[:-1]) 
        
                    
        
        
        
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            win = MainWindow()
            win.show()
            app.exec_()
            sys.exit(0)
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Why not use the Qt::Key enum ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          1
          • SGaistS SGaist

            Why not use the Qt::Key enum ?

            C Offline
            C Offline
            Caeden
            wrote on last edited by
            #7

            @SGaist Hey, sorry, I've been trying to research how to use it but I don't completely understand - How would I use an enum in this case?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8
              if key in [Qt.Key_0, Qt.Key_1]:
                  print("smothering")
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply
              1
              • SGaistS SGaist
                if key in [Qt.Key_0, Qt.Key_1]:
                    print("smothering")
                
                C Offline
                C Offline
                Caeden
                wrote on last edited by
                #9

                @SGaist but where? I would need an event for this, seeing as the keyboardpress event doesnt trigger from it

                jsulmJ 1 Reply Last reply
                0
                • C Caeden

                  @SGaist but where? I would need an event for this, seeing as the keyboardpress event doesnt trigger from it

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @Caeden https://doc.qt.io/qt-5/qkeyevent.html#key

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  C 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Caeden https://doc.qt.io/qt-5/qkeyevent.html#key

                    C Offline
                    C Offline
                    Caeden
                    wrote on last edited by
                    #11

                    @jsulm When I print(dir(MainWindow)) there is no QEventKey

                    jsulmJ 1 Reply Last reply
                    0
                    • C Caeden

                      @jsulm When I print(dir(MainWindow)) there is no QEventKey

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @Caeden The event is right here: def keyPressEvent(self, event)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      C 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Caeden The event is right here: def keyPressEvent(self, event)

                        C Offline
                        C Offline
                        Caeden
                        wrote on last edited by
                        #13

                        @jsulm Yes, but the event isn't triggered when I press the spacebar.

                        def keyPressEvent(self, event) -> None:
                                """Key press"""
                        
                                print('hi')
                        

                        this does not print 'hi' when I press the spacebar. Spacebar is the key i'm after and it does not trigger this event

                        jsulmJ 1 Reply Last reply
                        0
                        • C Caeden

                          @jsulm Yes, but the event isn't triggered when I press the spacebar.

                          def keyPressEvent(self, event) -> None:
                                  """Key press"""
                          
                                  print('hi')
                          

                          this does not print 'hi' when I press the spacebar. Spacebar is the key i'm after and it does not trigger this event

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @Caeden One moment. In which exact widget do you want to enter space? Which widget has focus when you press space? MainWindow is a container widget containing other widgets, right?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          C 1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            Caeden
                            wrote on last edited by
                            #15

                            @jsulm I just want it to accept a spacebar press. Here is my ui code, its automatically generated so doesnt look amazingly neat
                            I believe that I have one QWidget on my main window, because on linux for some reason background-color: black; makes it appear black on Qt Designer, but shows as a white background. It worked fine on windows before, but anyway, thats irrelevant because I'm just using a widget instead

                            from PySide2.QtCore import *
                            from PySide2.QtGui import *
                            from PySide2.QtWidgets import *
                            
                            class Ui_Dialog(object):
                                def setupUi(self, Dialog):
                                    Dialog.resize(580, 607)
                                    Dialog.setStyleSheet(u"")
                                    self.widget = QWidget(Dialog)
                                    Dialog.setWindowTitle('Calculator')
                                    self.widget.setObjectName(u"widget")
                                    self.widget.setGeometry(QRect(0, 0, 581, 611))
                                    self.widget.setStyleSheet(u"background-color: black;")
                                    self.label = QLabel(self.widget)
                                    self.label.setObjectName(u"label")
                                    self.label.setMargin(10)
                                    self.label.setGeometry(QRect(9, 10, 561, 121))
                                    font = QFont()
                                    font.setPointSize(20)
                                    self.label.setFont(font)
                                    self.label.setStyleSheet(u"border: 2px solid rgb(166, 166, 166);\n"
                            "border-radius: 10px;\n"
                            "color: white;")
                                    self.pushButton_9 = QPushButton(self.widget)
                                    self.pushButton_9.setObjectName(u"pushButton_9")
                                    self.pushButton_9.setGeometry(QRect(20, 320, 111, 51))
                                    self.pushButton_9.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_9.setAutoDefault(False)
                                    self.pushButton_10 = QPushButton(self.widget)
                                    self.pushButton_10.setObjectName(u"pushButton_10")
                                    self.pushButton_10.setGeometry(QRect(160, 320, 111, 51))
                                    self.pushButton_10.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_10.setAutoDefault(False)
                                    self.pushButton_11 = QPushButton(self.widget)
                                    self.pushButton_11.setObjectName(u"pushButton_11")
                                    self.pushButton_11.setGeometry(QRect(300, 320, 111, 51))
                                    self.pushButton_11.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_11.setAutoDefault(False)
                                    self.pushButton_12 = QPushButton(self.widget)
                                    self.pushButton_12.setObjectName(u"pushButton_12")
                                    self.pushButton_12.setGeometry(QRect(300, 460, 111, 51))
                                    self.pushButton_12.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_12.setAutoDefault(False)
                                    self.pushButton_13 = QPushButton(self.widget)
                                    self.pushButton_13.setObjectName(u"pushButton_13")
                                    self.pushButton_13.setGeometry(QRect(160, 460, 111, 51))
                                    self.pushButton_13.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_13.setAutoDefault(False)
                                    self.pushButton_14 = QPushButton(self.widget)
                                    self.pushButton_14.setObjectName(u"pushButton_14")
                                    self.pushButton_14.setGeometry(QRect(20, 460, 111, 51))
                                    self.pushButton_14.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_14.setAutoDefault(False)
                                    self.pushButton_15 = QPushButton(self.widget)
                                    self.pushButton_15.setObjectName(u"pushButton_15")
                                    self.pushButton_15.setGeometry(QRect(300, 390, 111, 51))
                                    self.pushButton_15.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_15.setAutoDefault(False)
                                    self.pushButton_16 = QPushButton(self.widget)
                                    self.pushButton_16.setObjectName(u"pushButton_16")
                                    self.pushButton_16.setGeometry(QRect(160, 390, 111, 51))
                                    self.pushButton_16.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_16.setAutoDefault(False)
                                    self.pushButton_17 = QPushButton(self.widget)
                                    self.pushButton_17.setObjectName(u"pushButton_17")
                                    self.pushButton_17.setGeometry(QRect(20, 390, 111, 51))
                                    self.pushButton_17.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_17.setAutoDefault(False)
                                    self.pushButton_18 = QPushButton(self.widget)
                                    self.pushButton_18.setObjectName(u"pushButton_18")
                                    self.pushButton_18.setGeometry(QRect(300, 240, 111, 51))
                                    self.pushButton_18.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_18.setAutoDefault(False)
                                    self.pushButton_19 = QPushButton(self.widget)
                                    self.pushButton_19.setObjectName(u"pushButton_19")
                                    self.pushButton_19.setGeometry(QRect(160, 240, 111, 51))
                                    self.pushButton_19.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_19.setAutoDefault(False)
                                    self.pushButton_20 = QPushButton(self.widget)
                                    self.pushButton_20.setObjectName(u"pushButton_20")
                                    self.pushButton_20.setGeometry(QRect(20, 240, 111, 51))
                                    self.pushButton_20.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_20.setAutoDefault(False)
                                    self.pushButton_21 = QPushButton(self.widget)
                                    self.pushButton_21.setObjectName(u"pushButton_21")
                                    self.pushButton_21.setGeometry(QRect(20, 540, 111, 51))
                                    self.pushButton_21.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_21.setAutoDefault(False)
                                    self.pushButton_22 = QPushButton(self.widget)
                                    self.pushButton_22.setObjectName(u"pushButton_22")
                                    self.pushButton_22.setGeometry(QRect(160, 540, 111, 51))
                                    self.pushButton_22.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_22.setAutoDefault(False)
                                    self.pushButton_23 = QPushButton(self.widget)
                                    self.pushButton_23.setObjectName(u"pushButton_23")
                                    self.pushButton_23.setGeometry(QRect(300, 540, 111, 51))
                                    self.pushButton_23.setStyleSheet(u"background-color: black;\n"
                            "color: white;\n"
                            "border:  4px solid rgb(112, 112, 112);\n"
                            "border-radius: 4px;")
                                    self.pushButton_23.setAutoDefault(False)
                                    self.pushButton_24 = QPushButton(self.widget)
                                    self.pushButton_24.setObjectName(u"pushButton_24")
                                    self.pushButton_24.setGeometry(QRect(450, 240, 111, 51))
                                    self.pushButton_24.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_24.setAutoDefault(False)
                                    self.pushButton_25 = QPushButton(self.widget)
                                    self.pushButton_25.setObjectName(u"pushButton_25")
                                    self.pushButton_25.setGeometry(QRect(450, 320, 111, 51))
                                    self.pushButton_25.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_25.setAutoDefault(False)
                                    self.pushButton_26 = QPushButton(self.widget)
                                    self.pushButton_26.setObjectName(u"pushButton_26")
                                    self.pushButton_26.setGeometry(QRect(450, 390, 111, 51))
                                    self.pushButton_26.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_26.setAutoDefault(False)
                                    self.pushButton_27 = QPushButton(self.widget)
                                    self.pushButton_27.setObjectName(u"pushButton_27")
                                    self.pushButton_27.setGeometry(QRect(450, 460, 111, 51))
                                    self.pushButton_27.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_27.setAutoDefault(False)
                                    self.pushButton_28 = QPushButton(self.widget)
                                    self.pushButton_28.setObjectName(u"pushButton_28")
                                    self.pushButton_28.setGeometry(QRect(450, 540, 111, 51))
                                    self.pushButton_28.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_28.setAutoDefault(False)
                                    self.pushButton_29 = QPushButton(self.widget)
                                    self.pushButton_29.setObjectName(u"pushButton_29")
                                    self.pushButton_29.setGeometry(QRect(450, 160, 111, 51))
                                    self.pushButton_29.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_29.setAutoDefault(False)
                                    self.pushButton_30 = QPushButton(self.widget)
                                    self.pushButton_30.setObjectName(u"pushButton_30")
                                    self.pushButton_30.setGeometry(QRect(300, 160, 111, 51))
                                    self.pushButton_30.setStyleSheet(u"""
                            
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            
                            """)
                                    self.pushButton_30.setAutoDefault(False)
                                    self.pushButton_31 = QPushButton(self.widget)
                                    self.pushButton_31.setObjectName(u"pushButton_31")
                                    self.pushButton_31.setGeometry(QRect(160, 160, 111, 51))
                                    self.pushButton_31.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_31.setAutoDefault(False)
                                    self.pushButton_32 = QPushButton(self.widget)
                                    self.pushButton_32.setObjectName(u"pushButton_32")
                                    self.pushButton_32.setGeometry(QRect(20, 160, 111, 51))
                                    self.pushButton_32.setStyleSheet(u"""
                            background-color: black;
                            color: white;
                            border:  2px solid rgb(166, 166, 166);
                            border-radius: 4px;
                            
                            """)
                                    self.pushButton_32.setAutoDefault(False)
                            
                                    self.retranslateUi(Dialog)
                            
                                    QMetaObject.connectSlotsByName(Dialog)
                                # setupUi
                            
                                def retranslateUi(self, Dialog):
                                    Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
                                    self.label.setText(QCoreApplication.translate("Dialog", u"0", None))
                                    self.pushButton_9.setText(QCoreApplication.translate("Dialog", u"7", None))
                                    self.pushButton_10.setText(QCoreApplication.translate("Dialog", u"8", None))
                                    self.pushButton_11.setText(QCoreApplication.translate("Dialog", u"9", None))
                                    self.pushButton_12.setText(QCoreApplication.translate("Dialog", u"3", None))
                                    self.pushButton_13.setText(QCoreApplication.translate("Dialog", u"2", None))
                                    self.pushButton_14.setText(QCoreApplication.translate("Dialog", u"1", None))
                                    self.pushButton_15.setText(QCoreApplication.translate("Dialog", u"6", None))
                                    self.pushButton_16.setText(QCoreApplication.translate("Dialog", u"5", None))
                                    self.pushButton_17.setText(QCoreApplication.translate("Dialog", u"4", None))
                                    self.pushButton_18.setText(QCoreApplication.translate("Dialog", u"\u00b2", None))
                                    self.pushButton_19.setText(QCoreApplication.translate("Dialog", u")", None))
                                    self.pushButton_20.setText(QCoreApplication.translate("Dialog", u"(", None))
                                    self.pushButton_21.setText(QCoreApplication.translate("Dialog", u"0", None))
                                    self.pushButton_22.setText(QCoreApplication.translate("Dialog", u".", None))
                                    self.pushButton_23.setText(QCoreApplication.translate("Dialog", u"=", None))
                                    self.pushButton_24.setText(QCoreApplication.translate("Dialog", u"\u221a", None))
                                    self.pushButton_25.setText(QCoreApplication.translate("Dialog", u"-", None))
                                    self.pushButton_26.setText(QCoreApplication.translate("Dialog", u"/", None))
                                    self.pushButton_27.setText(QCoreApplication.translate("Dialog", u"*", None))
                                    self.pushButton_28.setText(QCoreApplication.translate("Dialog", u"+", None))
                                    self.pushButton_29.setText(QCoreApplication.translate("Dialog", u"AC", None))
                                    self.pushButton_30.setText(QCoreApplication.translate("Dialog", u"DEL", None))
                                    self.pushButton_31.setText(QCoreApplication.translate("Dialog", u"X", None))
                                    self.pushButton_32.setText(QCoreApplication.translate("Dialog", u"Y", None))
                                # retranslateUi
                            
                            
                            
                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Caeden One moment. In which exact widget do you want to enter space? Which widget has focus when you press space? MainWindow is a container widget containing other widgets, right?

                              C Offline
                              C Offline
                              Caeden
                              wrote on last edited by
                              #16

                              @jsulm When I do

                               def event(self, _event):
                                      print('hi, {}, {}'.format(_event, self.num))
                                      self.num += 1
                              

                              and press spacebar it seems that when I press spacebar it counts it as <PySide2.QtCore.QEvent object at 0x7f600b1b9b40>. How do I turn this into an event function?

                              jsulmJ 1 Reply Last reply
                              0
                              • C Caeden

                                @jsulm When I do

                                 def event(self, _event):
                                        print('hi, {}, {}'.format(_event, self.num))
                                        self.num += 1
                                

                                and press spacebar it seems that when I press spacebar it counts it as <PySide2.QtCore.QEvent object at 0x7f600b1b9b40>. How do I turn this into an event function?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                @Caeden said in PySide2 Spacebar press:

                                event function?

                                Event is not a function it is an object.

                                _event.key()
                                

                                should do.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                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