Hey guys,

I am programming in Python, but I hope anybody can help. PyQt is a binding (API) and works with Qt-technology. Well, in the following code you can see that I am working with QTreeView(), wich I add to QComboBox() by using the setView()-method. That works fine. But, I also want certain columns to be hidden, by using the setColumnHidden()-method. In my case, the first (in Python language, its zero) column should be hidden and the second column (in Python language, its one) should remain visible. I tried it, but it doesn't work. Can aynbody help me? Now I can hide the QHeaderView(). Look here.

uYqtp.jpg

But somehow I don't manage to hide the first column. Whats wrong? I also tried with hideSection()-method, but it doesn't work.


Qt Code:
  1. import sys
  2. from PyQt4.QtCore import Qt, QVariant
  3.  
  4. class MyCustomDialog(QDialog):
  5. def __init__(self, parent=None):
  6. QDialog.__init__(self, parent)
  7.  
  8. self.standard_item_model = QStandardItemModel(0,2, self)
  9. self.set_header_data(list_header_data = ["ID", "Data"])
  10.  
  11. self.items = [
  12. ["0", 'Cookie dough'],
  13. ["1", 'Hummus'],
  14. ["2", 'Spaghetti'],
  15. ["3", 'Dal makhani'],
  16. ["4", 'Chocolate whipped cream']
  17. ]
  18.  
  19. self.tree_view = QTreeView(self)
  20. self.tree_view.setRootIsDecorated(False)
  21. self.tree_view.header().hide()
  22. #self.tree_view.header().hideSection(0)
  23. #self.tree_view.setColumnHidden(0, True)
  24.  
  25. self.combo_box = QComboBox(self)
  26.  
  27. self.combo_box.setView(self.tree_view)
  28. self.combo_box.setModel(self.standard_item_model)
  29.  
  30. self.push_button_load = QPushButton(self)
  31. self.push_button_load.setText("Populate in TreeView")
  32.  
  33. self.push_button_hide_column = QPushButton(self)
  34. self.push_button_hide_column.setText("Hide first column")
  35.  
  36. layout = QVBoxLayout(self)
  37. layout.addWidget(self.combo_box)
  38. layout.addWidget(self.push_button_load)
  39. layout.addWidget(self.push_button_hide_column)
  40. self.setLayout(layout)
  41.  
  42. self.selection_changed()
  43. self.init_signal_clicked_push_button()
  44.  
  45. def hide_column(self):
  46. #self.tree_view.header().hide()
  47. #self.tree_view.header().hideSection(0)
  48. self.tree_view.setColumnHidden(0, True)
  49.  
  50. def selection_changed(self):
  51. self.tree_view.selectionModel().selectionChanged.connect(lambda new_index:
  52. self.get_id_tree_view(new_index=new_index, tree_view_object=self.tree_view))
  53.  
  54.  
  55. def generator_header_data(self, list_header):
  56.  
  57. for header_data in list_header:
  58. yield header_data
  59.  
  60. def set_header_data(self, list_header_data=None):
  61.  
  62. count_column = 0
  63.  
  64. for header_data in self.generator_header_data(list_header_data):
  65. self.standard_item_model.setHeaderData(count_column, Qt.Horizontal, header_data)
  66.  
  67. count_column +=1
  68.  
  69. return
  70.  
  71. def populate_data_item(self, tuple_items):
  72.  
  73. count_items = len(tuple_items)
  74.  
  75. if count_items == 2:
  76.  
  77. item_first, item_second = tuple_items
  78.  
  79. two_columns_item = [QStandardItem(str(item_first)), QStandardItem(item_second)]
  80.  
  81. self.standard_item_model.appendRow(two_columns_item)
  82.  
  83. return
  84.  
  85. def get_id_tree_view(self, new_index=None,
  86. tree_view_object=None,):
  87.  
  88. try:
  89. if not new_index is None:
  90.  
  91. index = new_index.indexes()[0].data()#.toPyObject()
  92.  
  93. if isinstance(index, QVariant):
  94. print "index", index.toString()
  95.  
  96. except IndexError as InErr:
  97. print "InErr", InErr
  98.  
  99. def populate(self):
  100. for i in self.items:
  101. self.populate_data_item(i)
  102.  
  103.  
  104.  
  105. def init_signal_clicked_push_button(self):
  106.  
  107. self.push_button_load.clicked.connect(self.populate)
  108.  
  109. self.push_button_hide_column.clicked.connect(self.hide_column)
  110.  
  111.  
  112. def main():
  113. app = QApplication(sys.argv)
  114. window = MyCustomDialog()
  115. window.resize(300, 50)
  116. window.show()
  117. sys.exit(app.exec_())
  118.  
  119. if __name__ == "__main__":
  120. main()
To copy to clipboard, switch view to plain text mode