开发者

Export data from qtablewidget to csv

开发者 https://www.devze.com 2023-01-31 22:24 出处:网络
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.

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-开发者_如何学Python>tableWidget->item( r, c )->text()+"\" ";
        }
        data << strList.join( ";" )+"\n";
    }
    f.close();
}


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";
0

精彩评论

暂无评论...
验证码 换一张
取 消