<分区>
我正在做一个小的个人项目。我想在窗口(窗体)中显示实时桌面 View 。这可能吗?我正在使用 C++ 开发 Qt Designer/Creator。请提供指导文件,教程(如果有的话)。
我正在努力实现这一目标:
<分区>
我正在做一个小的个人项目。我想在窗口(窗体)中显示实时桌面 View 。这可能吗?我正在使用 C++ 开发 Qt Designer/Creator。请提供指导文件,教程(如果有的话)。
我正在努力实现这一目标:
最佳答案
你想要的是不断截取屏幕截图并显示在标签上:
这是一个小例子:
SimpleScreenCapture.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SimpleScreenCapture
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
main.cpp:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QLabel;
class QVBoxLayout;
class QTimer;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private slots:
void takeScreenShot();
private:
QLabel *screenshotLabel;
QPixmap originalPixmap;
QVBoxLayout *mainLayout;
QTimer *timer;
};
#endif // WIDGET_H
小部件.cpp:
#include "widget.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QTimer>
#include <QScreen>
#include <QGuiApplication>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
timer = new QTimer(this);
timer->setInterval(2000);
screenshotLabel = new QLabel;
screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
screenshotLabel->setAlignment(Qt::AlignCenter);
screenshotLabel->setMinimumSize(240, 160);
mainLayout = new QVBoxLayout;
mainLayout->addWidget(screenshotLabel);
setLayout(mainLayout);
connect(timer, SIGNAL(timeout()), SLOT(takeScreenShot()));
timer->start();
}
Widget::~Widget()
{
}
void Widget::takeScreenShot()
{
originalPixmap = QPixmap();
QScreen *screen = QGuiApplication::primaryScreen();
if (screen)
{
originalPixmap = screen->grabWindow(0);
}
screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
}
这很简单...您每 2000 毫秒
截取一次屏幕截图并将它们显示在 QLabel
上。
我建议你看看 screenshot example .我的例子是它的简化版本。
结果是:
如果您正在寻找类似屏幕共享的应用程序,您应该实现窗口的鼠标事件并获取点的坐标。然后处理它们以匹配原始桌面的屏幕分辨率并将这些点发送到系统以供点击。这是特定于平台的,您应该根据平台检查 POSIX/WinAPI 函数。
关于c++ - 如何使用Qt在windows窗体中显示桌面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26583929/