如何解决如何在 Pyqt5 中设置 QTableWidget 的样式
我目前正在为我的 GUI 应用程序设计样式,但是我在为 QTableWidget 设置样式时遇到了一些麻烦,因为正如您在图片中看到的,我的 QTableWidget 的左上角有一个小白色区域,我希望它是淡蓝色作为表的其余部分。
这是我到目前为止的样式代码:
def start_ui():
app = QtWidgets.QApplication(sys.argv)
style = """
QFrame{
color: blue;
background: lightgreen;
font-weight: bold;
}
QLabel{
color: blue;
font-weight: bold;
}
QLineEdit{
color: blue;
border-style: solid;
border: 2px solid black;
background: lightblue;
font-weight: bold;
}
QPushButton{
border-style: solid;
border: 2px solid black;
color: darkblue;
background: lightblue;
font-weight: bold;
}
QTableWidget{
background: lightblue;
}
"""
app.setStyleSheet(style)
win = ModbusLoggerWindow()
win.show()
sys.exit(app.exec_())
然后我在类中有这三行,在那里我创建了我的 QTableWidget,这些行设置了我的垂直标题和水平标题:
stylesheetHeader = "::section{background-color: lightblue}"
self.tableWidget.horizontalHeader().setStyleSheet(stylesheetHeader)
self.tableWidget.verticalHeader().setStyleSheet(stylesheetHeader)
解决方法
左上角的这个元素不是 QHeaderView 的一部分,而是一个 QAbstractButton,因此一个可能的解决方案是将样式表直接应用于按钮:
button = self.tableWidget.findChild(QAbstractButton)
button.setStyleSheet("background-color: lightblue")
为了避免使用 findChild 可以通过 QTableWidget 设置:
stylesheet = """
QHeaderView::section{background-color: lightblue}
QAbstractButton{background-color: lightblue}
"""
self.tableWidget.setStyleSheet(stylesheet)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。