QMenu.item.selected 样式表不工作
QMenu.item.selected stylesheet is not working
我试图让 qmenu 中的项目在悬停时变为灰色,就像我应用的融合样式表一样,它们在悬停时变为白色。不幸的是,我的样式表没有得到应用。
我搜索了一下,发现我应该将 "hover" 设置为 "selected",我这样做了,但没有任何变化。
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QMessageBox, QRadioButton, QMainWindow, QLabel, QListWidget, QListWidgetItem,
QDesktopWidget, QCheckBox, QPlainTextEdit, QHBoxLayout, QVBoxLayout, QGridLayout, QStackedWidget, QFormLayout, QMenu,
QComboBox, QScrollArea, QLineEdit, QGroupBox, QListView, QToolTip, QFileDialog, QTabWidget, QAction, QInputDialog)
from PyQt5.QtGui import QIcon, QFont, QRegExpValidator, QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtCore import Qt, QRegExp, QModelIndex
class WindowGUI(QMainWindow):
def __init__(self, gui):
super().__init__()
self.initUI(gui)
def initUI(self, gui): #Initializing basic GUI
self.setGeometry(QDesktopWidget().screenGeometry())
self.showMaximized()
self.setWindowTitle("ScheduleO")
self.setWindowIcon(QIcon("icons/schedule.png"));
self.menu = self.menuBar()
self.SCHMenu = self.menu.addMenu("Schedule")
self.SCHFormat = QAction("New Schedule Format", self)
self.SCHApply = QAction("Apply Schedule Format...", self)
#This is where the issue is
self.menu.setStyleSheet("font: 12pt; background-color: white; QMenu.item.selected {color: gray}")
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
userGUI = UserGUI() #It is not shown, but I have it in my code
windowGUI = WindowGUI(userGUI)
windowGUI.show()
sys.exit(app.exec_())
此外,当我尝试时
self.menu.setStyleSheet("QMenu {font: 12pt; background-color: white;}; QMenu.item.selected {color: gray}")
未应用整个样式表
我的 QMenu 选择器有问题吗?
试一试:
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QMessageBox, QRadioButton, QMainWindow, QLabel, QListWidget, QListWidgetItem,
QDesktopWidget, QCheckBox, QPlainTextEdit, QHBoxLayout, QVBoxLayout, QGridLayout, QStackedWidget, QFormLayout, QMenu,
QComboBox, QScrollArea, QLineEdit, QGroupBox, QListView, QToolTip, QFileDialog, QTabWidget, QAction, QInputDialog)
from PyQt5.QtGui import QIcon, QFont, QRegExpValidator, QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtCore import Qt, QRegExp, QModelIndex
class WindowGUI(QMainWindow):
def __init__(self): #, gui):
super().__init__()
self.initUI() #(gui)
def initUI(self): #, gui): #Initializing basic GUI
self.setGeometry(QDesktopWidget().screenGeometry())
self.showMaximized()
self.setWindowTitle("ScheduleO")
self.setWindowIcon(QIcon("E:/_Qt/img/qt-logo.png")) #("icons/schedule.png"));
self.menu = self.menuBar()
self.SCHMenu = self.menu.addMenu("Schedule")
self.SCHFormat = QAction("New Schedule Format", self)
self.SCHApply = QAction("Apply Schedule Format...", self)
self.SCHApply.setData('option2') #@
self.SCHMenu.addAction(self.SCHFormat)
self.SCHMenu.addAction(self.SCHApply)
#This is where the issue is
# self.menu.setStyleSheet("font: 12pt; background-color: white; QMenu.item.selected {color: gray}")
# self.menu.setStyleSheet("QMenu {font: 12pt; background-color: white;}; QMenu.item.selected {color: gray}")
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
app.setStyleSheet("""
QMenu
{
font: 12pt;
background-color: white;
}
QMenu::item:selected
{
color: gray;
}
""")
# userGUI = UserGUI()
windowGUI = WindowGUI() # (userGUI)
windowGUI.show()
sys.exit(app.exec_())
包含格式的Python字符串被直接发送到C++后端。这意味着您必须使用 ::(范围解析)和 :(初始化列表)运算符。有关示例,请参阅文档:
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qmenu
还需要注意 Qt 范围,例如应用样式表的位置和类型。我想也许你想要的是这样的东西?选择字体大小和颜色以显示明显的差异。
self.menu.setStyleSheet(
"""
QMenu
{
font: 18pt;
background-color: purple;
}
QMenu::item:selected
{
color: green
}
"""
)
完整示例如下所示。
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDesktopWidget)
from PyQt5.QtGui import QIcon
class WindowGUI(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setGeometry(QDesktopWidget().screenGeometry())
self.showMaximized()
self.setWindowTitle("ScheduleO")
self.setWindowIcon(QIcon("icons/schedule.png"));
self.menu = self.menuBar()
self.SCHMenu = self.menu.addMenu("Schedule")
self.SCHMenu.addAction("New Schedule Format")
self.SCHMenu.addAction("Apply Schedule Format...")
# Issue should be fixed.
self.menu.setStyleSheet(
"""
QMenu
{
font: 18pt;
background-color: purple;
}
QMenu::item:selected
{
background-color: green
}
"""
)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
windowGUI = WindowGUI()
windowGUI.show()
sys.exit(app.exec_())
可以在此处找到示例图片 https://i.stack.imgur.com/dCDId.png(没有足够的声誉 post 图片。
我试图让 qmenu 中的项目在悬停时变为灰色,就像我应用的融合样式表一样,它们在悬停时变为白色。不幸的是,我的样式表没有得到应用。 我搜索了一下,发现我应该将 "hover" 设置为 "selected",我这样做了,但没有任何变化。
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QMessageBox, QRadioButton, QMainWindow, QLabel, QListWidget, QListWidgetItem,
QDesktopWidget, QCheckBox, QPlainTextEdit, QHBoxLayout, QVBoxLayout, QGridLayout, QStackedWidget, QFormLayout, QMenu,
QComboBox, QScrollArea, QLineEdit, QGroupBox, QListView, QToolTip, QFileDialog, QTabWidget, QAction, QInputDialog)
from PyQt5.QtGui import QIcon, QFont, QRegExpValidator, QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtCore import Qt, QRegExp, QModelIndex
class WindowGUI(QMainWindow):
def __init__(self, gui):
super().__init__()
self.initUI(gui)
def initUI(self, gui): #Initializing basic GUI
self.setGeometry(QDesktopWidget().screenGeometry())
self.showMaximized()
self.setWindowTitle("ScheduleO")
self.setWindowIcon(QIcon("icons/schedule.png"));
self.menu = self.menuBar()
self.SCHMenu = self.menu.addMenu("Schedule")
self.SCHFormat = QAction("New Schedule Format", self)
self.SCHApply = QAction("Apply Schedule Format...", self)
#This is where the issue is
self.menu.setStyleSheet("font: 12pt; background-color: white; QMenu.item.selected {color: gray}")
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
userGUI = UserGUI() #It is not shown, but I have it in my code
windowGUI = WindowGUI(userGUI)
windowGUI.show()
sys.exit(app.exec_())
此外,当我尝试时
self.menu.setStyleSheet("QMenu {font: 12pt; background-color: white;}; QMenu.item.selected {color: gray}")
未应用整个样式表
我的 QMenu 选择器有问题吗?
试一试:
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QMessageBox, QRadioButton, QMainWindow, QLabel, QListWidget, QListWidgetItem,
QDesktopWidget, QCheckBox, QPlainTextEdit, QHBoxLayout, QVBoxLayout, QGridLayout, QStackedWidget, QFormLayout, QMenu,
QComboBox, QScrollArea, QLineEdit, QGroupBox, QListView, QToolTip, QFileDialog, QTabWidget, QAction, QInputDialog)
from PyQt5.QtGui import QIcon, QFont, QRegExpValidator, QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtCore import Qt, QRegExp, QModelIndex
class WindowGUI(QMainWindow):
def __init__(self): #, gui):
super().__init__()
self.initUI() #(gui)
def initUI(self): #, gui): #Initializing basic GUI
self.setGeometry(QDesktopWidget().screenGeometry())
self.showMaximized()
self.setWindowTitle("ScheduleO")
self.setWindowIcon(QIcon("E:/_Qt/img/qt-logo.png")) #("icons/schedule.png"));
self.menu = self.menuBar()
self.SCHMenu = self.menu.addMenu("Schedule")
self.SCHFormat = QAction("New Schedule Format", self)
self.SCHApply = QAction("Apply Schedule Format...", self)
self.SCHApply.setData('option2') #@
self.SCHMenu.addAction(self.SCHFormat)
self.SCHMenu.addAction(self.SCHApply)
#This is where the issue is
# self.menu.setStyleSheet("font: 12pt; background-color: white; QMenu.item.selected {color: gray}")
# self.menu.setStyleSheet("QMenu {font: 12pt; background-color: white;}; QMenu.item.selected {color: gray}")
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
app.setStyleSheet("""
QMenu
{
font: 12pt;
background-color: white;
}
QMenu::item:selected
{
color: gray;
}
""")
# userGUI = UserGUI()
windowGUI = WindowGUI() # (userGUI)
windowGUI.show()
sys.exit(app.exec_())
包含格式的Python字符串被直接发送到C++后端。这意味着您必须使用 ::(范围解析)和 :(初始化列表)运算符。有关示例,请参阅文档:
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qmenu
还需要注意 Qt 范围,例如应用样式表的位置和类型。我想也许你想要的是这样的东西?选择字体大小和颜色以显示明显的差异。
self.menu.setStyleSheet(
"""
QMenu
{
font: 18pt;
background-color: purple;
}
QMenu::item:selected
{
color: green
}
"""
)
完整示例如下所示。
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDesktopWidget)
from PyQt5.QtGui import QIcon
class WindowGUI(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setGeometry(QDesktopWidget().screenGeometry())
self.showMaximized()
self.setWindowTitle("ScheduleO")
self.setWindowIcon(QIcon("icons/schedule.png"));
self.menu = self.menuBar()
self.SCHMenu = self.menu.addMenu("Schedule")
self.SCHMenu.addAction("New Schedule Format")
self.SCHMenu.addAction("Apply Schedule Format...")
# Issue should be fixed.
self.menu.setStyleSheet(
"""
QMenu
{
font: 18pt;
background-color: purple;
}
QMenu::item:selected
{
background-color: green
}
"""
)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
windowGUI = WindowGUI()
windowGUI.show()
sys.exit(app.exec_())
可以在此处找到示例图片 https://i.stack.imgur.com/dCDId.png(没有足够的声誉 post 图片。