【问题标题】:Change QDocketWidget hover title bar color with CSS使用 CSS 更改 QDocketWidget 悬停标题栏颜色
【发布时间】:2019-05-07 08:21:30
【问题描述】:

我有一个带有QDockwidget 的 GUI,我希望如果用户将鼠标光标悬停在停靠小部件的标题栏上,它的颜色会改变颜色。我在下面有一个小测试程序,当光标在其上方时,标题栏会改变颜色。但是,如果光标位于停靠小部件的其余部分上方,它也会改变颜色。有没有办法解决这个问题?

CSS = """

    QDockWidget::title {
        background-color: lightblue; 
        border: 1px solid black;
    }

    QDockWidget::title:hover {
        background: yellow;
    }

    QMainWindow::separator {
        background: palette(Midlight);
    }

    QMainWindow::separator:hover {
        background: palette(Mid);
    }
"""

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QSize

class CenteredLabel(QtWidgets.QWidget):

    def __init__(self, text, parent=None):
        super().__init__(parent=parent)
        self.verLayout = QtWidgets.QVBoxLayout()
        self.setLayout(self.verLayout)
        self.horLayout = QtWidgets.QHBoxLayout()
        self.verLayout.addLayout(self.horLayout)
        self.label = QtWidgets.QLabel(text)
        self.horLayout.addWidget(self.label)

    def sizeHint(self):
        return QSize(300, 400)


class MyWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setCentralWidget(CenteredLabel("Central Widget"))
        self.dockWidget = QtWidgets.QDockWidget("Dock Title", parent=self)
        self.dockWidget.setWidget(CenteredLabel("Dock Widget"))
        self.addDockWidget(Qt.LeftDockWidgetArea, self.dockWidget)


def main():
    app = QtWidgets.QApplication([])

    # Fusion style is the default style on Linux
    app.setStyle(QtWidgets.QStyleFactory.create("fusion"))
    app.setStyleSheet(CSS)

    win = MyWindow()
    win.show()
    win.raise_()
    app.exec_()

if __name__ == "__main__":
    main()

附:我已将应用程序 Qt 样式设置为 fusion,它完全可以使用调色板进行配置(与 macintosh 样式不同)。我更喜欢适用于所有 Qt 样式的解决方案,但如果这不可能,我可以考虑在所有平台上将我的应用程序样式设置为 fusion

【问题讨论】:

    标签: python qt pyqt pyqt5


    【解决方案1】:

    我试过你的代码,我可以重现同样的问题,这对我来说似乎是一个错误。

    以下是使用自定义小部件作为标题的可能解决方法:

    from PyQt5.QtWidgets import QLabel
    
    CSS = """
        #CustomTitle {
            background-color: lightblue; 
            border: 1px solid black;
        }
    
        #CustomTitle:hover {
            background: red;
        }
    
        QMainWindow::separator {
            background: palette(Midlight);
        }
    
        QMainWindow::separator:hover {
            background: palette(Mid);
        }
    """
    
    from PyQt5 import QtWidgets
    from PyQt5.QtCore import Qt, QSize
    
    class CenteredLabel(QtWidgets.QWidget):
    
        def __init__(self, text, parent=None):
            super().__init__(parent=parent)
            self.verLayout = QtWidgets.QVBoxLayout()
            self.setLayout(self.verLayout)
            self.horLayout = QtWidgets.QHBoxLayout()
            self.verLayout.addLayout(self.horLayout)
            self.label = QtWidgets.QLabel(text)
            self.horLayout.addWidget(self.label)
    
        def sizeHint(self):
            return QSize(300, 400)
    
    
    class MyWindow(QtWidgets.QMainWindow):
    
        def __init__(self, parent=None):
            super().__init__(parent=parent)
            self.setCentralWidget(CenteredLabel("Central Widget"))
            self.dockWidget = QtWidgets.QDockWidget(parent=self)
            self.dockWidget.setWidget(CenteredLabel("Dock Widget"))
            self.customTitle = QLabel("Dock Title", parent=self.dockWidget)
            self.customTitle.setObjectName("CustomTitle")
            self.dockWidget.setTitleBarWidget(self.customTitle)
            self.addDockWidget(Qt.LeftDockWidgetArea, self.dockWidget)
    
    
    def main():
        app = QtWidgets.QApplication([])
    
        # Fusion style is the default style on Linux
        app.setStyle(QtWidgets.QStyleFactory.create("fusion"))
        app.setStyleSheet(CSS)
    
        win = MyWindow()
        win.show()
        win.raise_()
    

    【讨论】:

    • 是的,这可能是一个错误,但使用 CSS 总是很难判断我做错了什么还是错误。至少有了setTitleBarWidget,我就可以自己制作标题栏了。
    • 我认为这是 Qt 中的一个错误,您的代码对我来说看起来不错 ;-)
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2017-07-21
    • 2021-09-09
    • 2016-12-22
    • 2018-11-12
    • 2014-08-12
    相关资源
    最近更新 更多