开发者

Taking screenshot of a specific window - C++ / Qt

开发者 https://www.devze.com 2022-12-11 22:01 出处:网络
In Qt, how do I take a screenshot of a specific window (i.e. suppose I had Notepad up and I wanted to take a screenshot of the window titled \"Untitled - Notepad\")?

In Qt, how do I take a screenshot of a specific window (i.e. suppose I had Notepad up and I wanted to take a screenshot of the window titled "Untitled - Notepad")? In their screenshot example code, they show how to take a screenshot of the entire desktop:

originalPixmap = QPixmap::grabWindow(QApplication::desktop()开发者_运维百科->winId());

How would I get the winId() for a specific window (assuming I knew the window's title) in Qt?

Thanks


I'm pretty sure that's platform-specific. winIds are HWNDs on Windows, so you could call FindWindow(NULL, "Untitled - Notepad") in the example you gave.


Look at QDesktopWidget class. It's inherited from QWidget so there is literally no problem of taking screenshot:

QPixmap pm(QDesktopWidget::screenGeometry().size());
QDesktopWidget::screen().render(&pm); // pm now contains screenshot


Also look at WindowFromPoint and EnumChildWindows. The latter could allow you to prompt the user to disambiguate if you had multiple windows with the same title.


Have a look at Screenshot example

In short:

QScreen *screen = QGuiApplication::primaryScreen();
if (screen)
    QPixmap originalPixmap = screen->grabWindow(0);


Although this has already been answered, just for the sake of completeness, I'll add to Trevor Boyd Smith's post (see above) a code-snippet example:

void MainWindow::on_myButton_GUI_Screeshot_clicked()
{
    QPixmap qPixMap = QPixmap::grabWidget(this);  // *this* is window pointer, the snippet     is in the mainwindow.cpp file

    QImage qImage = qPixMap.toImage();

    cv::Mat GUI_SCREENSHOT = cv::Mat(         qImage.height(),
                                              qImage.width(), CV_8UC4,
                                      (uchar*)qImage.bits(),
                                              qImage.bytesPerLine()  );

    cv::imshow("GUI_SCREENSHOT",GUI_SCREENSHOT);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消