⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

How to ensure that an HTML-table in QTextEdit is copied as a table when pasting into for instance Excel?

  • We use a QTextEdit instance to display some results of calculations in tabular form. We would like to be able to copy such tables and paste them into for instance Excel.

    In our current (naive) implementation each value from the HTML table, becomes a separate row in Excel. How can we do better?

  • Moderators

    How do you create the drop data?
    When you simply create text/plain i guess it will become a single row/cell.
    Probably making sure, that you create text/html mimedata excel will paste them differently.
    But i am not sure.

  • @raven-worx Thanx for reading and taking some time to help me out.

    Here with 'copying' I simply mean selecting the relevant text in the QTextEdit and hitting Ctrl+C.

  • Moderators

    what happens if you do this?

    QMimeData* MyTextEdit::createMimeDataFromSelection()
         QMimeData* origData = QTextEdit::createMimeDataFromSelection();
         QMimeData* data = new QMimeData;
             data->setData( "text/html", origData->data("Text/html") )
         delete origData;
         retrun Data;

  • @raven-worx Thanx for the suggestion. I think we can go a long way with this, except that we are on Windows and it seems we need some Windows-specific MIME-conversions. We'll try to tinker a bit with that.