Qt标准密钥在Python中停止工作

时间:2018-10-17 12:10:49

标签: python python-3.x pyqt

我正在使用编写程序

  • Linux Mint 19
  • Python版本:3.6.6
  • Qt版本:5.9.5
  • PyQt版本:5.10.1
  • SIP版本:4.19.7

,我刚刚注意到关闭标准短键(CTRL+W)已经停止工作。

在我的文件中,我写了以下一行将其连接到关闭按钮

self.closeBtn.setShortcut(QtGui.QKeySequence(QtGui.QKeySequence.Close))

但是,如果我按下按钮,则什么也不会发生。如果我将其更改为

self.closeBtn.setShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_W))

它按预期工作。 我也尝试过

self.closeBtn.setShortcut(QtGui.QKeySequence(QtGui.QKeySequence.Quit)) 

但是CTRL+Q也不执行任何操作。标准密钥可用于其他应用程序。 为什么会有任何想法?

2 个答案:

答案 0 :(得分:0)

import sys
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *

class demo_widget(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)
        lay_content = QVBoxLayout()
        self.closeBtn = QPushButton("Close")
        self.lineEdit = QLineEdit()

        self.closeBtn.clicked.connect(self.slt_close)
        self.closeAction = QAction(self, triggered=self.slt_close)
        self.closeAction.setShortcuts([QKeySequence("Ctrl+Q"), QKeySequence("Ctrl+W")])
        self.closeBtn.addAction(self.closeAction)

        lay_content.addWidget(self.closeBtn)
        lay_content.addWidget(self.lineEdit)
        self.setLayout(lay_content)

    def slt_close(self):
        self.lineEdit.setText("close")

if __name__ == '__main__':
    app=QApplication([])
    demo = demo_widget()
    demo.show()
    app.exec_()

答案 1 :(得分:0)

  

枚举QKeySequence :: StandardKey

     

此枚举代表标准键绑定。   它们可以用于将依赖于平台的键盘快捷键分配给QAction。

http://doc.qt.io/qt-5/qkeysequence.html#StandardKey-enum

尝试一下:

$file->getPathname

enter image description here