Results 1 to 2 of 2

Thread: Copy text from QTableView

  1. #1
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Copy text from QTableView

    Hi,

    I try to copy selected data from a qtableview to clipboard, seperated with '\t' for columns and '\n' for rows.
    Its working just fine, but only when I select numbers, if I select text its screwed up and only the last item shows on pasting.

    I've overridden the keyPressEvent:
    Qt Code:
    1. void keyPressEvent(QKeyEvent *event)
    2. {
    3. if (event->matches(QKeySequence::Copy))
    4. {
    5. QAbstractItemModel *abmodel = this->model();
    6. QItemSelectionModel *model = this->selectionModel();
    7. QModelIndexList list = model->selectedIndexes();
    8.  
    9. qSort(list);
    10.  
    11. if(list.size() < 1)
    12. return;
    13.  
    14. QString copy_table;
    15. QModelIndex last = list.last();
    16. QModelIndex previous = list.first();
    17.  
    18. list.removeFirst();
    19.  
    20. for(int i = 0; i < list.size(); i++)
    21. {
    22. QVariant data = abmodel->data(previous);
    23. QString text = data.toString();
    24. QModelIndex index = list.at(i);
    25. copy_table.append(text);
    26.  
    27. if(index.row() != previous.row())
    28. copy_table.append('\n');
    29. else
    30. copy_table.append('\t');
    31. previous = index;
    32. }
    33.  
    34. copy_table.append(abmodel->data(list.last()).toString());
    35. copy_table.append('\n');
    36.  
    37. QClipboard *clipboard = QApplication::clipboard();
    38. clipboard->setText(copy_table);
    39. }
    40. QTableView::keyPressEvent(event);
    41. };
    To copy to clipboard, switch view to plain text mode 
    And the QString copy_table holds the correct text, but it isnt set correctly to the clipboard.

    To clarify
    Works:
    Qt Code:
    1. 230 26 26
    2. 303 26 26
    3. 528 17 17
    To copy to clipboard, switch view to plain text mode 
    Doesnt Work:
    Qt Code:
    1. 4602 4601 Orgrimmar Grunt
    2. 374 4639 Southsea Brigand
    3. 4638 4640 Southsea Cutthroat
    To copy to clipboard, switch view to plain text mode 
    In this case, only "Southsea Cutthroat" will be the content of the clipboard.
    And I really dont know why, somebody knows a solution for this?

  2. #2
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: Copy text from QTableView

    Got it!

    Something is happening with the clipboard in the QTableView::keyPressEvent if its a Copy Keysequence.

    Adding a:
    Qt Code:
    1. else
    2. QTableView::keyPressEvent(event);
    To copy to clipboard, switch view to plain text mode 
    at the end, so the copy sequence doesnt get forwarded to the qtableview keypressevent, resolved the problem.

    Still I'm curios, what happens with text at the keypressevent that doesnt happen with numbers?

Similar Threads

  1. Qwebview Copy Text And Save Image
    By ShapeShiftme in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2016, 06:30
  2. extra hard copy (as text) of QSettings
    By navid in forum Newbie
    Replies: 5
    Last Post: 5th December 2009, 14:05
  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. Problem with copy text to clipboard
    By weiching in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2008, 12:44
  5. copy string from text box
    By Rekha in forum Newbie
    Replies: 6
    Last Post: 27th July 2006, 19:21

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.