Important: Please read the Qt Code of Conduct -

Copy QTableWidget cells to Excell

  • Hi all,

    Is there any way to copy a whole table (QTableWidget) and paste it in Excell?
    Should I use copy to clipboard with ctrl+c or something else?

    Thank you in advance

  • Lifetime Qt Champion


    What did you try ?

    One thing that comes to mind: you can copy to the clipboard your table content as a CSV string. Warning not tested, it just a suggestion.

  • Lifetime Qt Champion

    Expanding on @SGaist , if you loop over the items and construct a string with \t between
    the cols values for each row then you can paste it directly.

    alt text

  • Thank you bot h for your responses. I ended up with a qstring, a qtextstream and a for loop, as below (for a table with 8 columns):

    QClipboard *clipboard = QApplication::clipboard();

    int tbl_lines = ui->tbl->rowCount();

    QString str;

    for (int i=0; i<tbl_lines; i++)

    QString mydata0 = ui->tbl->item(i, 0)->text();
    QString mydata1 = ui->tbl->item(i, 1)->text();
    QString mydata2 = ui->tbl->item(i, 2)->text();
    QString mydata3 = ui->tbl->item(i, 3)->text();
    QString mydata4 = ui->tbl->item(i, 4)->text();
    QString mydata5 = ui->tbl->item(i, 5)->text();
    QString mydata6 = ui->tbl->item(i, 6)->text();
    QString mydata7 = ui->tbl->item(i, 7)->text();

    QTextStream(&str) << mydata0 << "\t" << mydata1 << "\t"<< mydata2 << "\t" << mydata3 <<"\t"<< mydata4 << "\t" << mydata5 << "\t"<< mydata6 << "\t" << mydata7 << endl;

    it might be kind of silly my approach, but it works! Thanks again!

Log in to reply