我在用pyqt写UI的时候遇到一个需求,要在线上显示一个箭头。如图所示。
先需要使用 border-image 属性来设置箭头图像:
QLabel#arrowLabel {
border-image: url(arrow.png) 0 0 0 0 stretch stretch;
}
然后可以使用 transform 属性来旋转箭头。例如要将箭头旋转 45 度,可以这样写:
QLabel#arrowLabel {
border-image: url(arrow.png) 0 0 0 0 stretch stretch;
transform: rotate(45deg);
}
使用 origin 属性来调整箭头的旋转中心。例如要将箭头的旋转中心设置为其底部中心,可以这样写:
QLabel#arrowLabel {
border-image: url(arrow.png) 0 0 0 0 stretch stretch;
transform: rotate(45deg);
transform-origin: center bottom;
}
注意这些属性仅适用于 PyQt 5.6 及更高版本。
仅供参考,望采纳,谢谢。
使用 QSS (Qt Style Sheets) 是可以实现在 QLabel 或 QWidget 中显示箭头的需求的。
你可以使用 border-image 属性在 QLabel 或 QWidget 中显示箭头。
这里是一个简单的例子:
QLabel {
border-image: url(arrow.png) 0 0 0 0 stretch stretch;
}
在这个例子中,arrow.png 是一个包含箭头图像的文件,它将会被拉伸以填满整个 QLabel 或 QWidget。
你也可以使用 border-top, border-right, border-bottom, border-left 属性单独指定每个边框的图像,从而在某些边框中显示箭头,而在其他边框中显示其他图像或颜色。
使用QSS (Qt Style Sheets) 的 QLabel 或 QWidget的 border-image 属性就可以啊
PyQT5 (九十五)QSS子控件选择器 案例
借鉴下
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
QLabel, QGridLayout, QLineEdit, QTextEdit, QFormLayout, QComboBox
'''
PyQt5 控件样式表 QSS子控件选择器 案例
使用QSS选择器设置QComboBox控件基本样式和下拉箭头样式
'''
class QSSSubControlDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 360, 260)
# 设置窗口标题
self.setWindowTitle('QSS样式 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
cbo = QComboBox(self)
cbo.setObjectName("myCbo")
cbo.addItems(["windows","Linux","Mac OS X"])
cbo.move(50,50)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = QSSSubControlDemo()
# 使用选择器
# 设置cbo样式
# 用图片替换下拉箭头
qssStyle = '''
QComboBox#myCbo {
background-color:blue;
color:yellow;
height:60;
font-size:40px;
}
QComboBox#myCbo::drop-down{
image:url(../web.ico)
}
'''
w.setStyleSheet(qssStyle)
w.show()
sys.exit(app.exec_())
要在 PyQt 程序中使用 QSS 显示箭头,你可以使用以下代码:
from PyQt5.QtWidgets import QApplication, QWidget
app = QApplication([])
window = QWidget()
# 定义 QSS 样式
style = """
QWidget {
border: 1px solid red;
}
QWidget::down-arrow {
image: url(down_arrow.png);
}
"""
# 应用 QSS 样式
app.setStyleSheet(style)
window.show()
app.exec_()