c++ - 如何在各种显示器上使用 Qt/C++ 获取打印屏幕?

标签 c++ qt screenshot multiple-monitors printscreen

我是 Qt Creator 的新手,但我正在尝试创建和应用而不是每分钟拍摄一次屏幕截图,我在 Qt Creator 中找到了一个代码来执行此操作,但它只能在一个显示器上工作,如果我有两个或更多,它只需要一个显示器的屏幕截图。

这是我正在使用的代码。

这是截图.h

#ifndef SCREENSHOT_H
#define SCREENSHOT_H

#include <QPixmap> 
#include <QWidget>

class QCheckBox;
class QGridLayout;
class QGroupBox;
class QHBoxLayout;
class QLabel;
class QPushButton;
class QSpinBox;
class QVBoxLayout;

class screenshot : public QWidget
{
    Q_OBJECT

public:
    screenshot();

protected:
   void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;

private slots:
   void newScreenshot();
   void saveScreenshot();
   void shootScreen();
   void updateCheckBox();

private:
   void createOptionsGroupBox();
   void createButtonsLayout();
   QPushButton *createButton(const QString &text, QWidget *receiver, const char *member);
   void updateScreenshotLabel();

   QPixmap originalPixmap;

   QLabel *screenshotLabel;
   QGroupBox *optionsGroupBox;
   QSpinBox *delaySpinBox;
   QLabel *delaySpinBoxLabel;
   QCheckBox *hideThisWindowCheckBox;
   QPushButton *newScreenshotButton;
   QPushButton *saveScreenshotButton;
   QPushButton *quitScreenshotButton;

   QVBoxLayout *mainLayout;
   QGridLayout *optionsGroupBoxLayout;
   QHBoxLayout *buttonsLayout;
};

#endif // SCREENSHOT_H

这是截图.cpp

#include <QtWidgets>

#include "screenshot.h"

screenshot::screenshot()
{
    screenshotLabel = new QLabel;
    screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    screenshotLabel->setAlignment(Qt::AlignCenter);
    screenshotLabel->setMinimumSize(240, 160);

    createOptionsGroupBox();
    createButtonsLayout();

    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(screenshotLabel);
    mainLayout->addWidget(optionsGroupBox);
    mainLayout->addLayout(buttonsLayout);
    setLayout(mainLayout);

    shootScreen();
    delaySpinBox->setValue(5);

    setWindowTitle(tr("Screenshot"));
    resize(300, 200);
}

void screenshot::resizeEvent(QResizeEvent * /* event */)
{
     QSize scaledSize = originalPixmap.size();
     scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
     if (!screenshotLabel->pixmap() || scaledSize != screenshotLabel->pixmap()->size())
         updateScreenshotLabel();
  }

  void screenshot::newScreenshot()
  {
       if (hideThisWindowCheckBox->isChecked())
           hide();
       newScreenshotButton->setDisabled(true);

      QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(shootScreen()));
 }

 void screenshot::saveScreenshot()
 {
     QString format = "jpg";
     QString initialPath = QDir::currentPath() + tr("/untitled.") + format;

     QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), initialPath,
                                                tr("%1 Files (*.%2);;All Files (*)")
                                                .arg(format.toUpper())
                                                .arg(format));
   if (!fileName.isEmpty())
       originalPixmap.save(fileName, format.toLatin1().constData());
}

void screenshot::shootScreen()
{
    if (delaySpinBox->value() != 0)
       qApp->beep();
    originalPixmap = QPixmap(); // clear image for low memory situations
                            // on embedded devices.
   QScreen *screen = QGuiApplication::primaryScreen();
   if (screen)
      originalPixmap = screen->grabWindow(0);
    updateScreenshotLabel();

   newScreenshotButton->setDisabled(false);
   if (hideThisWindowCheckBox->isChecked())
      show();
}

void screenshot::updateCheckBox()
{
  if (delaySpinBox->value() == 0) {
      hideThisWindowCheckBox->setDisabled(true);
      hideThisWindowCheckBox->setChecked(false);
  } else {
      hideThisWindowCheckBox->setDisabled(false);
  }
}

void screenshot::createOptionsGroupBox()
{
    optionsGroupBox = new QGroupBox(tr("Options"));

    delaySpinBox = new QSpinBox;
    delaySpinBox->setSuffix(tr(" s"));
    delaySpinBox->setMaximum(60);
    connect(delaySpinBox,SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox()));
    //connect(delaySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox()));

    delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:"));

    hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"));

    optionsGroupBoxLayout = new QGridLayout;
    optionsGroupBoxLayout->addWidget(delaySpinBoxLabel, 0, 0);
    optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1);
    optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2);
    optionsGroupBox->setLayout(optionsGroupBoxLayout);
}

void screenshot::createButtonsLayout()
{
    newScreenshotButton = createButton(tr("New Screenshot"), this, SLOT(newScreenshot()));
   saveScreenshotButton = createButton(tr("Save Screenshot"), this, SLOT(saveScreenshot()));
   quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close()));

   buttonsLayout = new QHBoxLayout;
   buttonsLayout->addStretch();
   buttonsLayout->addWidget(newScreenshotButton);
   buttonsLayout->addWidget(saveScreenshotButton);
   buttonsLayout->addWidget(quitScreenshotButton);
}

   QPushButton *screenshot::createButton(const QString &text, QWidget *receiver,
                                  const char *member)
{
    QPushButton *button = new QPushButton(text);
    button->connect(button, SIGNAL(clicked()), receiver, member);
    return button;
}

 void screenshot::updateScreenshotLabel()
 {
    screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
                                                 Qt::KeepAspectRatio, Qt::SmoothTransformation));
 }

有人可以帮我修改这段代码或告诉我该怎么做吗??

谢谢

最佳答案

QGuiApplication类有一个 screens()返回指向计算机所有 QScreen 对象的指针列表的方法。所以你想使用它,例如替换此代码:

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

更像是这样的:

QList<QScreen *> screens = QGuiApplication::screens();
QList<QPixmap> pixmapsList;
for (int i=0; i<screens.size(); i++)
{
   const QRect r = screens[i]->geometry();
   pixmapsList.push_back(screens[i]->grabWindow(0), r.x(), r.y(), r.width(), r.height());
}

... 那么您当然需要修改文件保存代码以将每个 QPixmap 保存在 pixmapsList 中,而不仅仅是单个 QPixmap,但这很简单。

关于c++ - 如何在各种显示器上使用 Qt/C++ 获取打印屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29988952/

相关文章:

python - PyQt - 在 QThread 中设置代理模型的模型

php - 在wkhtmltoimage中截图时如何指定大小?

c# - 在 Windows 7 中使用 Windows 服务截取屏幕截图

c++ - 使用 Qt Designer 通过附加菜单扩展 QMainWindow 的现有 UI

c++ - 'auto' 关键字有什么意义?

c++ - gdb 调试异常

qt - 这两种OpenSSL生成方式的区别

java - 处理与 JNI 的关联

c++ - 使用文件管理器创建空目录的方法

iphone - 以编程方式截取屏幕截图,无需状态栏