本文介绍了将数据从qtablewidget导出到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据导出到csv(逗号分隔值)时遇到了一些问题.所有数据均已导出,但QTableWidget的标题和行名未导出.我需要列和行的标题.

I have a little problem with export data to csv (comma-separated values). All data was exported, but headers and name of rows from QTableWidget don't. I need headers of columns and rows.

您知道如何获取列的名称标题和行的名称吗?这是我的代码:

Do you have any idea how to get name headers of columns and name of rows?Here is my code:

QFile f( "money.csv" );

if (f.open(QFile::WriteOnly | QFile::Truncate))
{
    QTextStream data( &f );
    QStringList strList;

    for( int r = 0; r < ui->tableWidget->rowCount(); ++r )
    {
        strList.clear();
        for( int c = 0; c < ui->tableWidget->columnCount(); ++c )
        {
            strList << "\" "+ui->tableWidget->item( r, c )->text()+"\" ";
        }
        data << strList.join( ";" )+"\n";
    }
    f.close();
}

推荐答案

您可以使用 QTableWidget :: horizo​​ntalHeaderItem(int列),它返回列 column 的标题项.

You can use QTableWidget::horizontalHeaderItem(int column), which returns the header item for the column column.

QTableWidget * table = ui->tableWidget;

for( int c = 0; c < widget->columnCount(); ++c )
{
    strList <<
            "\" " +
            table->horizontalHeaderItem(c)->data(Qt::DisplayRole).toString() +
            "\" ";
}

data << strList.join(";") << "\n";

这篇关于将数据从qtablewidget导出到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 05:47