简体   繁体   中英

Add custom header view to table view

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM