qt 在tablewidget中实现复制粘贴功能完整示例
时间: 2024-05-07 20:19:49 浏览: 271
以下是在 Qt 中实现 TableWidget 中复制粘贴功能的完整示例:
```cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QHeaderView>
#include <QtGui/QClipboard>
#include <QMimeData>
class TableWidget : public QTableWidget
{
public:
TableWidget(QWidget* parent = nullptr)
: QTableWidget(parent)
{
// 设置表格大小
setRowCount(3);
setColumnCount(3);
// 设置表格内容
setItem(0, 0, new QTableWidgetItem("1"));
setItem(0, 1, new QTableWidgetItem("2"));
setItem(0, 2, new QTableWidgetItem("3"));
setItem(1, 0, new QTableWidgetItem("4"));
setItem(1, 1, new QTableWidgetItem("5"));
setItem(1, 2, new QTableWidgetItem("6"));
setItem(2, 0, new QTableWidgetItem("7"));
setItem(2, 1, new QTableWidgetItem("8"));
setItem(2, 2, new QTableWidgetItem("9"));
// 设置表头
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 设置复制粘贴动作
setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* copyAction = new QAction(tr("复制"), this);
copyAction->setShortcut(QKeySequence::Copy);
connect(copyAction, &QAction::triggered, this, &TableWidget::copy);
addAction(copyAction);
QAction* pasteAction = new QAction(tr("粘贴"), this);
pasteAction->setShortcut(QKeySequence::Paste);
connect(pasteAction, &QAction::triggered, this, &TableWidget::paste);
addAction(pasteAction);
}
void copy()
{
QClipboard* clipboard = QApplication::clipboard();
QMimeData* mimeData = new QMimeData;
QByteArray data;
// 将选中的单元格数据拷贝到剪贴板
QList<QTableWidgetItem*> items = selectedItems();
if (!items.isEmpty())
{
int row_min = items.first()->row();
int row_max = items.first()->row();
int column_min = items.first()->column();
int column_max = items.first()->column();
foreach(QTableWidgetItem* item, items)
{
row_min = qMin(row_min, item->row());
row_max = qMax(row_max, item->row());
column_min = qMin(column_min, item->column());
column_max = qMax(column_max, item->column());
}
for (int row = row_min; row <= row_max; ++row)
{
for (int column = column_min; column <= column_max; ++column)
{
if (column > column_min)
data += "\t";
QTableWidgetItem* item = this->item(row, column);
if (item)
data += item->text().toUtf8();
}
data += "\n";
}
}
mimeData->setData("text/plain", data);
clipboard->setMimeData(mimeData);
}
void paste()
{
QClipboard* clipboard = QApplication::clipboard();
QStringList lines = clipboard->text().split('\n');
// 将剪贴板数据粘贴到当前单元格
int row = currentRow();
int column = currentColumn();
foreach(QString line, lines)
{
if (line.isEmpty())
continue;
QStringList items = line.split('\t');
for (int i = 0; i < items.size(); ++i)
{
QTableWidgetItem* item = new QTableWidgetItem(items[i]);
setItem(row, column + i, item);
}
row++;
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TableWidget tableWidget;
tableWidget.show();
return a.exec();
}
```
在该示例中,我们继承了 QTableWidget 类,并在构造函数中设置了表格的行数、列数和内容,以及表头的大小调整方式。我们还重写了 copy 和 paste 方法,分别实现了将选中的单元格数据拷贝到剪贴板和将剪贴板数据粘贴到当前单元格的功能。最后,我们在构造函数中设置了复制和粘贴动作,并将它们添加到上下文菜单中。当用户在表格中选择单元格并右键单击时,就可以看到复制和粘贴动作,从而实现了复制粘贴功能。
阅读全文