Search code examples
pythonpyqt5qtableview

view.setColumnHidden (0, True) is not working


I have this coding which is working fine. Except some tableview function don't

    try:
        model = QtGui.QStandardItemModel()
        view = QtWidgets.QTableView() 
        view.horizontalHeader().hide()
        view.verticalHeader().hide()
        view.setSelectionBehavior(1)
        view.setColumnHidden(0, True)            
        self.comboBox_2.setView(view)
        self.comboBox_2.setModel(model)
        data = helper.select("Select fahrerid, fahrervorname || fahrernachname from fahrer")            
        for row in data:
            items = []
            for item in row:
                items.append(QtGui.QStandardItem(str(item)))
            model.appendRow(items)
    except Exception as e:
        ErrorLogger.WriteError('Line 177: ' + str(e))
        QtWidgets.QMessageBox.critical(None, 'Exception raised', format(e))  

Only this is not working:

view.setColumnHidden (0, True)

Same with hide a column


Solution

  • The visibility of rows and columns can only be set when those rows and columns exist. Since that function is being called before setting the model (so there is no row/column count), that call is ignored.

    Move that line after setting the model on the view.