为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

holdtom 2023-11-09 21:34:58
在下图中,我有一个 QTableWidget 来查看来自一对多关系 SQLite 数据库的数据,我设法使用 setspan 功能和 @eyllanesc 的这个答案的帮助来合并一些列输出以正确显示数据。我现在想要实现的是用背景颜色(交替颜色)对每个订单进行着色以提高可读性,尝试了 table.setAlternatingRowColors(True) 但当订单包含多个项目时它会失败!如何完成条件行样式?import sysfrom PyQt5 import QtWidgetsfrom 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)
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

经过一番搜索,设法以这种方式解决问题:

  • 如果订单号是奇数,则使用颜色 1 对该行进行着色,如果订单号是偶数,则使用颜色 2 对该行进行着色


https://img1.sycdn.imooc.com/654ce02d00015be911560653.jpg

这是工作代码


import sys

from PyQt5 import QtWidgets, QtGui, Qt

from PyQt5.QtGui import QColor

from 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):

    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)

        self.viewTodayTable.setHorizontalHeaderItem(0, QtWidgets.QTableWidgetItem("Order ID"))

        self.viewTodayTable.setHorizontalHeaderItem(1, QtWidgets.QTableWidgetItem("Karat"))

        self.viewTodayTable.setHorizontalHeaderItem(2, QtWidgets.QTableWidgetItem("Price"))

        self.viewTodayTable.setHorizontalHeaderItem(3, QtWidgets.QTableWidgetItem("Weight"))

        self.viewTodayTable.setHorizontalHeaderItem(4, QtWidgets.QTableWidgetItem("Description"))

        self.viewTodayTable.setHorizontalHeaderItem(5, QtWidgets.QTableWidgetItem("Gram price"))

        self.viewTodayTable.setHorizontalHeaderItem(6, QtWidgets.QTableWidgetItem("Employee"))

        self.viewTodayTable.setHorizontalHeaderItem(7, QtWidgets.QTableWidgetItem("Client"))

        self.viewTodayTable.horizontalHeader().setDefaultSectionSize(130)

        self.viewTodayTable.horizontalHeader().setSortIndicatorShown(True)

        self.viewTodayTable.horizontalHeader().setStretchLastSection(True)


        vbox.addWidget(btn_show_table)

        vbox.addWidget(self.viewTodayTable)

        self.setLayout(vbox)


        self.show()


    def apply_span_to_sales_table(self, row, nrow):

        if nrow <= 1:

            return

        for c in (0, 2, 6, 7):

            self.viewTodayTable.setSpan(row, c, nrow, 1)

            for r in range(row + 1, row + nrow):

                t = self.viewTodayTable.takeItem(r, c)

                del t


    def today_sales_table(self):

        today_result = query_result

        self.viewTodayTable.setRowCount(0)


        last_id = -1

        start_row = 0

        for row_number, row_data in enumerate(today_result):

            self.viewTodayTable.insertRow(row_number)

            current_id, *other_values = row_data


            for column_number, data in enumerate(row_data):

                it = QtWidgets.QTableWidgetItem(str(data))


                self.viewTodayTable.setItem(row_number, column_number, it)


                if current_id % 2 == 1:

                    self.viewTodayTable.item(row_number, column_number).setBackground(QColor(185, 206, 172))

                    print("whats up")

                elif current_id % 2 == 0:

                    self.viewTodayTable.item(row_number, column_number).setBackground(QColor(193, 171, 206))

                    print("whats up 2")


                if last_id != current_id and last_id != -1:

                    self.apply_span_to_sales_table(start_row, row_number - start_row)

                    start_row = row_number


                last_id = current_id

                if start_row != row_number:

                    self.apply_span_to_sales_table(start_row, self.viewTodayTable.rowCount() - start_row)



def main():

    App = QApplication(sys.argv)

    window = Window()

    sys.exit(App.exec())



if __name__ == '__main__':

    main()



查看完整回答
反对 回复 2023-11-09
  • 1 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信