I want to create a custom header view and add it to a table view using model. This is my approach:
QStandardItemModel * s= new QStandardItemModel(this);
s->setHeaderData(0, Qt::Horizontal, "Header 1", Qt::DisplayRole);
s->setHeaderData(1, Qt::Horizontal, "Header 2", Qt::DisplayRole);
s->setHeaderData(2, Qt::Horizontal, "Header 3", Qt::DisplayRole);
QHeaderView * p = new QHeaderView(ui->tableView);
p->setModel(s);
ui->tableView->setHorizontalHeader(p);
ui->tableView->show();
But the header doesn't appear in the table. I have also tried
ui->tableView->horizontalHeader()->setModel(s);
but no result.
The column count is missing in your example that's why it's not showing header. Use s->setColumnCount(3)
in your code. For more information read this.