qt复制图片功能
右键弹出菜单栏,添加(给widget换色),删除(清除颜色),复制(复制widget图像到剪切板)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QWidgetAction> #include<QDebug> #include <QMenu> #include <QContextMenuEvent> #include<QEvent> #include <QRandomGenerator> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void copyWidgetTestImageToClipboard(QWidget* widgetTest); QString generateRandomHexColor(); public slots: void onAddActionTriggered(); void onDeleteActionTriggered(); void onCopyActionTriggered(); protected: void contextMenuEvent(QContextMenuEvent* event)override; private: Ui::MainWindow *ui; QMenu* contextMenu; // 右键菜单 QWidgetAction* addAction; // 新增菜单项 QWidgetAction* deleteAction; // 删除菜单项 QWidgetAction* copyAction; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QClipboard> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // 创建右键菜单 contextMenu = new QMenu(this); // 创建菜单项 addAction = new QWidgetAction(contextMenu); addAction->setText(u8"新增"); connect(addAction, &QAction::triggered, this, &MainWindow::onAddActionTriggered); contextMenu->addAction(addAction); deleteAction = new QWidgetAction(contextMenu); deleteAction->setText(u8"删除"); connect(deleteAction, &QAction::triggered, this, &MainWindow::onDeleteActionTriggered); contextMenu->addAction(deleteAction); copyAction = new QWidgetAction(contextMenu); copyAction->setText(u8"复制"); connect(copyAction, &QAction::triggered, this, &MainWindow::onCopyActionTriggered); contextMenu->addAction(copyAction); } MainWindow::~MainWindow() { delete ui; } void MainWindow::copyWidgetTestImageToClipboard(QWidget *widgetTest) { // 创建一个QPixmap对象,以便保存widgetTest中的图像 // QPixmap pixmap(widgetTest->size()); // widgetTest->render(&pixmap); QPixmap screenshot = widgetTest->grab(); // 将图像复制到剪贴板中 QClipboard* clipboard = QGuiApplication::clipboard(); clipboard->setPixmap(screenshot); } QString MainWindow::generateRandomHexColor() { // 生成3个[0, 255]之间的随机数作为RGB颜色值的分量 int red = QRandomGenerator::global()->bounded(256); int green = QRandomGenerator::global()->bounded(256); int blue = QRandomGenerator::global()->bounded(256); // 将RGB颜色值转换为16进制字符串 QString hexColor = QString("#%1%2%3") .arg(red, 2, 16, QChar('0')) .arg(green, 2, 16, QChar('0')) .arg(blue, 2, 16, QChar('0')); return hexColor; } void MainWindow::onAddActionTriggered() { qDebug()<<"add"; QString qss="background-color:"+generateRandomHexColor(); ui->widgetTest->setStyleSheet(qss); } void MainWindow::onDeleteActionTriggered() { qDebug()<<"delete"; ui->widgetTest->setStyleSheet("background-color:transparent"); } void MainWindow::onCopyActionTriggered() { copyWidgetTestImageToClipboard(ui->widgetTest); qDebug()<<"copy"; } void MainWindow::contextMenuEvent(QContextMenuEvent *event) { if (ui->widgetTest->rect().contains(ui->widgetTest->mapFromGlobal(event->globalPos()))) { contextMenu->exec(event->globalPos()); // 显示右键菜单 event->accept(); } else { QMainWindow::contextMenuEvent(event); } }#qt##c++#