自定义QTableView右键弹出菜单, 并复制选中的单元格内容到剪贴板中

源码如下:

头文件

private:
    QAction *m_pActionCopy;

private slots:
    void copyData1();
    void copyData2();

源文件:

void frmDbDelegate::initTableView()
{
m_pActionCopy = new QAction(tr("复制"), ui->tableView);
    connect(m_pActionCopy, &QAction::triggered, this, &frmDbDelegate::copyData2);
    ui->tableView->setSelectionMode(QAbstractItemView::ContiguousSelection); //设置为连续选择模式
    ui->tableView->setContextMenuPolicy(Qt::ActionsContextMenu);             //设置为action菜单模式
    ui->tableView->addAction(this->m_pActionCopy);
}

//粘贴板表格内容格式:
//列与列之间内容以制表符分隔("\t")
//行与行之间内容以换行符分隔("\n")

//粘贴:
//获取粘贴板内容,把内容分解成单个item的值并放到表格中
//通过QApplication::clipboard()->text()类获取粘贴板的内容

//复制:
//获取选中item,把选中item内容组织一下并放到粘贴板
//通过QApplication::clipboard()->setText()设置粘贴板内容
void frmDbDelegate::copyData1()
{
    QModelIndexList indexes = ui->tableView->selectionModel()->selectedIndexes();
    if (indexes.count() == 0)
    {
        //select nothing
        return;
    }

    QMap<QString, QString> map;
    QModelIndex index;
    int k = 0;
    int maxCol = 0;
    int maxRow = 0;
    int minCol = 0;
    int minRow = 0;

    foreach (index, indexes)
    {
        int col = index.column();
        int row = index.row();

        if (k == 0)
        {
            minCol = col;
            minRow = row;
        }

        if (col > maxCol)
            maxCol = col;

        if (row > maxRow)
            maxRow = row;

        QString text = index.model()->data(index, Qt::EditRole).toString();
        map[QString::number(row) + "," + QString::number(col)] = text;

        k++;
    }

    QString rs = "";
    for (int row = minRow; row <= maxRow; row++)
    {
        for (int col = minCol; col <= maxCol; col++)
        {
            if (col != minCol)
                rs += "\t";

            rs += map[QString::number(row) + "," + QString::number(col)];
        }
    }

    rs += "\r\n";

    //复制到剪贴板
    QClipboard *board = QApplication::clipboard();
    board->setText(rs);

    qDebug() << rs;
}

void frmDbDelegate::copyData2()
{
    QStringList list;
    QModelIndexList indexes = ui->tableView->selectionModel()->selectedIndexes();
    if (indexes.count() == 0)
    {
        //select nothing
        return;
    }

    foreach (const QModelIndex &index, indexes)
    {
        list << index.data().toString();
    }

    QApplication::clipboard()->setText(list.join(","));
}

 

 

---

参考文献

《Qt QTableView 上加右键弹出菜单, 并复制选中的单元格内容到剪贴板中》

http://www.doczj.com/doc/c51cfb63cf84b9d528ea7a29.html

《已选中QTableView中的行/行复制到QClipboard》

http://cn.voidcc.com/question/p-myqnyjft-mk.html

《复制QTableView的一部分(Copying part of QTableView)》

https://www.it1352.com/549965.html

《QTableWidget, QTableView实现粘贴复制》

https://blog.csdn.net/time_forget/article/details/100765595

 

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于QTableView,可以通过使用QMenu和QAction来实现菜单。以下是一个简单的示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QMenu, QAction, QMessageBox from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("QTableView菜单示例") # 创建QTableView self.table_view = QTableView(self) self.setCentralWidget(self.table_view) # 创建菜单 self.context_menu = QMenu(self.table_view) self.delete_action = QAction("删除", self) self.context_menu.addAction(self.delete_action) # 绑定菜单信号 self.table_view.setContextMenuPolicy(Qt.CustomContextMenu) self.table_view.customContextMenuRequested.connect(self.show_context_menu) # 绑定删除动作的槽函数 self.delete_action.triggered.connect(self.delete_row) def show_context_menu(self, pos): # 显示菜单 global_pos = self.table_view.mapToGlobal(pos) self.context_menu.exec_(global_pos) def delete_row(self): # 获取当前选中的行 selected_row = self.table_view.currentIndex().row() if selected_row >= 0: # 在这里编写删除行的代码 QMessageBox.information(self, "提示", "删除行:" + str(selected_row)) if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_() ``` 这个例子创建了一个带有QTableView的主窗口,并在QTableView上实现了一个菜单菜单有一个"删除"动作,当用户选择该动作时,会一个消息框显示删除的行号。 你可以根据实际需求修改和扩展这个示例代码,来满足你的具体需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值