PyQt5 QCommandLinkButton – 为选中和按下的组合状态设置边框

PyQt5 QCommandLinkButton – 为选中和按下的组合状态设置边框

在这篇文章中,我们将看到如何根据选中和按下的状态,为可检查的QCommandLinkButton设置边框。命令链接按钮是一个特殊类型的按钮,它同时具有按钮和单选按钮的特性。默认情况下,命令链接按钮没有额外的边框,尽管当它被按下时我们可以看到边框,我们也可以给它设置自定义的边框。

我们可以在setCheckable方法的帮助下使命令链接按钮成为可检查的,基本上有两种检查状态,一种是检查状态,即当它处于被按下的状态,另一种是未检查状态,即当它处于释放状态。

为了做到这一点,我们将为它设置样式表代码,为此我们使用命令链接按钮对象的setStyleSheet方法,下面是样式表代码

QCommandLinkButton::checked
{
border : 4px solid green;
}
QCommandLinkButton::checked::pressed
{
border : 4px solid yellow;
}
QCommandLinkButton::!checked
{
border : 2px solid red;
}
QCommandLinkButton::checked::pressed
{
border : 2px solid pink;
}

以下是实施情况

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 500, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for components
    def UiComponents(self):
  
        # creating a command link button
        cl_button = QCommandLinkButton("Press", self)
  
        # making it chekable
        cl_button.setCheckable(True)
  
        # setting geometry
        cl_button.setGeometry(150, 100, 200, 50)
  
        # setting style sheet
        # setting border to it for checked and unchecked state
        # setting border when pressed in checked and unchecked state
        cl_button.setStyleSheet("QCommandLinkButton::! checked"
                                "{"
                                "border : 2px solid red;"
                                "}"
                                "QCommandLinkButton::checked"
                                "{"
                                "border : 4px solid green;"
                                "}"
                                "QCommandLinkButton::checked::pressed"
                                "{"
                                "border : 4px solid yellow;"
                                "}"
                                "QCommandLinkButton::! checked::pressed"
                                "{"
                                "border : 2px solid pink;"
                                "}"
                                )
  
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

PyQt5 命令连接按钮控件QCommandLinkButton