在 pyqt5 QTableWidget 中应用样式:已选择
Posted
技术标签:
【中文标题】在 pyqt5 QTableWidget 中应用样式:已选择【英文标题】:Apply style in pyqt5 QTableWidget:selected 【发布时间】:2021-10-06 21:28:35 【问题描述】:我有这个简单的应用程序:
文件:untitled.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1160, 574)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.player_list_table = QtWidgets.QTableWidget(Dialog)
self.player_list_table.setMinimumSize(QtCore.QSize(0, 850))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 120, 215))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.HighlightedText, brush)
brush = QtGui.QBrush(QtGui.QColor(170, 255, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.AlternateBase, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 120, 215))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.HighlightedText, brush)
brush = QtGui.QBrush(QtGui.QColor(170, 255, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.AlternateBase, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 120, 215))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.HighlightedText, brush)
brush = QtGui.QBrush(QtGui.QColor(170, 255, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.AlternateBase, brush)
self.player_list_table.setPalette(palette)
self.player_list_table.setFocusPolicy(QtCore.Qt.NoFocus)
self.player_list_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.player_list_table.setAlternatingRowColors(True)
self.player_list_table.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.player_list_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.player_list_table.setRowCount(20)
self.player_list_table.setObjectName("player_list_table")
self.player_list_table.setColumnCount(2)
item = QtWidgets.QTableWidgetItem()
self.player_list_table.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.player_list_table.setHorizontalHeaderItem(1, item)
self.player_list_table.horizontalHeader().setCascadingSectionResizes(False)
self.player_list_table.horizontalHeader().setHighlightSections(False)
self.player_list_table.horizontalHeader().setMinimumSectionSize(260)
self.player_list_table.horizontalHeader().setSortIndicatorShown(False)
self.player_list_table.horizontalHeader().setStretchLastSection(True)
self.player_list_table.verticalHeader().setHighlightSections(False)
self.gridLayout.addWidget(self.player_list_table, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.player_list_table.setSortingEnabled(False)
item = self.player_list_table.horizontalHeaderItem(0)
item.setText(_translate("Dialog", "Αλλαγή θέσης"))
item = self.player_list_table.horizontalHeaderItem(1)
item.setText(_translate("Dialog", "Θέση αρχείου"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
文件:main.py
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from untitled import Ui_Dialog
class Main:
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
ui.player_list_table.setRowCount(0)
for i in range(0,20):
ui.player_list_table.insertRow(i)
#Title
title_item = QtWidgets.QTableWidgetItem("Title text")
title_item.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter | QtCore.Qt.Alignment(QtCore.Qt.TextWordWrap))
ui.player_list_table.setItem(i,0,title_item)
#Image
image_frame = QtWidgets.QFrame()
image_frame.setStyleSheet("QFrame\nborder:none;background:transparent;\n")
gridLayout = QtWidgets.QGridLayout(image_frame)
image_title = QtWidgets.QLabel(image_frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(image_title.sizePolicy().hasHeightForWidth())
image_title.setSizePolicy(sizePolicy)
image_title.setAlignment(QtCore.Qt.AlignCenter)
image_title.setWordWrap(True)
image_title.setStyleSheet("QLabel\nborder:none;background:transparent;\n")
gridLayout.addWidget(image_title, 1, 0, 1, 3)
image = QtWidgets.QLabel(image_frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(image.sizePolicy().hasHeightForWidth())
image.setSizePolicy(sizePolicy)
image.setMinimumSize(QtCore.QSize(150, 150))
image.setMaximumSize(QtCore.QSize(150, 150))
image.setText("")
image.setScaledContents(True)
gridLayout.addWidget(image, 0, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
gridLayout.addItem(spacerItem, 0, 2, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
gridLayout.addItem(spacerItem1, 0, 0, 1, 1)
image_title.setText("Image title text")
ui.player_list_table.setCellWidget(i,1,image_frame)
ui.player_list_table.resizeRowsToContents()
ui.player_list_table.resizeColumnsToContents()
ui.player_list_table.selectRow(0)
sys.exit(app.exec_())
if __name__=="__main__":
main = Main()
问题是当我运行:python main.py
然后突出显示一行时,第一列中的文本颜色为白色,但第二列中的文本颜色为黑色。 :/
在这种情况下我可以使用什么 qss 选择器?
【问题讨论】:
【参考方案1】:我使用currentCellChanged signal 来查找选择了哪一行。然后当这个信号发出时,我改变图像标题标签的 qss。
【讨论】:
您能否对您的答案添加更多见解?不是,不清楚你做了什么,也没有解释为什么。此外,如果修改只有几行,则无需再次发布未更改的文件,也无需发布整个修改的文件。 @musicamante 我编辑答案。 @ChrisP 最准确的答案总是最好的。准确不是最少但不多,这个想法是任何用户都可以了解如何应用解决方案,如果可能的话还可以解释原因,但是如果您启动大量代码只会让我们感到困惑 @ChrisP 抱歉,但您的编辑不是很有帮助(您可以看到这不仅仅是我的意见)。我相信您已经看到了很多很好的答案示例,它们不一定需要非常长或非常详细,但应该始终详尽并解释问题的原因(如果有的话)以及解决方案的工作原理。在您当前的答案中,您只告诉您正在使用什么方法,而不是显示和解释您如何使用它(也不是为什么)。以上是关于在 pyqt5 QTableWidget 中应用样式:已选择的主要内容,如果未能解决你的问题,请参考以下文章