PDA

View Full Version : Copy row(s) from QTableWidget



allensr
3rd January 2008, 23:54
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.



////////////////////////////////////////////////////////////////////////////////
void cDisplayDlg::on_mTableWidget_itemSelectionChanged( )
{
QList<QTableWidgetItem *> selected(mTableWidget->selectedItems());
QTableWidgetItem *item;

mByteArray.clear();
foreach(item, selected)
{
mByteArray.append(item->text());

}
qDebug() << "mByteArray " << mByteArray.size();

} // cDisplayDlg::on_itemSelectionChanged




////////////////////////////////////////////////////////////////////////////////
void cDisplayDlg::keyPressEvent(QKeyEvent * event)
{
//if there is a control-C event copy data to the global clipboard
if(event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier)
{
QMimeData * mimeData = new QMimeData();
mimeData->setData("text/plain",mByteArray);
QApplication::clipboard()->setMimeData(mimeData);
}
} // cDisplayDlg::keyPressEvent

marcel
4th January 2008, 00:32
Append a tab(or comma) after each word. This way Excel will now how to split them:


foreach(item, selected)
{
mByteArray.append(item->text());
mByteArray.append("\t");
}

jpn
4th January 2008, 08:38
I thought it worked out of box. Doesn't it?

allensr
4th January 2008, 15:29
Append a tab(or comma) after each word. This way Excel will now how to split them:


foreach(item, selected)
{
mByteArray.append(item->text());
mByteArray.append("\t");
}




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?

marcel
4th January 2008, 15:32
Separate rows by "\r\n". Just like in a CSV file, only here you don't have commas, but tabs.

Regards

marcel
4th January 2008, 15:33
I thought it worked out of box. Doesn't it?
The cells were not separated by anything. They looked like a contiguous string.

jacek
4th January 2008, 15:54
I thought it worked out of box. Doesn't it?
The default implementation copies only the current cell, not the whole selection.

allensr
4th January 2008, 17:08
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 :o

TIA.

Thomas
4th January 2008, 22:28
I would add the line end after the foreach loop and see if Excel understands it.


foreach(item, selected)
{
mByteArray.append(item->text());
mByteArray.append("\t");
}

mByteArray.append("\r\n");

saylijawale
30th January 2017, 11:17
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?



void OTPWindow::initLogTable()
{

QList<OtpLog> logs;
int ret = otpRepo.fetchOtpLogs(logs);
if( ret != errorCodes::SUCCESS )
{
//todo: error message
QMessageBox msgBox(QMessageBox::Critical, QString("SafeOTP"),
QString("OTPLogs could not be fetched"),QMessageBox::Ok, this);
msgBox.exec();
QLOG_ERROR() << "fetchLogs error " << ret;
return;
}

QStandardItemModel *model = new QStandardItemModel(0,5,this); //5 columns
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Date")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Customer")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Server")));
model->setHorizontalHeaderItem(3, new QStandardItem(QString("Authorized by")));
model->setHorizontalHeaderItem(4, new QStandardItem(QString("Description")));


for(QList<OtpLog>::Iterator lIt = logs.begin(); lIt != logs.end(); lIt++)
{
OtpLog& log = *lIt;
QList<QStandardItem*> row;
row.push_back(new QStandardItem(log.when.toString("dd MMM yyyy, hh:mm")));
row.push_back(new QStandardItem(QString(log.customer)));
row.push_back(new QStandardItem(QString(log.server)));
row.push_back(new QStandardItem(QString(log.author)));
row.push_back(new QStandardItem(QString(log.reason)));

model->appendRow(row);
}
// set the data model
ui->tblLog->setModel(model);
ui->tblLog->setColumnHidden(1, true);


// set the column widths
int tw = ui->tblLog->width() - 5;
int w = tw / 6;

for(int i=0; i<4;i++)
{
ui->tblLog->setColumnWidth(i,w);
tw -= w;
}
ui->tblLog->setColumnWidth(4,tw);
}

anda_skoa
1st February 2017, 08:59
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,
_