PyQt5 QCommandLinkButton – 按下的信号

  • Post category:Python

PyQt5是Python的一个GUI库。QCommandLinkButton是Qt中的一个重要组件,它是一个与QPushButton类似的按钮,但具有更适合于指南或操作提示的样式。当用户按下QCommandLinkButton时,它会发出一个信号,可以通过连接到此信号的槽来处理此事件。下面是关于如何使用“PyQt5 QCommandLinkButton-按下的信号”的完整攻略。

安装PyQt5库

首先需要确保安装了PyQt5库。安装方法如下:

pip install PyQt5

创建QCommandLinkButton

下面的示例演示如何创建一个QCommandLinkButton:

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QGridLayout, QPushButton, QCommandLinkButton

class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        self.setWindowTitle('QCommandLinkButton Example')

        layout = QGridLayout()

        #创建QCommandLinkButton 
        commandLinkButton = QCommandLinkButton('Submit', self)
        commandLinkButton.clicked.connect(self.onClicked)
        layout.addWidget(commandLinkButton, 0, 0)

        self.setLayout(layout)

    def onClicked(self):
        print('The "Submit" button was clicked.')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Dialog()
    dialog.show()
    sys.exit(app.exec_()) 

按下信号的使用

下面的示例演示了如何使用QCommandLinkButton的按下信号实现按钮按下操作的响应函数:

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QGridLayout, QCommandLinkButton

class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        self.setWindowTitle('QCommandLinkButton Example')

        layout = QGridLayout()

        #创建QCommandLinkButton
        commandLinkButton = QCommandLinkButton('Submit', self)
        commandLinkButton.clicked.connect(self.onClicked)
        layout.addWidget(commandLinkButton, 0, 0)

        self.setLayout(layout)

    def onClicked(self):
        print('The "Submit" button was clicked.')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Dialog()
    dialog.show()
    sys.exit(app.exec_())

在上面的示例中,创建一个QCommandLinkButton实例,并将其clicked()信号连接到onClicked()函数。当用户单击按钮时,PyQt5将自动调用onClicked()函数,并执行其中的代码。