python tablewidget 颜色_【整理】PyQt如何修改QTableView中表格颜色即如何修改QModelIndex颜色 | 勤奋的小青蛙...

修改颜色,整理有两种方法,其中第一中方法是对index设置role,另外一个方法是在data的函数里根据role进行返回颜色第一种方法:对index设置role参考链接:示例代码:QModelIndex vIndex = model()->index(mLastIndex,0);model->setData(vIndex, QBrush(Qt::red), Qt::Foreground...
摘要由CSDN通过智能技术生成

修改颜色,整理有两种方法,其中第一中方法是对index设置role,另外一个方法是在data的函数里根据role进行返回颜色

第一种方法:对index设置role

参考链接:

示例代码:

QModelIndex vIndex = model()->index(mLastIndex,0);

model->setData(vIndex, QBrush(Qt::red), Qt::ForegroundRole);

第二种方法:重写data方法,根据role进行判断,返回颜色

示例代码:

QVariant MyModel::data ( const QModelIndex & item, int role ) const

{

//change the background color

if (role == Qt::BackgroundRole)

{

return QColor(255, 0, 0);

}

return QSortFilterProxyModel::data(item, role);

}

下面是根据这第二种方法,写出来的一个PyQt的示例,所有代码如下:

''' pqt_tableview3.py

explore PyQT's QTableView Model

using QAbstractTableModel to present tabular data

allow table sorting by clicking on the header title

used the Anaconda package (comes with PyQt4) on OS X

(dns)

'''

#coding=utf-8

import operator # used for sorting

from PyQt4.QtCore import *

from PyQt4.QtGui import *

from PyQt4 import QtGui, QtCore

from time import time

import threading

class MyWindow(QWidget):

def __init__(self, dataList, header, *args):

QWidget.__init__(self, *args)

# setGeometry(x_pos, y_pos, width, height)

self.setGeometry(70, 150, 1326, 582)

self.setWindowTitle("Click on the header to sort table")

self.table_model = MyTableModel(self, dataList, header)

self.table_view = QTableView()

#self.table_view.setSelectionMode(QAbstractItemView.SingleSelection)

self.table_view.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)

self.table_view.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)

# bind cell click to a method reference

self.table_view.clicked.connect(self.showSelection)

self.table_view.clicked.connect(self.selectRow)

self.table_view.setModel(self.table_model)

# enable sorting

self.table_view.setSortingEnabled(True)

layout = QVBoxLayout(self)

layout.addWidget(self.table_view)

self.setLayout(layout)

def update_model(self, datalist, header):

self.table_model2 = MyTableModel(self, dataList, header)

self.table_view.setModel(self.table_model2)

self.table_view.update()

def showSelection(self, item):

cellContent = item.data()

# print(cellContent) # test

sf = "You clicked on {}".format(cellContent)

# display in title bar for convenience

self.setWindowTitle(sf)

def selectRow(self, index):

# print("current row is %d", index.row())

pass

class MyTableModel(QAbstractTableModel):

"""

keep the method names

they are an integral part of the model

"""

def __init__(self, parent, mylist, header, *args):

QAbstractTableModel.__init__(self, parent, *args)

self.mylist = mylist

self.header = header

self.timer = QtCore.QTimer()

self.change_flag = True

self.timer.timeout.connect(self.updateModel)

self.timer.start(500)

# self.rowCheckStateMap = {}

def setDataList(self, mylist):

self.mylist = mylist

self.layoutAboutToBeChanged.emit()

self.dataChanged.emit(self.createIndex(0, 0), self.createIndex(self.rowCount(0), self.columnCount(0)))

self.layoutChanged.emit()

def updateModel(self):

dataList2 = []

checkbox1 = QtGui.QCheckBox("关");

checkbox1.setChecked(True)

if self.change_flag is True:

dataList2 = [

[checkbox1, 0, '058176', '02', 'cu1705,cu1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '03', 'zn1705,zn1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '04', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'zn1705,zn1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '02', 'ru1705,ru1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '02', 'ni1705,ni1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '02', 'cu1705,cu1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '03', 'zn1705,zn1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '04', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'zn1705,zn1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '02', 'ru1705,ru1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '02', 'ni1705,ni1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'MA', '01'],

[checkbox1, 0, '058176', '01', 'rb1705,rb1710', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值