猿问

满足条件时更改 PyQt5 QTableWidget 行样式表

在下图中,我有一个 QTableWidget 来查看来自一对多关系 SQLite 数据库的数据,我设法使用 setspan 功能和 @eyllanesc 的这个答案的帮助来合并一些列输出以正确显示数据。


我现在想要实现的是用背景颜色(交替颜色)对每个订单进行着色以提高可读性,尝试了 table.setAlternatingRowColors(True) 但当订单包含多个项目时它会失败!如何完成条件行样式?


import sys

from PyQt5 import QtWidgets

from PyQt5.QtWidgets import *


query_result = [(683, 18, 765, 1.73, '1 ring ruby ring', 685.71, 'vincent percy', 'john joseph croft'), (684, 14, 900, 4.48, '1 earring ear drop', 534.86, 'Ben Otten', 'Anne Cooksley Beltrame'), (684, 14, 900, 2.1, '1 ring cluster ring', 534.86, 'Ben Otten', 'Anne Cooksley Beltrame'), (684, 18, 900, 1.3, '1 ring eternity band', 685.71, 'Ben Otten', 'Anne Cooksley Beltrame'), (685, 14, 200, 3.26, '1 ring promise ring', 534.86, 'raymond bob', 'owen george taylor'), (686, 24, 450, 10.0, '1 bullion Gold bar', 914.28, 'vincent percy', 'owen george taylor'), (687, 14, 345, 4.75, '1 earring Dangles Earring', 534.86, 'Ben Otten', 'dan justin balmers'), (688, 18, 810, 3.1, '1 earring fish hookEarring', 677.14, 'raymond bob', 'jeff david steve'), (688, 21, 810, 2.6, '1 ring ANTIQUE RING', 790, 'raymond bob', 'jeff david steve')]



class Window(QWidget):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("mini_ui")

        self.setGeometry(300, 150, 800, 600)

        self.Ui()


    def Ui(self):

        vbox = QVBoxLayout()

        btn_show_table = QPushButton("view sample data")

        btn_show_table.clicked.connect(self.today_sales_table)

        self.viewTodayTable = QTableWidget()

        self.viewTodayTable.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)

        self.viewTodayTable.setObjectName("viewTodayTable")

        self.viewTodayTable.setColumnCount(8)

        self.viewTodayTable.setRowCount(0)

holdtom
浏览 152回答 1
1回答

临摹微笑

经过一番搜索,设法以这种方式解决问题:如果订单号是奇数,则使用颜色 1 对该行进行着色,如果订单号是偶数,则使用颜色 2 对该行进行着色这是工作代码import sysfrom PyQt5 import QtWidgets, QtGui, Qtfrom PyQt5.QtGui import QColorfrom PyQt5.QtWidgets import *prev_day = "2020-10-15"query_result = [(683, 18, 765, 1.73, '1 ring ruby ring', 685.71, 'vincent percy', 'john joseph croft'), (684, 14, 900, 4.48, '1 earring ear drop', 534.86, 'Ben Otten', 'Anne Cooksley Beltrame'), (684, 14, 900, 2.1, '1 ring cluster ring', 534.86, 'Ben Otten', 'Anne Cooksley Beltrame'), (684, 18, 900, 1.3, '1 ring eternity band', 685.71, 'Ben Otten', 'Anne Cooksley Beltrame'), (685, 14, 200, 3.26, '1 ring promise ring', 534.86, 'raymond bob', 'owen george taylor'), (686, 24, 450, 10.0, '1 bullion Gold bar', 914.28, 'vincent percy', 'owen george taylor'), (687, 14, 345, 4.75, '1 earring Dangles Earring', 534.86, 'Ben Otten', 'dan justin balmers'), (688, 18, 810, 3.1, '1 earring fish hookEarring', 677.14, 'raymond bob', 'jeff david steve'), (688, 21, 810, 2.6, '1 ring ANTIQUE RING', 790, 'raymond bob', 'jeff david steve')]class Window(QWidget):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("mini_ui")&nbsp; &nbsp; &nbsp; &nbsp; self.setGeometry(300, 150, 800, 600)&nbsp; &nbsp; &nbsp; &nbsp; self.Ui()&nbsp; &nbsp; def Ui(self):&nbsp; &nbsp; &nbsp; &nbsp; vbox = QVBoxLayout()&nbsp; &nbsp; &nbsp; &nbsp; btn_show_table = QPushButton("view sample data")&nbsp; &nbsp; &nbsp; &nbsp; btn_show_table.clicked.connect(self.today_sales_table)&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable = QTableWidget()&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setObjectName("viewTodayTable")&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setColumnCount(8)&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setRowCount(0)&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(0, QtWidgets.QTableWidgetItem("Order ID"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(1, QtWidgets.QTableWidgetItem("Karat"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(2, QtWidgets.QTableWidgetItem("Price"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(3, QtWidgets.QTableWidgetItem("Weight"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(4, QtWidgets.QTableWidgetItem("Description"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(5, QtWidgets.QTableWidgetItem("Gram price"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(6, QtWidgets.QTableWidgetItem("Employee"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setHorizontalHeaderItem(7, QtWidgets.QTableWidgetItem("Client"))&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.horizontalHeader().setDefaultSectionSize(130)&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.horizontalHeader().setSortIndicatorShown(True)&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.horizontalHeader().setStretchLastSection(True)&nbsp; &nbsp; &nbsp; &nbsp; vbox.addWidget(btn_show_table)&nbsp; &nbsp; &nbsp; &nbsp; vbox.addWidget(self.viewTodayTable)&nbsp; &nbsp; &nbsp; &nbsp; self.setLayout(vbox)&nbsp; &nbsp; &nbsp; &nbsp; self.show()&nbsp; &nbsp; def apply_span_to_sales_table(self, row, nrow):&nbsp; &nbsp; &nbsp; &nbsp; if nrow <= 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; for c in (0, 2, 6, 7):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setSpan(row, c, nrow, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for r in range(row + 1, row + nrow):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t = self.viewTodayTable.takeItem(r, c)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; del t&nbsp; &nbsp; def today_sales_table(self):&nbsp; &nbsp; &nbsp; &nbsp; today_result = query_result&nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setRowCount(0)&nbsp; &nbsp; &nbsp; &nbsp; last_id = -1&nbsp; &nbsp; &nbsp; &nbsp; start_row = 0&nbsp; &nbsp; &nbsp; &nbsp; for row_number, row_data in enumerate(today_result):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.insertRow(row_number)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_id, *other_values = row_data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for column_number, data in enumerate(row_data):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it = QtWidgets.QTableWidgetItem(str(data))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.setItem(row_number, column_number, it)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if current_id % 2 == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.item(row_number, column_number).setBackground(QColor(185, 206, 172))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("whats up")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif current_id % 2 == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.viewTodayTable.item(row_number, column_number).setBackground(QColor(193, 171, 206))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("whats up 2")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if last_id != current_id and last_id != -1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.apply_span_to_sales_table(start_row, row_number - start_row)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start_row = row_number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_id = current_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if start_row != row_number:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.apply_span_to_sales_table(start_row, self.viewTodayTable.rowCount() - start_row)def main():&nbsp; &nbsp; App = QApplication(sys.argv)&nbsp; &nbsp; window = Window()&nbsp; &nbsp; sys.exit(App.exec())if __name__ == '__main__':&nbsp; &nbsp; main()
随时随地看视频慕课网APP

相关分类

Python
我要回答