如何在QT中使用Paint事件(WM_Paint)进行截图

how to take screenshot with Paint event (WM_Paint) in QT

本文关键字:Paint WM 事件 QT      更新时间:2023-10-16

我正试图用QT中的Paint事件(WM_Paint)事件截图,但我不知道如何,我用这个代码来截图,但它不是很好当桌面发生一些变化时,它必须进行截图,而不是每1000毫秒用计时器进行截图

     void MainWindow::shootScreen()
 {
 originalPixmap = QPixmap(); // clear image for low memory situations
                             // on embedded devices.
 originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
 //emit getScreen(originalPixmap);
 updateScreenshotLabel();
 }
 void MainWindow::updateScreenshotLabel()
 {
     this->ui->label_2->setPixmap(originalPixmap.scaled(this->ui->label_2-    >size(),
                                                  Qt::KeepAspectRatio,
                                                  Qt::SmoothTransformation));
 }

在您感兴趣的小部件上使用QObject::installEventFilter,然后检查适当的事件。例如,在MainWindow ui初始化中:

void MainWindow::yourUiInitFunc()
{
    exampleWidget = new QWidget;
    ...
    exampleWidget->installEventFilter(this);
    ...
}

然后重新实现eventFilter:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == exampleWidget && event->type() == QEvent::KeyPress)
        shootScreen();
    return QMainWindow::eventFilter(obj, event);
}

谢谢你,Jon Harper,你的代码很有用,但是我改变了一些东西你的代码只是工作时,一些按键事件在我的项目形式,但我添加了油漆方法,你的如果,现在它工作在Windows事件太

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == this && QEvent::Paint == event->type()) {
        shootScreen();
    }
    return false;
}