本文主要是介绍QTableWidget实现复制粘贴,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1 实现功能
2 代码
1 实现功能
(1)选择一个单元格复制,可进行多个单元格粘贴
(2)选择多个单元格复制,可进行同样多单元格粘贴,行列均可
2 代码
void TableWidgetClass::keyPressEvent(QKeyEvent *event) {if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_C) {// 实现复制逻辑QTableWidgetItem *item;//QMimeData *mimeData = new QMimeData;QString list;for (int i = 0; i < selectedItems().count(); ++i) {item = selectedItems().at(i);if(item->text().isEmpty())list += " ";elselist += item->text();list += "\n";}//mimeData->setText(list);m_Clipboard->setText(list);QString list1 = m_Clipboard->text();list1.trimmed();}else if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_V) {// 实现粘贴逻辑QString mimeData = m_Clipboard->text();if (!mimeData.isEmpty()) {QStringList list = mimeData.split("\n");list.removeLast();if(list.count() > 1){if(list.count() == selectedItems().count()){for (int i = 0; i < selectedItems().count(); ++i) {QTableWidgetItem *item = selectedItems().at(i);QString text = list.at(i);item->setText(text);}}}else if(list.count() == 1){for (int i = 0; i < selectedItems().count(); ++i) {QTableWidgetItem *item = selectedItems().at(i);QString text = list.at(0);item->setText(text);}}}}elseQTableWidget::keyPressEvent(event);
}
该类继承了QTableWidget,代码较为简单。
这篇关于QTableWidget实现复制粘贴的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!