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
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