Results 1 to 4 of 4

Thread: copy tableview to Excel

  1. #1
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default copy tableview to Excel

    Hello,

    I want to copy data from of a QTableview to Excel, below I have an example of such a table.
    I tried several examples found on the internet, but I can't get it working.
    Can someone give me a working example?

    Regards,
    Arend


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QStandardItemModel"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. QStandardItemModel *model = new QStandardItemModel(3,3,this);
    9.  
    10. model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column1 Header")));
    11. model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column2 Header")));
    12.  
    13. QStandardItem *firstRow = new QStandardItem(QString("1"));
    14. QStandardItem *secondRow = new QStandardItem(QString("2"));
    15.  
    16. model->setItem(0,0,firstRow);
    17. model->setItem(1,0,secondRow);
    18.  
    19. ui->tableView->setModel(model);
    20. }
    21.  
    22.  
    23. MainWindow::~MainWindow()
    24. {
    25. delete ui;
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: copy tableview to Excel

    What have you tried so far?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: copy tableview to Excel

    So far I tried this.
    I want to use ctrl-c and ctrl-v when I have selected a column or two.

    Thanks for your help,
    Arend

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QStandardItemModel"
    4. #include "QAbstractItemModel"
    5. #include "QItemSelectionModel"
    6. #include "QKeyEvent"
    7. #include "QKeySequence"
    8. #include "QClipboard"
    9.  
    10. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. QStandardItemModel *model = new QStandardItemModel(3,3,this);
    14. model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column1 Header")));
    15. model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column2 Header")));
    16.  
    17. QStandardItem *firstRow = new QStandardItem(QString("1"));
    18. QStandardItem *secondRow = new QStandardItem(QString("2"));
    19.  
    20. model->setItem(0,0,firstRow);
    21. model->setItem(1,0,secondRow);
    22.  
    23. ui->tableView->setModel(model);
    24.  
    25. QAbstractItemModel *abmodel = ui.tableview->model();
    26. QItemSelectionModel *model = ui.tableview->selectionModel();
    27. QModelIndexList list = model->selectionIndexes();
    28. qSort(list);
    29. QModelIndex index = list.first();
    30. for(int i = 0; i < list.size(); i++)
    31. {
    32. QModelIndex index = list.at(i);
    33. QString text = abmodel->data(index).toString();
    34. copy_table.append(text);
    35.  
    36. if(index.row() != previous.row())
    37. {
    38. copy_table.append('\n');
    39. }
    40. else
    41. {
    42. copy_table.append('\t');
    43. }
    44.  
    45. previous = index;
    46. }
    47.  
    48. QClipboard *clipboard = QApplication::clipboard();
    49. clipboard->setText(copy_table);
    50.  
    51. }
    52.  
    53. MainWindow::~MainWindow()
    54. {
    55. delete ui;
    56. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 9th December 2012 at 21:56.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: copy tableview to Excel

    You need to use text/csv as the mime-type instead of text/plain which is used when you call QClipboard::setText(). Use QClipboard::setMimeData() instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. copy/move excel sheet with QT
    By gerardpuducul in forum Qt Programming
    Replies: 16
    Last Post: 4th December 2012, 09:25
  2. QSettings: copy from ini to xml? Copy group?
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2012, 14:51
  3. How to copy from Excel and paste to QTableView
    By sms in forum Qt Programming
    Replies: 5
    Last Post: 7th February 2009, 03:58
  4. Excel using Qt
    By nmn in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2007, 02:44
  5. Copy/Paste in TableView
    By bronte in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2006, 21:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.