【问题标题】:QMenu.item.selected stylesheet is not workingQMenu.item.selected 样式表不起作用
【发布时间】:2018-10-26 12:34:10
【问题描述】:

我试图让 qmenu 中的项目在悬停时变为灰色,就像我应用的融合样式表一样,它们在悬停时变为白色。不幸的是,我的样式表没有得到应用。 我搜索了,我发现我应该将“悬停”设置为“选中”,我这样做了,但没有任何变化。

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 选择器有问题吗?

【问题讨论】:

    标签: python pyqt hover selected qmenu


    【解决方案1】:

    试试看:

    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_())
    

    【解决方案2】:

    包含格式的 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(没有足够的声誉来发布图片。

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 2014-01-29
      • 2015-11-25
      • 2013-05-27
      • 2014-10-03
      • 2011-04-01
      相关资源
      最近更新 更多