Results 1 to 2 of 2

Thread: QTableWidget - Copy & Paste failed

  1. #1
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget - Copy & Paste failed

    Firstly, I am sorry for my poor english

    Here I subclass QTableWidget and name it TabelJo. Next, I implement 2 functions, copy and paste. You can see the codes below.

    Qt Code:
    1. void TabelJo::copy(){
    2. QTableWidgetSelectionRange range = selectedRange();
    3. QString str;
    4. for (int i = 0; i < range.rowCount(); ++i) {
    5. if (i > 0)
    6. str += "\n";
    7. for (int j = 0; j < range.columnCount(); ++j) {
    8. if (j > 0)
    9. str += "\t";
    10. str += getText( range.topRow() + i, range.leftColumn() + j);
    11. }
    12. }
    13. str += "\n";
    14. QApplication::clipboard()->setText(str);
    15. qDebug() << QApplication::clipboard()->text();
    16. }
    17.  
    18. void TabelJo::paste(){
    19. QString str = QApplication::clipboard()->text();
    20. qDebug() << QApplication::clipboard()->text();
    21. QStringList rows = str.split('\n');
    22. int numRows = rows.count()-1;
    23. int numColumns = rows.first().count('\t') + 1;
    24.  
    25. qDebug() << numRows << " " << numColumns ;
    26. for (int i = 0; i < numRows; ++i) {
    27. QStringList columns = rows[i].split('\t');
    28. for (int j = 0; j < numColumns; ++j) {
    29. setText(currentRow()+i,currentColumn()+j,columns[j],Qt::AlignLeft | Qt::AlignVCenter);
    30. }
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    Consider I select these items on my table:
    | a | a1 | a2 |
    | b | b2 | b3 |
    then I copy them.

    The code on line 14 execute successfully.
    Line 15 give this result:
    "a a1 a2
    b b2 b3
    "
    maybe this form will have a better look "a\ta1\ta2\nb\tb2\b3\n"

    The problem is when I try to paste them, the code on line 20 only give this result:
    "b3"

    I use win7 and Qt 4.7.0 (32 bit)

    Could anyone help me to solve this problem? I'm really stack here.
    Last edited by rivci; 28th December 2010 at 04:23.

  2. #2
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget - Copy & Paste failed

    I just realize that QTableWidget has its default copy function (Ctrl+C). The next question is, how to overwrite this function?


    Added after 1:


    my bad

    Qt Code:
    1. void TabelJo::keyPressEvent(QKeyEvent *event){
    2. if( event==QKeySequence::Delete )
    3. del();
    4. else if( event->matches( QKeySequence::Copy ) )
    5. copy();
    6. else if( event->matches( QKeySequence::Paste ) )
    7. paste();
    8. QTableWidget::keyPressEvent(event);
    9. }
    To copy to clipboard, switch view to plain text mode 

    I forget to place "else" at the last "if"
    the code should be like this

    Qt Code:
    1. void TabelJo::keyPressEvent(QKeyEvent *event){
    2. if( event==QKeySequence::Delete )
    3. del();
    4. else if( event->matches( QKeySequence::Copy ) )
    5. copy();
    6. else if( event->matches( QKeySequence::Paste ) )
    7. paste();
    8. else
    9. QTableWidget::keyPressEvent(event);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rivci; 4th January 2011 at 09:38.

Similar Threads

  1. Does Qt Support copy and paste options with the QTableWidget
    By kapoorsudhish in forum Qt Programming
    Replies: 0
    Last Post: 27th October 2009, 04:59
  2. copy and paste
    By hubipet in forum Qt Tools
    Replies: 1
    Last Post: 17th July 2009, 06:19
  3. Copy-Paste files
    By Lele in forum Qt Programming
    Replies: 11
    Last Post: 4th April 2008, 14:15
  4. using cut(), copy(), paste()
    By systemz89 in forum Newbie
    Replies: 5
    Last Post: 18th December 2007, 14:47
  5. Replies: 8
    Last Post: 10th November 2006, 09:39

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.