获取 QKeySequenceEdit 值

Get QKeySequenceEdit value

获取“QKeySequenceEdit”的值时遇到问题,我需要获取此值才能将其打印在标签和控制台上。

这是我的代码:

  ##Create my KeySecuenceEdit
        self.ksec_hotkey = QtWidgets.QKeySequenceEdit(self.gpb_main)##it is inside of a groupbox
        self.ksec_hotkey.setGeometry(QtCore.QRect(10, 75, 85, 27))
        self.ksec_hotkey.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.ksec_hotkey.setObjectName("ksec_hotkey")
        self.ksec_hotkey.keySequenceChanged.connect(self.show_ksec)##Call my function here

    ##function to get and print keySequenceEdit value
    def show_ksec(self):    
        ksec_value = self.ksec_hotkey.keySequence()
        #print(str(ksec_value))   ##does not work
        #print(ksec_value.__dict__)   ##.__dict__  or .__repr__ does not work
        print(ksec_value)   ##prints "<PyQt5.QtGui.QKeySequence object at 0x7f28386a5dd0>"
        self.lbl_print_hotkey.setText(" : " + str(ksec_value))   ##prints "<PyQt5.QtGui.QKeySequence object at 0x7f28386a5dd0>"

我的代码打印:“”。

我阅读了有关 类 的问题,所以这是我目前的程序:

class Ui_MainWindow(object):

    ##here is my function to print
    def show_ksec(self):


    ##here is where my qkeysequence and all my widgets are
    def setupUi(self, MainWindow):

你必须使用QKeySequencetoString()方法:

keysequence = self.ksec_hotkey.keySequence()
text = keysequence.toString(QKeySequence.NativeText)
# or
# text = keysequence.toString(QKeySequence.PortableText)
self.lbl_print_hotkey.setText(f" : {text}")