1

我正在考虑使用一些调试可能性扩展 QT4 应用程序,以便更轻松地分析客户问题。应用程序已经有一个“调试”模式,当它被启用时,会产生很多日志条目,很难阅读。我想要实现的是在 GUI 上发生更改时截取应用程序的屏幕截图。我知道可能会拍很多照片,但一般是很长时间没有开启Debug模式。问题是我找不到这样的事件/信号。所以我有两个问题:

  1. 有这样的活动我可以订阅吗?我的意思是,每当屏幕上发生任何变化时都会触发一个事件。
  2. 我可以使用 Qt 截取应用程序的屏幕截图吗?

提前致谢!

4

3 回答 3

3

我会使用事件过滤器和 QTimer 来完成,如下所示:

class MyEventFilter : public QObject
{
public:
   MyEventFilter() : _screenshotPending(false) {/* empty */}

   virtual bool eventFilter(QObject * o, QEvent * e)
   {
      if (e->type() == QEvent::Paint)
      {
         if (_screenshotPending == false)
         {
            // we'll wait 500mS before taking the screenshot
            // that way we aren't trying to take 1000 screenshots per second :)
            _screenshotPending = true;
            QTimer::singleShot(500, this, SLOT(TakeAScreenshot()));
         }
      }
      return QObject::eventFilter(o, e);
   }

public slots:
   void TakeAScreenshot()
   {
      _screenshotPending = false;

      // add the standard Qt code for taking a screenshot here
      // see $QTDIR/examples/widgets/desktop/screenshot for that
   }

private:
   bool _screenshotPending;  // true iff we've called QTimer::singleShot() recently
};

int main(int argc, char ** argv)
{
   MyEventFilter filter;

   QApplication app(argc, argv);
   app.installEventFilter(&filter);
   [...]

   return app.exec();
}
于 2015-11-24T01:29:23.483 回答
1

通常,当某些小部件发生更改时,Qt 需要重新绘制它,因此您感兴趣的事件是QEvent::Paint. 这里的问题是,对于相互重叠的小部件,会有大量此类事件。您可以覆盖QApplication::notify()以在所有绘制事件被传递给接收者之前捕获它们。

至于制作Qt应用程序的屏幕截图 - SO上有几个类似的问题,例如从应用程序内部截取qt应用程序截取特定窗口的屏幕截图 - C++ / Qt

这里还有一个讨论将小部件转储到paintEvent().

于 2015-11-24T01:27:29.003 回答
0

至于您的第二个问题,是我的一些旧代码,可以截取窗口的屏幕截图。您可以像这样使用此代码:

HDC WinDC = GetDC(HWND_OF_YOUR_WINDOW);
HBITMAP image = ScreenshotUtility::fromHDC(WinDC);

然后,您可以将 HBITMAP 转换为 Qt Pixmap 对象并按照您的喜好使用它:QPixmap pixmap = QPixmap::fromWinHBITMAP(image);.

编辑:这是特定于 Windows 的代码,不确定其他系统上的等效代码是什么。

于 2015-11-24T01:13:30.077 回答