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

前端 未结 4 1136
轻奢々
轻奢々 2020-12-18 12:28

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 t

相关标签:
4条回答
  • 2020-12-18 13:05

    For posterity:

    The default implimentations of setHeaderData() and headerData() in QAbstractItemModel do not do anything. You can (should?) (re)impliment headerData() in order to return useful a label.

    0 讨论(0)
  • 2020-12-18 13:18

    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" );
    
    0 讨论(0)
  • 2020-12-18 13:24

    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);
    
    0 讨论(0)
  • 2020-12-18 13:28

    The easiest solution is setHorizontalHeaderLabels(myListOfHeaderLabels).

    0 讨论(0)
提交回复
热议问题