qt向tableview中加数据,如何在Qt中向QTableWidget添加带有数据的标题?

I'm still learning Qt and I am indebted to the SO community for providing me with great, very timely answers to my Qt questions. Thank you.

I'm quite confused on the idea of adding a header to a QTableWidget. What I'd like to do is have a table that contains information about team members. Each row for a member should contain his first and last name, each in its own cell, an email address in one cell, and office in the other cell. I'd to have a header above these columns to name them as appropriate.

I'm trying to start off easy and get just the header to display "Last" (as in last name). Here is my code.

int column = m_ui->teamTableWidget->columnCount();

m_ui->teamTableWidget->setColumnCount(column+1);

QString* qq = new QString("Last");

m_ui->teamTableWidget->horizontalHeader()->model()->setHeaderData(0, Qt::Horizontal, QVariant(QVariant::String, &qq));

My table gets rendered corretly, but the header doesn't contain what I would expect. It contains 1 cell that contains the text "1".

I am obviously doing something very silly here that is wrong, but i am lost. I keep pouring over the documentation, finding nothing. Here are the documentation links to the function calls I am making for the very last line.

Thanks for any and all help.

解决方案

At the request of the person who steered me toward the right place, I am posting the way I accomplished this as an answer and I am accepting it.

m_ui->teamTableWidget->setColumnCount(m_ui->teamTableWidget->columnCount()+1);

QTableWidgetItem* qtwi = new QTableWidgetItem(QString("Last"),QTableWidgetItem::Type);

m_ui->teamTableWidget->setHorizontalHeaderItem(0,qtwi);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值