PyQt5 QTreeView.clear() AttributeError: 'QTreeView' object has no attribute 'clear'

PyQt5 QTreeView.clear() AttributeError: 'QTreeView' object has no attribute 'clear'

File "/.../.../........./app.py", line 64, in textchanged
    self.treeView.clear()

AttributeError: 'QTreeView' object has no attribute 'clear'

知道如何清除 QTreeView 小部件吗? Google 没有帮助

尝试过https://gist.github.com/tcrowson/8273931

import PySide
from PySide import QtGui

def clearQTreeWidget(tree):
    iterator = QtGui.QTreeWidgetItemIterator(tree, QtGui.QTreeWidgetItemIterator.All)
    while iterator.value():
        iterator.value().takeChildren()
        iterator +=1
    i = tree.topLevelItemCount()
    while i > -1:
        tree.takeTopLevelItem(i)
        i -= 1

但在 PyQt5 中:

AttributeError: module 'PyQt5.QtGui' has no attribute 'QTreeWidgetItemIterator'

此答案适用于所有在上述评论中帮助过我的人:

1111.pdb :

ATOM      1  N   ASN A   1      -7.615   2.627   3.239  1.00  4.51           N  
ANISOU    1  N   ASN A   1      644    614    454    -45     26     52       N  
ATOM      2  CA  ASN A   1      -6.403   1.794   2.980  1.00  4.52           C  
ANISOU    2  CA  ASN A   1      638    558    520    -22    -10      3       C  
ATOM      3  C   ASN A   1      -5.154   2.381   3.598  1.00  4.76           C  
ANISOU    3  C   ASN A   1      618    625    565    -15     -6     -5       C  
ATOM      4  O   ASN A   1      -5.052   3.583   3.763  1.00  5.03           O  
ANISOU    4  O   ASN A   1      687    619    605     42    -54     -9       O  
ATOM      5  CB  ASN A   1      -6.219   1.595   1.484  1.00  4.79           C  
ANISOU    5  CB  ASN A   1      673    605    542     14      5    -27       C  
ATOM      6  CG  ASN A   1      -7.469   1.079   0.834  1.00  6.31           C  
ANISOU    6  CG  ASN A   1      755    877    767     26     17    -45       C  
ATOM      7  OD1 ASN A   1      -7.912  -0.028   1.124  1.00  5.83           O  
ANISOU    7  OD1 ASN A   1      854    753    610     60    -37   -119       O  
ATOM      8  ND2 ASN A   1      -8.079   1.901  -0.009  1.00  6.13           N  
ANISOU    8  ND2 ASN A   1      686    925    717     87     13      0       N  
ATOM      9  N   ASN A   2      -4.224   1.506   3.951  1.00  4.81           N  
ANISOU    9  N   ASN A   2      624    619    583      0     16     -5       N  
ATOM     10  CA  ASN A   2      -2.995   1.882   4.640  1.00  4.73           C  
ANISOU   10  CA  ASN A   2      604    608    584     -3     21      7       C  
ATOM     11  C   ASN A   2      -1.771   1.299   3.937  1.00  4.43           C  
ANISOU   11  C   ASN A   2      571    573    539     10     25      4       C  
ATOM     12  O   ASN A   2      -1.737   0.108   3.651  1.00  4.42           O  
ANISOU   12  O   ASN A   2      498    606    576     -8      6     34       O  
ATOM     13  CB  ASN A   2      -3.041   1.359   6.079  1.00  4.98           C  
ANISOU   13  CB  ASN A   2      638    630    626    -18     50     19       C  
ATOM     14  CG  ASN A   2      -1.845   1.784   6.884  1.00  5.61           C  
ANISOU   14  CG  ASN A   2      770    685    678    -10     17     29       C  
ATOM     15  OD1 ASN A   2      -1.609   2.986   7.034  1.00  7.46           O  
ANISOU   15  OD1 ASN A   2     1179    674    980    -44   -236     39       O  
ATOM     16  ND2 ASN A   2      -1.080   0.819   7.410  1.00  5.64           N  
ANISOU   16  ND2 ASN A   2      767    719    658    -36    -48     54       N  
ATOM     17  N   GLN A   3      -0.784   2.153   3.656  1.00  4.16           N  
ANISOU   17  N   GLN A   3      565    559    457     36      6      0       N  
ATOM     18  CA  GLN A   3       0.550   1.721   3.226  1.00  4.34           C  
ANISOU   18  CA  GLN A   3      567    556    526     10     21      8       C  
ATOM     19  C   GLN A   3       1.591   2.457   4.068  1.00  4.48           C  
ANISOU   19  C   GLN A   3      566    573    562     41      0      6       C  
ATOM     20  O   GLN A   3       1.617   3.677   4.085  1.00  4.56           O  
ANISOU   20  O   GLN A   3      576    576    580     16    -47     47       O  
ATOM     21  CB  GLN A   3       0.790   1.994   1.735  1.00  4.34           C  
ANISOU   21  CB  GLN A   3      533    574    541    -20     18     51       C  
ATOM     22  CG  GLN A   3       2.095   1.388   1.209  1.00  4.58           C  
ANISOU   22  CG  GLN A   3      676    524    540     32     30     10       C  
ATOM     23  CD  GLN A   3       2.524   1.951  -0.124  1.00  4.63           C  
ANISOU   23  CD  GLN A   3      680    546    532    -56      7     -3       C  
ATOM     24  OE1 GLN A   3       2.751   3.154  -0.250  1.00  4.86           O  
ANISOU   24  OE1 GLN A   3      741    508    598    -99     97    108       O  
ATOM     25  NE2 GLN A   3       2.670   1.085  -1.122  1.00  3.96           N  
ANISOU   25  NE2 GLN A   3      549    403    554     59    -29    -44       N  
ATOM     26  N   GLN A   4       2.425   1.709   4.783  1.00  4.51           N  
ANISOU   26  N   GLN A   4      570    573    572     26      0      4       N  
ATOM     27  CA  GLN A   4       3.503   2.295   5.567  1.00  5.00           C  
ANISOU   27  CA  GLN A   4      616    655    629      7    -13     19       C  
ATOM     28  C   GLN A   4       4.824   1.700   5.121  1.00  4.74           C  
ANISOU   28  C   GLN A   4      566    615    619     10    -23     16       C  
ATOM     29  O   GLN A   4       4.977   0.485   5.042  1.00  4.74           O  
ANISOU   29  O   GLN A   4      499    623    680     34    -69     61       O  
ATOM     30  CB  GLN A   4       3.263   2.101   7.063  1.00  5.31           C  
ANISOU   30  CB  GLN A   4      638    704    674     -1    -17     35       C  
ATOM     31  CG  GLN A   4       2.128   2.959   7.602  1.00  6.23           C  
ANISOU   31  CG  GLN A   4      784    793    792    -13     12     41       C  
ATOM     32  CD  GLN A   4       1.734   2.581   9.002  1.00  6.26           C  
ANISOU   32  CD  GLN A   4      816    844    719    -70    -74    -50       C  
ATOM     33  OE1 GLN A   4       1.144   1.526   9.223  1.00  7.59           O  
ANISOU   33  OE1 GLN A   4      989   1052    841   -241    -67     25       O  
ATOM     34  NE2 GLN A   4       2.088   3.427   9.976  1.00  8.64           N  
ANISOU   34  NE2 GLN A   4     1402   1172    710    -95    -36   -172       N  
ATOM     35  N   ASN A   5       5.758   2.597   4.821  1.00  5.19           N  
ANISOU   35  N   ASN A   5      596    702    673    -16      8     23       N  
ATOM     36  CA  ASN A   5       7.061   2.275   4.262  1.00  5.29           C  
ANISOU   36  CA  ASN A   5      642    709    660     11     -1      8       C  
ATOM     37  C   ASN A   5       8.114   2.789   5.215  1.00  5.89           C  
ANISOU   37  C   ASN A   5      700    750    788    -28     -4    -18       C  
ATOM     38  O   ASN A   5       8.370   3.981   5.260  1.00  6.26           O  
ANISOU   38  O   ASN A   5      765    718    897    -40    -47     31       O  
ATOM     39  CB  ASN A   5       7.185   2.937   2.893  1.00  5.44           C  
ANISOU   39  CB  ASN A   5      620    819    628     18     -1     23       C  
ATOM     40  CG  ASN A   5       6.036   2.570   1.974  1.00  4.77           C  
ANISOU   40  CG  ASN A   5      581    600    633     -7     71    -35       C  
ATOM     41  OD1 ASN A   5       5.922   1.414   1.558  1.00  4.98           O  
ANISOU   41  OD1 ASN A   5      758    486    648     56    -38    -81       O  
ATOM     42  ND2 ASN A   5       5.157   3.538   1.679  1.00  4.95           N  
ANISOU   42  ND2 ASN A   5      531    675    676   -141     28     79       N  
ATOM     43  N   TYR A   6       8.692   1.895   6.001  1.00  5.71           N  
ANISOU   43  N   TYR A   6      720    684    764    -31     -6    -29       N  
ATOM     44  CA  TYR A   6       9.522   2.304   7.140  1.00  6.06           C  
ANISOU   44  CA  TYR A   6      787    751    763    -21      1    -18       C  
ATOM     45  C   TYR A   6      10.972   2.565   6.754  1.00  6.79           C  
ANISOU   45  C   TYR A   6      852    866    863     -4    -20    -14       C  
ATOM     46  O   TYR A   6      11.344   2.407   5.590  1.00  6.49           O  
ANISOU   46  O   TYR A   6      864    773    829      5     17     59       O  
ATOM     47  CB  TYR A   6       9.432   1.277   8.273  1.00  6.25           C  
ANISOU   47  CB  TYR A   6      771    825    777     -9      1     -3       C  
ATOM     48  CG  TYR A   6       8.034   1.157   8.838  1.00  6.51           C  
ANISOU   48  CG  TYR A   6      872    761    840    -24     50     29       C  
ATOM     49  CD1 TYR A   6       7.124   0.253   8.307  1.00  7.04           C  
ANISOU   49  CD1 TYR A   6      893    876    905    -54     60      0       C  
ATOM     50  CD2 TYR A   6       7.618   1.964   9.891  1.00  6.57           C  
ANISOU   50  CD2 TYR A   6      816    839    841    -36    -22     -8       C  
ATOM     51  CE1 TYR A   6       5.838   0.139   8.824  1.00  7.41           C  
ANISOU   51  CE1 TYR A   6      891    961    963    -52    -26    -14       C  
ATOM     52  CE2 TYR A   6       6.330   1.864  10.407  1.00  7.41           C  
ANISOU   52  CE2 TYR A   6      928   1009    877    -29     30    -28       C  
ATOM     53  CZ  TYR A   6       5.451   0.942   9.879  1.00  7.24           C  
ANISOU   53  CZ  TYR A   6      823   1006    922     -7     36     18       C  
ATOM     54  OH  TYR A   6       4.177   0.856  10.381  1.00  8.39           O  
ANISOU   54  OH  TYR A   6      988   1099   1102    -75     51     70       O  
ATOM     55  OXT TYR A   6      11.788   2.976   7.592  1.00  8.07           O  
ANISOU   55  OXT TYR A   6      968   1126    971    -28    -50     10       O  
TER      56      TYR A   6                                                      
HETATM   57 ZN    ZN A   7      13.281   2.891   5.326  1.00  6.49          ZN  
ANISOU   57 ZN    ZN A   7      784    821    860    -54     33     -6      ZN  
HETATM   58  C   ACY A  10      14.386   5.403   6.406  1.00  6.91           C  
ANISOU   58  C   ACY A  10      850    912    865    -52    -40     25       C  
HETATM   59  O   ACY A  10      14.546   6.633   6.397  1.00  6.40           O  
ANISOU   59  O   ACY A  10      765    812    853     78    -65    147       O  
HETATM   60  OXT ACY A  10      13.622   4.803   5.620  1.00  6.13           O  
ANISOU   60  OXT ACY A  10      798    706    827   -127      2    -40       O  
HETATM   61  CH3 ACY A  10      15.156   4.626   7.437  1.00  7.00           C  
ANISOU   61  CH3 ACY A  10      882    946    832    -65    -43      6       C  
HETATM   62  O   HOH A   8     -10.505   1.596   3.027  1.00  5.62           O  
ANISOU   62  O   HOH A   8      682    921    531     -3     99    -98       O  
HETATM   63  O   HOH A   9     -10.556   4.008   1.574  1.00  5.67           O  
ANISOU   63  O   HOH A   9      664    744    747     78    -92    -79       O  
HETATM   64  O   HOH A  11      12.427   4.356   9.859  1.00 15.37           O  
ANISOU   64  O   HOH A  11     1819   2319   1702   -118      1      0       O  
HETATM   65  O   HOH A  12      13.627   6.646   9.636  1.00 25.87           O  
ANISOU   65  O   HOH A  12     3544   3345   2941   -113    -31    -55       O  
HETATM   66  O   HOH A  13       0.421   0.597  11.913  1.00 37.06           O  
ANISOU   66  O   HOH A  13     4669   4811   4602     -8    -10    -69       O  
HETATM   67  O   HOH A  14      -1.801  -0.217  12.498  1.00 39.09           O  
ANISOU   67  O   HOH A  14     4948   5054   4851    -31     83      1       O  
HETATM   68  O   HOH A  15      -1.057   2.545  11.391  1.00 49.99           O  
ANISOU   68  O   HOH A  15     6356   6344   6294    -19     13    -16       O  
CONECT   46   57                                                                
CONECT   55   57                                                                
CONECT   57   46   55   60                                                      
CONECT   58   59   60   61                                                      
CONECT   59   58                                                                
CONECT   60   57   58                                                           
CONECT   61   58                                                                
MASTER      253    0    2    0    0    0    2    6   67    1    7    1          
END                                                                             

mainwin4.py :


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MyApp(object):
    def setupUi(self, MyApp):
        MyApp.setObjectName("MyApp")
        MyApp.resize(786, 600)
        self.centralwidget = QtWidgets.QWidget(MyApp)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit_input = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_input.setGeometry(QtCore.QRect(220, 70, 301, 41))
        self.lineEdit_input.setClearButtonEnabled(False)
        self.lineEdit_input.setObjectName("lineEdit_input")
        self.pushButton_input = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_input.setGeometry(QtCore.QRect(550, 70, 171, 41))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setItalic(False)
        self.pushButton_input.setFont(font)
        self.pushButton_input.setCheckable(True)
        self.pushButton_input.setObjectName("pushButton_input")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 70, 151, 41))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setItalic(False)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
        self.treeWidget.setGeometry(QtCore.QRect(390, 170, 331, 391))
        self.treeWidget.setObjectName("treeWidget")
        self.treeWidget.headerItem().setText(0, "1")
        self.treeWidget.header().setVisible(True)
        self.treeView = QtWidgets.QTreeView(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(50, 170, 331, 391))
        self.treeView.setObjectName("treeView")
        self.treeView.header().setVisible(True)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(150, 140, 141, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(480, 140, 161, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        MyApp.setCentralWidget(self.centralwidget)

        self.retranslateUi(MyApp)
        QtCore.QMetaObject.connectSlotsByName(MyApp)

    def retranslateUi(self, MyApp):
        _translate = QtCore.QCoreApplication.translate
        MyApp.setWindowTitle(_translate("MyApp", "MyApp"))
        self.pushButton_input.setText(_translate("MyApp", "load"))
        self.label.setText(_translate("MyApp", "insert PDB code"))
        self.label_2.setText(_translate("MyApp", "        QTreeView"))
        self.label_3.setText(_translate("MyApp", "        QTreeWidget"))

main.py :

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, \
                            QTreeWidgetItem
                            
from PyQt5 import  QtGui, QtWidgets

from mainwin4 import Ui_MyApp

from Bio.PDB import PDBParser



class MyApp(QMainWindow, Ui_MyApp):
    
    def __init__(self, parent=None):
        super(MyApp, self).__init__(parent)
        
        self.setupUi(self)
        
        self.lineEdit_input.setMaxLength(4)
        self.lineEdit_input.setPlaceholderText("   4 characters PDB entry code")
        self.lineEdit_input.textChanged.connect(self.textchanged)
        
        self.pushButton_input.setEnabled(False)
        self.pushButton_input.clicked.connect(self.load_pdb)
        self.pushButton_input.setStyleSheet("background-color: %s " % '#fa6555') 
        
        
        self.model = None
        self.treeView.hide()
        # self.treeView.setHeaderHidden(True)
        
        self.treeWidget.hide()
        
    
    def textchanged(self):
        
        if len(self.lineEdit_input.text()) == 4:
            self.pushButton_input.setStyleSheet("background-color: %s " % '#9de650') 
            self.pushButton_input.setEnabled(True)
            self.treeView.show()
            
            self.treeWidget.show()
        
        else:
            self.pushButton_input.setStyleSheet("background-color: %s " % '#fa6555') 
            self.pushButton_input.setEnabled(False)
            
                        
            # TO CLEAR QTreeView
            
            if self.model != None : 
                # self.model.clear()       # works
                self.model.setRowCount(0)  # works 
                
            
            # TO CLEAR QTreeWidget
            # self.treeWidget.clear()   # works
            
            self.clearQTreeWidget(self.treeWidget) # works
            
            
            self.treeView.hide()
            
            self.treeWidget.hide()
        
    def load_pdb(self):
        
        self.pushButton_input.setEnabled(False)

        self. parser = PDBParser(PERMISSIVE=1, QUIET=1)
        
        try:
            print(self.lineEdit_input.text()+'.pdb')
            
            self.structure = self.parser.get_structure(self.lineEdit_input.text(), self.lineEdit_input.text()+'.pdb')
            
            print('load')
            
            # self.read_bfact()
            
            self.create_tree()
        
                
        except:
                
                self.msg = QMessageBox(self)
                self.msg.setIcon(QMessageBox.Critical)
                self.msg.setText("Error")
                self.msg.setInformativeText('no PDB found')
                self.msg.setDetailedText("details")
                self.msg.setWindowTitle("Error")
                self.msg.show()
                self.lineEdit_input.setText('')
            
            
    
    def create_tree(self):
        
        
        self.model = QtGui.QStandardItemModel()
        
        self.rootNode = self.model.invisibleRootItem()
        
        self.stru = QtGui.QStandardItem(self.structure.id)
        
        
        self.rootNode.appendRow(self.stru)
        
        self.treeView.setModel(self.model)
        
        # self.treeView.expandAll()
        
        for model in self.structure:
            
            self.mod = QtGui.QStandardItem(str(model.id+1))
            self.stru.appendRow(self.mod)
        
        
            for chain in model:
                
                self.cha = QtGui.QStandardItem(str(chain.id))
                self.mod.appendRow(self.cha)
                
                
                for residue in chain:
                    
                    self.res = QtGui.QStandardItem(str(residue.get_resname()))
                    self.cha.appendRow(self.res)
                    
                    for atom in residue:
                        
                        self.ato = QtGui.QStandardItem(str(atom.id +' '+ str(atom.get_serial_number())))  
                        self.res.appendRow(self.ato)
          
        
        # self.treeView.expandAll()
            
        self.items = []
        
        self.struct = QTreeWidgetItem([str(self.structure.id)])
        
        
        for model in self.structure:
            
            item = QTreeWidgetItem([str(model.id+1)])
            
            self.struct.addChild(item)
            
            
            for chain in model:
                
                child = QTreeWidgetItem([str(chain.id)])
                
                item.addChild(child)
                
                for residue in chain:
                    
                    child2 = QTreeWidgetItem([str(residue.get_resname())])
                    
                    child.addChild(child2)
                    
                    for atom in residue:
                        
                        child3 = QTreeWidgetItem([str(atom.id +' '+ str(atom.get_serial_number()))])
                        
                        child2.addChild(child3)
                        
        self.items.append(self.struct)
                
        self.treeWidget.insertTopLevelItems(0, self.items)  
        
        # self.treeWidget.expandAll()
    
    

    def clearQTreeWidget(self, tree):
        iterator = QtWidgets.QTreeWidgetItemIterator(tree, QtWidgets.QTreeWidgetItemIterator.All)
        while iterator.value():
            iterator.value().takeChildren()
            iterator +=1
        i = tree.topLevelItemCount()
        while i > -1:
            tree.takeTopLevelItem(i)
            i -= 1  


            

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()

    try:
        sys.exit(app.exec_())
    except SystemExit:
        print('Closing Window...')

我尝试使用 QTreeViewQTreeWidget 创建 PDB 对象的可视化,脚本在其同一目录中加载 pdb 文件(如果文件被调用则插入 1111 1111.pdb) .然后按下 LOAD 按钮可视化其结构,如果 PDB 进入代码输入 window 被取消,则两个可视化树将用此代码擦除:

            # TO CLEAR QTreeView
            
            if self.model != None : 
                # self.model.clear()       # works
                self.model.setRowCount(0)  # works 
                
            
            # TO CLEAR QTreeWidget
            # self.treeWidget.clear()   # works
            
            self.clearQTreeWidget(self.treeWidget) # works

不确定这是使用 QTreeViewQTreeWidget

的最佳方式

但至少我掌握了如何清除它们