From fb9a52b48eb43191574392d58bf317a927b8e17e Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 16 Mar 2025 21:04:14 +0100 Subject: [PATCH] table menu item to make screenshot of selected rows https://github.com/COVESA/dlt-viewer/issues/640 Signed-off-by: Viktor Kopp --- src/mainwindow.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f02decb2..17ce0162 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -17,6 +17,8 @@ * @licence end@ */ +#include "qabstractitemmodel.h" +#include "qdebug.h" #include #include #include @@ -7005,6 +7007,38 @@ void MainWindow::on_tableView_customContextMenuRequested(QPoint pos) menu.addSeparator(); + action = new QAction("Copy Selection as image to Clipboard", this); + connect(action, &QAction::triggered, this, [this](){ + // save and clear selection for clean screenshot + auto selectionIndexes = ui->tableView->selectionModel()->selection().indexes(); + + auto headerRect = ui->tableView->horizontalHeader()->rect(); + + ui->tableView->selectionModel()->clearSelection(); + + QRect res; + for (qsizetype i = 0; i < selectionIndexes.size(); ++i) + { + res |= ui->tableView->visualRect(selectionIndexes[i]); + } + // take into account height of the table header + res.translate(0, headerRect.height()); + + auto tableViewRect = ui->tableView->contentsRect(); + auto pixmap = ui->tableView->grab(res & tableViewRect); + + QApplication::clipboard()->setPixmap(pixmap); + + // restore selection + for (const auto& index : selectionIndexes) + { + ui->tableView->selectionModel()->select(index, QItemSelectionModel::Select); + } + }); + menu.addAction(action); + + menu.addSeparator(); + action = new QAction("&Export...", this); if(qfile.sizeFilter() <= 0) {