spacepaste

  1.  
  2. import sys
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtCore import *
  5. class MainWindow(QMainWindow):
  6. def __init__(self):
  7. super(self.__class__, self).__init__()
  8. frame=QWidget()
  9. self.setCentralWidget(frame)
  10. hl=QVBoxLayout()
  11. frame.setLayout(hl)
  12. self.tree=QTreeWidget(self)
  13. hl.addWidget(self.tree)
  14. # add treewidgetitems
  15. for ii in range(5):
  16. item=QTreeWidgetItem([str(ii),])
  17. self.tree.addTopLevelItem(item)
  18. self.tree.itemDoubleClicked.connect(self.rename)
  19. self.tree.itemChanged.connect(self.checkString)
  20. self.show()
  21. def rename(self):
  22. item=self.tree.selectedItems()
  23. if item:
  24. item=item[0]
  25. item.setFlags(item.flags() | Qt.ItemIsEditable)
  26. self.tree.scrollToItem(item)
  27. self.tree.editItem(item)
  28. def checkString(self,item,column):
  29. text=item.data(0,column)
  30. print('text',text)
  31. invalid=['.', ':', '!']
  32. accepted=True
  33. for ii in invalid:
  34. if ii in text:
  35. accepted=False
  36. break
  37. if not accepted:
  38. print('invalid name')
  39. self.tree.editItem(item)
  40. if __name__ == "__main__":
  41. app = QApplication(sys.argv)
  42. form = MainWindow()
  43. form.show()
  44. sys.exit(app.exec_())
  45.