tit*_*jan 9 python qt pyqt pyqt5
我有一个带有a的GUI,QDockwidget我希望如果用户用鼠标光标悬停在它上面,则停靠窗口小部件标题栏的颜色会改变颜色.我在下面有一个小测试程序,当光标位于标题栏上方时,标题栏会改变颜色.但是,如果光标位于停靠窗口小部件的其余部分上方,它也会更改颜色.有没有什么办法解决这一问题?
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()
Run Code Online (Sandbox Code Playgroud)
PS我已经将应用程序Qt样式设置为fusion,可以使用调色板完全配置(与macintosh样式不同).我更喜欢适用于所有Qt样式的解决方案,但如果不可行,我可以考虑fusion在所有平台上设置我的应用程序样式.
我尝试了你的代码,我可以重现同样的问题,这对我来说似乎是一个错误。
这是使用自定义小部件作为标题的可能解决方法:
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_()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |