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.
The easiest solution is setHorizontalHeaderLabels(myListOfHeaderLabels)
.
I see one potential problem, and also an easier way to do this.
First, the problem:
QString* qq = new QString("Last"); // <- qq is a pointer to a string.
m_ui->teamTableWidget->horizontalHeader()->model()->setHeaderData(0,
Qt::Horizontal,
QVariant(QVariant::String, &qq)); // <- You take the address of a pointer, or create a handle.
I think you want to do this instead:
QString* qq = new QString("Last");
m_ui->teamTableWidget->horizontalHeader()->model()->setHeaderData(0,
Qt::Horizontal, QVariant(QVariant::String, *qq));
Now, the easier way to set the data for a header item:
m_ui->teamTableWidget->horizontalHeaderItem( 0 )->setText( "Last" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With