python - Qt 样式表破坏了 PyQt5 应用程序的功能
问题描述
QPushButton 点击事件在使用样式表时不会被触发,但在不使用样式表时会被触发。例如,我可以创建一个 QPushButton 并定义一个在单击按钮时要调用的函数,如下所示:
self.scrape_btn = QPushButton('Scrape', self)
self.scrape_btn.clicked.connect(self.scrape_btn_callback)
def scrape_btn_callback(self):
print("Scrape button has been clicked!")
然后我可以如下设置样式表:
# Set the stylesheet
base_path = QDir.currentPath()
relative_path = "/findface/components/ui/gui/css/style.qss"
full_path = base_path + relative_path
file = QFile(full_path)
file.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(file)
app.setStyleSheet(stream.readAll())
设置样式表时不调用scrape_btn_callback,但在我注释掉时调用:
app.setStyleSheet(stream.readAll())
我正在使用这个样式表:https ://raw.githubusercontent.com/Alexhuszagh/BreezeStyleSheets/master/dark.qss但我尝试了其他一些样式表并且无论使用哪种样式表都遇到了同样的问题。
我在 macOS Catalina 上使用所有最新更新。我的python版本是3.7.7。PyQt5 版本是 5.15.0。感谢所有帮助。
要重现此问题:
图形用户界面.py:
import sys
from .main_window import MainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QDir, QFile, QTextStream
class GraphicalUserInterface:
def display(self):
app = QApplication(sys.argv)
# Set the stylesheet
base_path = QDir.currentPath()
relative_path = "/findface/components/ui/gui/css/style.qss"
full_path = base_path + relative_path
file = QFile(full_path)
file.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(file)
app.setStyleSheet(stream.readAll())
window = MainWindow()
window.initialize()
window.showMaximized()
return app.exec_()
主窗口.py
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QPushButton, QVBoxLayout, QLabel, QTabWidget, QHBoxLayout, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
def initialize(self):
# Create sidebar buttons
self.scrape_btn = QPushButton('Scrape', self)
self.search_btn = QPushButton('Search', self)
self.settings_btn = QPushButton('Settings', self)
#
self.scrape_btn.clicked.connect(self.scrape_btn_callback)
self.search_btn.clicked.connect(self.search_btn_callback)
self.settings_btn.clicked.connect(self.settings_btn_callback)
self.tab1 = self.scrape_tab()
self.tab2 = self.search_tab()
self.tab3 = self.settings_tab()
left_layout = QVBoxLayout()
left_layout.addWidget(self.scrape_btn)
left_layout.addWidget(self.search_btn)
left_layout.addWidget(self.settings_btn)
left_layout.addStretch(5)
left_layout.setSpacing(20)
left_widget = QWidget()
left_widget.setLayout(left_layout)
self.right_widget = QTabWidget()
self.right_widget.tabBar().setObjectName("mainTab")
self.right_widget.addTab(self.tab1, '')
self.right_widget.addTab(self.tab2, '')
self.right_widget.addTab(self.tab3, '')
self.right_widget.setCurrentIndex(0)
self.right_widget.setStyleSheet('''QTabBar::tab{width: 0; \
height: 0; margin: 0; padding: 0; border: none;}''')
main_layout = QHBoxLayout()
main_layout.addWidget(left_widget)
main_layout.addWidget(self.right_widget)
main_layout.setStretch(0, 40)
main_layout.setStretch(1, 200)
main_widget = QWidget()
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
self.setWindowTitle("FindFace")
def scrape_btn_callback(self):
self.right_widget.setCurrentIndex(0)
def search_btn_callback(self):
self.right_widget.setCurrentIndex(1)
def settings_btn_callback(self):
self.right_widget.setCurrentIndex(2)
def scrape_tab(self):
main_layout = QVBoxLayout()
main_layout.addWidget(QLabel('Scrape tab'))
main_layout.addStretch(5)
main = QWidget()
main.setLayout(main_layout)
return main
def search_tab(self):
main_layout = QVBoxLayout()
main_layout.addWidget(QLabel('Search tab'))
main_layout.addStretch(5)
main = QWidget()
main.setLayout(main_layout)
return main
def settings_tab(self):
main_layout = QVBoxLayout()
main_layout.addWidget(QLabel('Settings tab'))
main_layout.addStretch(5)
main = QWidget()
main.setLayout(main_layout)
return main
https://raw.githubusercontent.com/Alexhuszagh/BreezeStyleSheets/master/dark.qss - style.qss
修改 graphics_user_interface.py 中的 relative_path 以适应您的环境。创建 GraphicalUserInterface 类的实例并调用 display() 方法。在 graphics_user_interface.py 中注释掉“app.setStyleSheet(stream.readAll())”以取消设置样式表。
解决方案
推荐阅读
- loops - 具有 DB2 LUW 中数据的范围分区表
- json - 使用 JSON 值创建 Terraform 资源
- node.js - 无法杀死 ec2 实例上的节点进程
- sorting - 用 MiniZinc 求解并显示最短路径问题中的有序边
- flutter - 用 JSON 填充对象会导致颤动
- java - 为什么在 Java ArrayLists 中允许整数?
- django - 向 django 路由器方法添加自定义功能
- ios - Swift dateFormat 返回错误的时间
- python - 如何使用 Python 从 PowerPoint 演示文稿幻灯片中提取评论
- android - 在android中更改允许相机使用权限消息