将数据从 qtablewidget 导出到 csv

发布于 2024-10-08 09:55:37 字数 630 浏览 0 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-10-15 09:55:37

您可以使用 QTableWidget::horizo​​ntalHeaderItem(int 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";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文