Sophus
23rd July 2017, 22:41
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.
12522
But somehow I don't manage to hide the first column. Whats wrong? I also tried with hideSection()-method, but it doesn't work.
import sys
from PyQt4.QtCore import Qt, QVariant
from PyQt4.QtGui import QApplication, QStandardItemModel, QStandardItem, QTreeView, QComboBox, QDialog, \
QVBoxLayout, QPushButton
class MyCustomDialog(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.standard_item_model = QStandardItemModel(0,2, self)
self.set_header_data(list_header_data = ["ID", "Data"])
self.items = [
["0", 'Cookie dough'],
["1", 'Hummus'],
["2", 'Spaghetti'],
["3", 'Dal makhani'],
["4", 'Chocolate whipped cream']
]
self.tree_view = QTreeView(self)
self.tree_view.setRootIsDecorated(False)
self.tree_view.header().hide()
#self.tree_view.header().hideSection(0)
#self.tree_view.setColumnHidden(0, True)
self.combo_box = QComboBox(self)
self.combo_box.setView(self.tree_view)
self.combo_box.setModel(self.standard_item_model)
self.push_button_load = QPushButton(self)
self.push_button_load.setText("Populate in TreeView")
self.push_button_hide_column = QPushButton(self)
self.push_button_hide_column.setText("Hide first column")
layout = QVBoxLayout(self)
layout.addWidget(self.combo_box)
layout.addWidget(self.push_button_load)
layout.addWidget(self.push_button_hide_column)
self.setLayout(layout)
self.selection_changed()
self.init_signal_clicked_push_button()
def hide_column(self):
#self.tree_view.header().hide()
#self.tree_view.header().hideSection(0)
self.tree_view.setColumnHidden(0, True)
def selection_changed(self):
self.tree_view.selectionModel().selectionChanged.c onnect(lambda new_index:
self.get_id_tree_view(new_index=new_index, tree_view_object=self.tree_view))
def generator_header_data(self, list_header):
for header_data in list_header:
yield header_data
def set_header_data(self, list_header_data=None):
count_column = 0
for header_data in self.generator_header_data(list_header_data):
self.standard_item_model.setHeaderData(count_colum n, Qt.Horizontal, header_data)
count_column +=1
return
def populate_data_item(self, tuple_items):
count_items = len(tuple_items)
if count_items == 2:
item_first, item_second = tuple_items
two_columns_item = [QStandardItem(str(item_first)), QStandardItem(item_second)]
self.standard_item_model.appendRow(two_columns_ite m)
return
def get_id_tree_view(self, new_index=None,
tree_view_object=None,):
try:
if not new_index is None:
index = new_index.indexes()[0].data()#.toPyObject()
if isinstance(index, QVariant):
print "index", index.toString()
except IndexError as InErr:
print "InErr", InErr
def populate(self):
for i in self.items:
self.populate_data_item(i)
def init_signal_clicked_push_button(self):
self.push_button_load.clicked.connect(self.populat e)
self.push_button_hide_column.clicked.connect(self. hide_column)
def main():
app = QApplication(sys.argv)
window = MyCustomDialog()
window.resize(300, 50)
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
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.
12522
But somehow I don't manage to hide the first column. Whats wrong? I also tried with hideSection()-method, but it doesn't work.
import sys
from PyQt4.QtCore import Qt, QVariant
from PyQt4.QtGui import QApplication, QStandardItemModel, QStandardItem, QTreeView, QComboBox, QDialog, \
QVBoxLayout, QPushButton
class MyCustomDialog(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.standard_item_model = QStandardItemModel(0,2, self)
self.set_header_data(list_header_data = ["ID", "Data"])
self.items = [
["0", 'Cookie dough'],
["1", 'Hummus'],
["2", 'Spaghetti'],
["3", 'Dal makhani'],
["4", 'Chocolate whipped cream']
]
self.tree_view = QTreeView(self)
self.tree_view.setRootIsDecorated(False)
self.tree_view.header().hide()
#self.tree_view.header().hideSection(0)
#self.tree_view.setColumnHidden(0, True)
self.combo_box = QComboBox(self)
self.combo_box.setView(self.tree_view)
self.combo_box.setModel(self.standard_item_model)
self.push_button_load = QPushButton(self)
self.push_button_load.setText("Populate in TreeView")
self.push_button_hide_column = QPushButton(self)
self.push_button_hide_column.setText("Hide first column")
layout = QVBoxLayout(self)
layout.addWidget(self.combo_box)
layout.addWidget(self.push_button_load)
layout.addWidget(self.push_button_hide_column)
self.setLayout(layout)
self.selection_changed()
self.init_signal_clicked_push_button()
def hide_column(self):
#self.tree_view.header().hide()
#self.tree_view.header().hideSection(0)
self.tree_view.setColumnHidden(0, True)
def selection_changed(self):
self.tree_view.selectionModel().selectionChanged.c onnect(lambda new_index:
self.get_id_tree_view(new_index=new_index, tree_view_object=self.tree_view))
def generator_header_data(self, list_header):
for header_data in list_header:
yield header_data
def set_header_data(self, list_header_data=None):
count_column = 0
for header_data in self.generator_header_data(list_header_data):
self.standard_item_model.setHeaderData(count_colum n, Qt.Horizontal, header_data)
count_column +=1
return
def populate_data_item(self, tuple_items):
count_items = len(tuple_items)
if count_items == 2:
item_first, item_second = tuple_items
two_columns_item = [QStandardItem(str(item_first)), QStandardItem(item_second)]
self.standard_item_model.appendRow(two_columns_ite m)
return
def get_id_tree_view(self, new_index=None,
tree_view_object=None,):
try:
if not new_index is None:
index = new_index.indexes()[0].data()#.toPyObject()
if isinstance(index, QVariant):
print "index", index.toString()
except IndexError as InErr:
print "InErr", InErr
def populate(self):
for i in self.items:
self.populate_data_item(i)
def init_signal_clicked_push_button(self):
self.push_button_load.clicked.connect(self.populat e)
self.push_button_hide_column.clicked.connect(self. hide_column)
def main():
app = QApplication(sys.argv)
window = MyCustomDialog()
window.resize(300, 50)
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()