从qtablewidget列中检索数据 [英] Retrieving data from columns qtablewidget

查看:1280
本文介绍了从qtablewidget列中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个tablewidget,它将根据某些条件和阈值为某些行着色.例如,如果一列中的数据超过20,它将为该20所在的行上色.我只通过Qtablewidgetitem搜索内容,而没有执行我想要的操作.

I want to have a tablewidget that will color some of the rows based on certain conditions and threshold. For example, if the data in one column reaches over 20, it will color the row where that 20 was. What i have only searches through the Qtablewidgetitem and it does not do what I want it to do.

def setmydata(self):
    for n, key in enumerate(self.data):
        for m, item in enumerate(self.data[key]):
            newitem = QtGui.QTableWidgetItem(item)
            c = newitem.column() + 2
            print c
            for items in item:
                if newitem.text() >= '20' or newitem.text() == 'WARNING':
                    newitem.setBackground(QtGui.QBrush(QtCore.Qt.yellow))
                else:
                    pass
            self.setItem(m, n, newitem)

推荐答案

对于包含数据的现有表,要在其上的特定列进行迭代,则应执行以下操作:

For an existing table, with data, where you want to iterate over a specific column, you would do something like this:

def process_column(table, processCol=0):
    for row in xrange(table.rowCount()):
        item = table.item(row, processCol)
        text = str(item.text())

        if (text.isdigit() and int(text) >= 20) or text == 'WARNING':
            item.setBackground(QtGui.QBrush(QtCore.Qt.yellow))

或者要设置整个行的颜色,您需要遍历各列以在存在匹配项时获取每个行项目:

Or to set the whole row color, you would need to loop over the columns to get each row item when there is a match:

def process_column(table, processCol=0):
    colCount = table.rowCount()

    for row in xrange(table.rowCount()):
        item = table.item(row, processCol)
        text = str(item.text())

        if (text.isdigit() and int(text) >= 20) or text == 'WARNING':
            for col in xrange(colCount):
                item = table.item(row, col)
                item.setBackground(QtGui.QBrush(QtCore.Qt.yellow))

正如其他问题也指出的那样,您需要将int与int进行比较,而不是字符串比较.首先,我首先检查该单元格实际上是一个int以便保存,这是我在这里所做的.因为如果您的单元格实际上是"WARNING",那么首先将其转换为int将会崩溃.

As the other questions have also pointed out, you need to compare int to int, instead of string comparisons. What I have done here is first checked that the cell was actually an int first to make it save. Because if your cell was actually "WARNING", then converting it to an int first would crash.

无论如何,即使函数在不同的类中,也将需要引用QTableWidget.这意味着,如果其他类从未明确知道该信号,则您需要提前使用对该表的引用来设置您的信号.例如,使用partial回调将表绑定到其中:

No matter what, you will need a reference to the QTableWidget, even if the function is in a different class. That means you will need to set up your signal with a reference to the table ahead of time, if that other class never specifically will know about it. An example of this would be to use a partial callback that binds the table into it:

from functools import partial 

class Foo:
    def __init__(self):
        self.the_table = QTableWidget()

        # create a callback with the table bound as first arg
        callback = partial(process_column, self.the_table)

        # set some signal that emits a column number
        self.process_column_signal.connect(callback)

    def some_method(self):
        # process column 0
        self.process_column_signal.emit(0)

# will get called by process_column_signal with the table
def process_column(table, processCol):
    ...

这篇关于从qtablewidget列中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆