contextMenuEvent

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QFileDialog>
#include <QScreen>
#include <QDir>
#include <QClipboard>
#include <QDebug>
#include <QCursor>
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_newPicButton_clicked()
{
    if (ui->radioButtonHide->isChecked()) {
        timer = new QTimer();
        //timer->setInterval(ui->spinBox->value()*1000);

        //this->hide();
        timer->start(1000/3);
        //int id = startTimer(ui->spinBox->value()*1000);
        connect(this->timer,&QTimer::timeout,this,[=]{
            //QDesktopWidget w;
            m_pic = QPixmap::grabWindow(QApplication::desktop()->winId());

            // 保存到剪切版
            //QClipboard *clipboard = QGuiApplication::clipboard();

//            QString originalText = clipboard->text();
//            qDebug() << originalText;
            //clipboard->setPixmap(m_pic);


            QSize size = ui->labelShowPic->size();
            ui->labelShowPic->setPixmap(m_pic.scaled(size));
            //timer->stop();
            //this->show();
        });


    }
    else {
        qApp->beep();// 嘀嘀响
    }
}

void MainWindow::on_savePicButton_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               QDir::currentPath(),
                               tr("Images (*.png *.xpm *.jpg)"));

    if (!fileName.isEmpty()) {
        m_pic.save(fileName);
    }

}

void MainWindow::on_exitButton_clicked()
{
    this->close();
}

void MainWindow::contextMenuEvent(QContextMenuEvent *e)
{
    QMenu *menu = new QMenu(this);
    QAction *action = new QAction(this);
    QAction *startNotePad = new QAction(this);

    connect(action,&QAction::triggered,this,&MainWindow::on_savePicButton_clicked);
    connect(startNotePad,&QAction::triggered,this,&MainWindow::startNotePad);

    action->setText("Save as");
    startNotePad->setText("startNotePad");
    menu->addAction(action);
    menu->addAction(startNotePad);
    menu->exec(QCursor::pos());
}

void MainWindow::startNotePad()
{
    QProcess *p = new QProcess();
    p->start("notepad.exe");
}

 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QPixmap>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_newPicButton_clicked();

    void on_savePicButton_clicked();

    void on_exitButton_clicked();

    void contextMenuEvent(QContextMenuEvent *e);

    void startNotePad();

private:
    Ui::MainWindow *ui;

    QTimer *timer;
    QPixmap m_pic;
};

#endif // MAINWINDOW_H

 

posted @ 2019-06-23 22:49  狂奔~  阅读(1336)  评论(0编辑  收藏  举报