如何使用Qt在窗体中显示桌面? [英] How to display desktop in windows form using Qt?

查看:461
本文介绍了如何使用Qt在窗体中显示桌面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事小型个人项目。我想在窗口(窗体)中显示实时桌面视图。是可能吗,我使用C ++在Qt Designer / Creator上工作。请提供指导文件,教程。

I m working on small personal project. I want to display live desktop view in a window(form). Is it possible?, I m working on Qt Designer/Creator using C++. Please provide me guides documents, tutorial if any.

我试图实现这个:

I m trying to achieve this:

推荐答案

你想要的是不断截图屏幕并将其显示在标签上:

What you want is to constantly take screenshots of the screen and display them on a label:

这是一个小例子:

SimpleScreenCapture.pro :

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:

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

widget.cpp: / em>

widget.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));
}

这很简单...你每 2000ms 并显示在 QLabel 上。
我们建议您查看屏幕截图示例 a>。

It's simple...you take screenshots every 2000msand display them on a QLabel. I recommend you to take a look at the screenshot example. My exaple is a simplified version of it.

结果是:

如果您正在寻找类似屏幕分享的应用程式你应该实现窗口的鼠标事件,并获取点的坐标。然后处理它们以匹配原始桌面的屏幕分辨率,并将点发送到系统进行点击。这是平台特定的,你应该检查POSIX / WinAPI函数取决于平台。

If you are looking for a screen-share-like application you should implement the mouse event of the window and take the coordinates of the point. Than process them to match the screen resolution of the original desktop and send the points to the system for clicking. This is platform specific and you should check POSIX/WinAPI functions depending on the platform.

这篇关于如何使用Qt在窗体中显示桌面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆