How do I add a header with data to a QTableWidget in Qt?
Posted
by San Jacinto
on Stack Overflow
See other posts from Stack Overflow
or by San Jacinto
Published on 2009-11-24T21:28:31Z
Indexed on
2010/03/08
6:36 UTC
Read the original article
Hit count: 391
Hi,
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.
http://doc.trolltech.com/4.5/qtableview.html#horizontalHeader http://doc.trolltech.com/4.5/qabstractitemview.html#model http://doc.trolltech.com/4.5/qabstractitemmodel.html#setHeaderData
Thanks for any and all help.
Edit: HOW I SOLVED THE PROBLEM
Using some help from the accepted answer, I came up with the following code:
m_ui->teamTableWidget->setColumnCount(m_ui->teamTableWidget->columnCount()+1); QTableWidgetItem* qtwi = new QTableWidgetItem(QString("Last"),QTableWidgetItem::Type); m_ui->teamTableWidget->setHorizontalHeaderItem(0,qtwi);
© Stack Overflow or respective owner