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

问题描述:

我仍然在学习Qt,我感谢SO社区为我提供了很好的,非常及时的答案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.

我对添加一个标题到QTableWidget的想法很困惑。我想做的是有一个包含团队成员信息的表。成员的每一行都应包含其姓和名,每个在其自己的单元格中,一个单元格中的电子邮件地址,以及另一个单元格中的office。

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.

我试图从头开始,只获取标题显示Last作为姓氏)。这是我的代码。

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));

我的表被正确渲染,但头不包含我的期望。它包含1个包含文本1的单元格。

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#horizo​​ntalHeader
http://doc.trolltech.com/4.5/qabstractitemview.html#model
http://doc.trolltech.com/4.5/qabstractitemmodel.html#setHeaderData

感谢您的帮助。

地方,我发布的方式我完成了这个答案,我接受它。

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);