Results 1 to 11 of 11

Thread: Copy row(s) from QTableWidget

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Copy row(s) from QTableWidget

    I've seen similar questions on this subject, but not this exact question. But apologies if I missed it.

    I want to copy either single cell, or single row, multiple rows from a QTableWidget and Paste into Excel, Notepad, etc. The way I have it now, if I copy a single row and try to paste into Excel, it pastes the data as one continous string in the first cell. Basically does the same thing in Notepad... one continous string. If I select multiple rows it still pastes as a single string but it pastes it by column.
    For example, if this were the data in my table widget and I selected both rows and copied

    name number1 data1
    name2 number2 data2

    it would paste this way:

    namename2number1number2data1data2


    What is the best approach here? TIA.


    Qt Code:
    1. ////////////////////////////////////////////////////////////////////////////////
    2. void cDisplayDlg::on_mTableWidget_itemSelectionChanged()
    3. {
    4. QList<QTableWidgetItem *> selected(mTableWidget->selectedItems());
    5.  
    6. mByteArray.clear();
    7. foreach(item, selected)
    8. {
    9. mByteArray.append(item->text());
    10.  
    11. }
    12. qDebug() << "mByteArray " << mByteArray.size();
    13.  
    14. } // cDisplayDlg::on_itemSelectionChanged
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ////////////////////////////////////////////////////////////////////////////////
    2. void cDisplayDlg::keyPressEvent(QKeyEvent * event)
    3. {
    4. //if there is a control-C event copy data to the global clipboard
    5. if(event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier)
    6. {
    7. QMimeData * mimeData = new QMimeData();
    8. mimeData->setData("text/plain",mByteArray);
    9. QApplication::clipboard()->setMimeData(mimeData);
    10. }
    11. } // cDisplayDlg::keyPressEvent
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy row(s) from QTableWidget

    Append a tab(or comma) after each word. This way Excel will now how to split them:
    Qt Code:
    1. foreach(item, selected)
    2. {
    3. mByteArray.append(item->text());
    4. mByteArray.append("\t");
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Copy row(s) from QTableWidget

    I thought it worked out of box. Doesn't it?
    J-P Nurmi

  4. #4
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copy row(s) from QTableWidget

    Quote Originally Posted by marcel View Post
    Append a tab(or comma) after each word. This way Excel will now how to split them:
    Qt Code:
    1. foreach(item, selected)
    2. {
    3. mByteArray.append(item->text());
    4. mByteArray.append("\t");
    5. }
    To copy to clipboard, switch view to plain text mode 


    Marcel, this worked great.... but for only one row at a time. I still have an issue with copying multiple rows at a time. My ByteArray gets loaded up column major instead of row major, so that whenever I copy and paste multiple rows, 1 - it pastes everything on the same row and 2 - it pastes column major. Any ideas?

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy row(s) from QTableWidget

    Separate rows by "\r\n". Just like in a CSV file, only here you don't have commas, but tabs.

    Regards

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy row(s) from QTableWidget

    Quote Originally Posted by jpn View Post
    I thought it worked out of box. Doesn't it?
    The cells were not separated by anything. They looked like a contiguous string.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy row(s) from QTableWidget

    Quote Originally Posted by jpn View Post
    I thought it worked out of box. Doesn't it?
    The default implementation copies only the current cell, not the whole selection.

  8. #8
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copy row(s) from QTableWidget

    Quote Originally Posted by marcel View Post
    Separate rows by "\r\n". Just like in a CSV file, only here you don't have commas, but tabs.

    Regards
    Great. (Hopefully) Last question. How is the best way to set up the loop to put those ("\r\n") separaters in? I should probably know this

    TIA.

  9. #9
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Copy row(s) from QTableWidget

    I would add the line end after the foreach loop and see if Excel understands it.

    Qt Code:
    1. foreach(item, selected)
    2. {
    3. mByteArray.append(item->text());
    4. mByteArray.append("\t");
    5. }
    6.  
    7. mByteArray.append("\r\n");
    To copy to clipboard, switch view to plain text mode 
    There are 10 people in the world. Those who understand binary and those who don't.

  10. #10
    Join Date
    Jan 2017
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Copy row(s) from QTableWidget

    I have this code written in qt c++. I need to copy the entire row
    For example Date Customer Server

    How can i paste this entire row . Write now it is selecting only onecell ho can entire row can be selected and copied to notepad?
    Here tblLog is My tableview. Please help?

    Qt Code:
    1. void OTPWindow::initLogTable()
    2. {
    3.  
    4. QList<OtpLog> logs;
    5. int ret = otpRepo.fetchOtpLogs(logs);
    6. if( ret != errorCodes::SUCCESS )
    7. {
    8. //todo: error message
    9. QMessageBox msgBox(QMessageBox::Critical, QString("SafeOTP"),
    10. QString("OTPLogs could not be fetched"),QMessageBox::Ok, this);
    11. msgBox.exec();
    12. QLOG_ERROR() << "fetchLogs error " << ret;
    13. return;
    14. }
    15.  
    16. QStandardItemModel *model = new QStandardItemModel(0,5,this); //5 columns
    17. model->setHorizontalHeaderItem(0, new QStandardItem(QString("Date")));
    18. model->setHorizontalHeaderItem(1, new QStandardItem(QString("Customer")));
    19. model->setHorizontalHeaderItem(2, new QStandardItem(QString("Server")));
    20. model->setHorizontalHeaderItem(3, new QStandardItem(QString("Authorized by")));
    21. model->setHorizontalHeaderItem(4, new QStandardItem(QString("Description")));
    22.  
    23.  
    24. for(QList<OtpLog>::Iterator lIt = logs.begin(); lIt != logs.end(); lIt++)
    25. {
    26. OtpLog& log = *lIt;
    27. QList<QStandardItem*> row;
    28. row.push_back(new QStandardItem(log.when.toString("dd MMM yyyy, hh:mm")));
    29. row.push_back(new QStandardItem(QString(log.customer)));
    30. row.push_back(new QStandardItem(QString(log.server)));
    31. row.push_back(new QStandardItem(QString(log.author)));
    32. row.push_back(new QStandardItem(QString(log.reason)));
    33.  
    34. model->appendRow(row);
    35. }
    36. // set the data model
    37. ui->tblLog->setModel(model);
    38. ui->tblLog->setColumnHidden(1, true);
    39.  
    40.  
    41. // set the column widths
    42. int tw = ui->tblLog->width() - 5;
    43. int w = tw / 6;
    44.  
    45. for(int i=0; i<4;i++)
    46. {
    47. ui->tblLog->setColumnWidth(i,w);
    48. tw -= w;
    49. }
    50. ui->tblLog->setColumnWidth(4,tw);
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 1st February 2017 at 08:56. Reason: missing [code] tags

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Copy row(s) from QTableWidget

    Quote Originally Posted by saylijawale View Post
    How can i paste this entire row . Write now it is selecting only onecell ho can entire row can be selected and copied to notepad?
    You must have forgotten to post the code that does your copying.

    Cheers,
    _

Similar Threads

  1. Removing rows
    By indifference in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2007, 16:54
  2. QTableWidget swapping rows with widgets in cells
    By dnnc in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2007, 14:11
  3. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  4. Replies: 1
    Last Post: 28th September 2006, 06:21
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.