Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

c++ - How do I add a header with data to a QTableWidget in Qt?

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.

Thanks for any and all help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easiest solution is setHorizontalHeaderLabels(myListOfHeaderLabels).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...