如何为QTreeWidget中的项设置标志?

2024-10-01 17:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QTreeWidget,它包含两列和一些行。我想设置一个标志,这样如果第二列中的一个项目是零或空的,它就不能被编辑。如果单击的项目不是数字,它将显示为红色文本。你知道吗

我的代码:

def qtree_check_item(self, item, column):
    item.setFlags(QtCore.Qt.ItemIsEnabled)
    if column == 1:
        if item.text(1) != '0':
            item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
        if not item.text(1).isnumeric():
            item.setForeground(1, QtGui.QBrush(QtGui.QColor("red")))

如果项为零,则此操作有效。如果我替换:

if item.text(1) != '0':

if item.text(1) != '':

这适用于空字符串。但如果我结合使用:

if item.text(1) != '0' or item.text(1) != '':

未设置标志。我做错什么了?你知道吗


Tags: 项目text文本编辑if标志column数字
1条回答
网友
1楼 · 发布于 2024-10-01 17:40:23

I would like to set a flag so that if an item in the second column is either a zero or empty, it cannot be edited.

那你就。。。你知道吗

if item.text(1) != '0' or item.text(1) != '':
    item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)

现在考虑一下如果item.text(1) == '0'发生了什么。在这种情况下,第二个测试item.text(1) != ''通过,并且由于or,条件作为一个整体通过。同样地,如果item.text(1) == ''为真,那么测试item.text(1) != '0'将通过,结果是整个条件通过。你知道吗

所以,您只想设置editable标志,如果两者都。。。你知道吗

item.text(1) != '0' and item.text(1) != ''

坚持到底。你知道吗

换言之,由于item.text(1)不能同时等于'0''',因此条件。。。你知道吗

if item.text(1) != '0' or item.text(1) != '':

本质上是。。。你知道吗

if True or False:

一切都会过去的。你知道吗

(抱歉,如果这一切看起来有点复杂。)

相关问题 更多 >

    热门问题