qtableview不选中_已选中QTableView中的行/行复制到QClipboard

这个博客介绍了如何为QTableWidget添加复制和粘贴功能,包括实现代码和关键方法,如`copy()`和`paste()`,使得用户可以通过键盘快捷键进行单元格数据的拷贝和粘贴操作。
摘要由CSDN通过智能技术生成

我有一个类似的问题,并最终调整QTableWidget(这是QTableView的扩展)来添加复制/粘贴功能。以下是基于上述夸克提供的代码:

qtablewidgetwithcopypaste。^ h

// QTableWidget with support for copy and paste added

// Here copy and paste can copy/paste the entire grid of cells

#ifndef QTABLEWIDGETWITHCOPYPASTE_H

#define QTABLEWIDGETWITHCOPYPASTE_H

#include

#include

#include

class QTableWidgetWithCopyPaste : public QTableWidget

{

Q_OBJECT

public:

QTableWidgetWithCopyPaste(int rows, int columns, QWidget *parent = 0) :

QTableWidget(rows, columns, parent)

{}

QTableWidgetWithCopyPaste(QWidget *parent = 0) :

QTableWidget(parent)

{}

private:

void copy();

void paste();

public slots:

void keyPressEvent(QKeyEvent * event);

};

#endif // QTABLEWIDGETWITHCOPYPASTE_H

qtablewidgetwithcopypaste.cpp

#include "qtablewidgetwithcopypaste.h"

#include

#include

#include

#include

void QTableWidgetWithCopyPaste::copy()

{

QItemSelectionModel * selection = selectionModel();

QModelIndexList indexes = selection->selectedIndexes();

if(indexes.size() < 1)

return;

// QModelIndex::operator < sorts first by row, then by column.

// this is what we need

// std::sort(indexes.begin(), indexes.end());

qSort(indexes);

// You need a pair of indexes to find the row changes

QModelIndex previous = indexes.first();

indexes.removeFirst();

QString selected_text_as_html;

QString selected_text;

selected_text_as_html.prepend("

");

QModelIndex current;

Q_FOREACH(current, indexes)

{

QVariant data = model()->data(previous);

QString text = data.toString();

selected_text.append(text);

text.replace("\n","
");

// At this point `text` contains the text in one cell

selected_text_as_html.append(text);

// If you are at the start of the row the row number of the previous index

// isn't the same. Text is followed by a row separator, which is a newline.

if (current.row() != previous.row())

{

selected_text_as_html.append("

");

selected_text.append(QLatin1Char('\n'));

}

// Otherwise it's the same row, so append a column separator, which is a tab.

else

{

selected_text_as_html.append("

");

selected_text.append(QLatin1Char('\t'));

}

previous = current;

}

// add last element

selected_text_as_html.append(model()->data(current).toString());

selected_text.append(model()->data(current).toString());

selected_text_as_html.append("

QMimeData * md = new QMimeData;

md->setHtml(selected_text_as_html);

// qApp->clipboard()->setText(selected_text);

md->setText(selected_text);

qApp->clipboard()->setMimeData(md);

// selected_text.append(QLatin1Char('\n'));

// qApp->clipboard()->setText(selected_text);

}

void QTableWidgetWithCopyPaste::paste()

{

if(qApp->clipboard()->mimeData()->hasHtml())

{

// TODO, parse the html data

}

else

{

QString selected_text = qApp->clipboard()->text();

QStringList cells = selected_text.split(QRegExp(QLatin1String("\\n|\\t")));

while(!cells.empty() && cells.back().size() == 0)

{

cells.pop_back(); // strip empty trailing tokens

}

int rows = selected_text.count(QLatin1Char('\n'));

int cols = cells.size()/rows;

if(cells.size() % rows != 0)

{

// error, uneven number of columns, probably bad data

QMessageBox::critical(this, tr("Error"),

tr("Invalid clipboard data, unable to perform paste operation."));

return;

}

if(cols != columnCount())

{

// error, clipboard does not match current number of columns

QMessageBox::critical(this, tr("Error"),

tr("Invalid clipboard data, incorrect number of columns."));

return;

}

// don't clear the grid, we want to keep any existing headers

setRowCount(rows);

// setColumnCount(cols);

int cell = 0;

for(int row=0; row < rows; ++row)

{

for(int col=0; col < cols; ++col, ++cell)

{

QTableWidgetItem *newItem = new QTableWidgetItem(cells[cell]);

setItem(row, col, newItem);

}

}

}

}

void QTableWidgetWithCopyPaste::keyPressEvent(QKeyEvent * event)

{

if(event->matches(QKeySequence::Copy))

{

copy();

}

else if(event->matches(QKeySequence::Paste))

{

paste();

}

else

{

QTableWidget::keyPressEvent(event);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值