如何使用Qt捕获当前屏幕图像?
我的自定义QDialog中有一个按钮,当按下按钮1时,我会发出一个信号如何使用Qt捕获当前屏幕图像?,qt,Qt,我的自定义QDialog中有一个按钮,当按下按钮1时,我会发出一个信号 void MyCustomDialog::on_pushButton_1() { this->hide(); //i need to hide this window before 'OnButton_1_Clicked' stuffs starts emit button_1_clicked(); } 在我的主窗口中,我连接了插槽并创建了如下所示的实例 void MainWindow::MainWi
void MyCustomDialog::on_pushButton_1()
{
this->hide(); //i need to hide this window before 'OnButton_1_Clicked' stuffs starts
emit button_1_clicked();
}
在我的主窗口中,我连接了插槽并创建了如下所示的实例
void MainWindow::MainWindow()
{
MyCustomDialog *dlg = MyCustomDialog::getInstance(this); //only single instance created
connect(dlg, &MyCustomDialog::button_1_clicked, this, &MainWindow::OnButton_1_Clicked);
}
我在mainwindow中的一个函数中显示我的自定义对话框,如下所示
void MainWindow::dispayCustomDialog()
{
MyCustomDialog *dlg = MyCustomDialog::getInstance();
dlg->show();
}
下面显示了我的“打开按钮”插槽是如何单击的。在其中,我使用下面的行捕获屏幕截图
void MainWindow::OnButton_1_Clicked()
{
//capture the screen shot
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap *map = new QPixmap(screen->grabWindow(0));
bool result = map->save("D:/test.jpg", "JPG");
}
使用上述函数捕获屏幕后,我仍然可以在test.jpg文件中看到我的“MyCustomDialog”。QtDoc说QGuiApplication::primaryScreen捕获应用程序的初始状态。所以我认为,这是我所期望的。我们有没有其他的解决方案来抓取当前状态的屏幕
我试图实现的是在隐藏我的“MyCustomDialog”后,在OnButton_1_Clicked函数中抓取屏幕 我找到了解决办法。在捕获屏幕之前,使用单槽定时器,延迟500毫秒,如下所示。这将等待我的自定义对话框正确隐藏
void MainWindow::OnButton_1_Clicked()
{
QTimer::singleShot(500, this, &MainWindow::shootscreen);
}
void MainWindow::shootscreen()
{
//capture the screen shot
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap map = screen->grabWindow(0);
bool result = map.save("D:/test.jpg", "JPG");
}
你怎么知道它是在关闭前执行的?我刚刚试过,它工作正常。要执行此操作,请验证它是否隐藏:void main window::OnButton\u 1\u单击{qDebugisVisible;qDebugOh.我的错。你是对的。我应该用visible函数进行验证。不幸的是,我在“OnButton_1_Clicked”中抓取屏幕进行了验证。因此错误出现在我的抓取屏幕函数中。QScreen*screen=QGuiApplication::primaryScreen;QPixmap*map=new QPixmapscreen->grabWindow0;boolresult=map->saveD:/screen.jpg,jpg;。据我所知,QGUI应用程序::primaryScreen;捕获屏幕的初始状态,在该状态下,我的自定义对话框最初可见。因此,我应该找到另一种方法来捕获当前屏幕,而不是通过QGUI应用程序::primaryScreen捕获初始屏幕。如果您有r主要目标,而不是解决一个不确定是否有效的可能解决方案。不要使用指针,您正在创建动态内存,这是一种不好的做法。将其更改为:QPixmap map=screen->grabWindow0;bool result=map.saveD:/test.jpg,jpg;