简体   繁体   English

从qtablewidget列中检索数据

[英]Retrieving data from columns qtablewidget

I want to have a tablewidget that will color some of the rows based on certain conditions and threshold. 我想拥有一个tablewidget,它将根据某些条件和阈值为某些行着色。 For example, if the data in one column reaches over 20, it will color the row where that 20 was. 例如,如果一列中的数据超过20,它将为该20所在的行上色。 What i have only searches through the Qtablewidgetitem and it does not do what I want it to do. 我只通过Qtablewidgetitem搜索,而没有执行我想要的操作。

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)

如果单元格包含整数,则应尝试:

int(newitem.text()) >= 20

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

As the other questions have also pointed out, you need to compare int to int, instead of string comparisons. 正如其他问题也指出的那样,您需要将int与int进行比较,而不是字符串比较。 What I have done here is first checked that the cell was actually an int first to make it save. 首先,我首先检查该单元格实际上是一个int型,以保存它。 Because if your cell was actually "WARNING", then converting it to an int first would crash. 因为如果您的单元格实际上是“ WARNING”,那么首先将其转换为int将会崩溃。

No matter what, you will need a reference to the QTableWidget , even if the function is in a different class. 无论如何,即使函数位于不同的类中,也将需要引用QTableWidget 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: 一个示例是使用将表绑定到其中的partial回调:

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

Joaquin's point is that you are comparing a string (newitem.text()) with another string ('20'). Joaquin的观点是,您正在将一个字符串(newitem.text())与另一个字符串('20')进行比较。 This is an alphabetical comparison -- '3' > '200' , for example, even though the number 3 < the number 200. The rules you are describing are comparisons between numbers, so you need to convert newitem.text() to a number. 这是一个字母比较-例如,即使数字3 <数字200,也是'3' > '200' 。您所描述的规则是数字之间的比较,因此您需要将newitem.text()转换为a数。

Note that even if you are entering "numbers" into the widget, they are stored and retrieved as strings. 请注意,即使您在小部件中输入“数字”,它们也会作为字符串存储和检索。 int(newitem.text()) turns it back into a number. int(newitem.text())将其转换为数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM