我试图将鼠标悬停在灰色菜单上,使其变为灰色,就像我应用的融合样式表一样,鼠标悬停时它们变为白色。不幸的是,我的样式表没有被应用。 经过搜索,我发现应该将“悬停”设置为“选定”状态,但是没有更改。
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选择器有问题吗?
答案 0 :(得分:0)
尝试一下:
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_())
答案 1 :(得分:0)
包含格式的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找到示例图片(没有足够的声誉来发布图片。