1

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: enter image description here

Gates
  • 67
  • 3
  • 11
  • Do you want to just show the desktop or you want to be able to click/type there and so on? It is the same desktop the application is running at? – Ezee Oct 27 '14 at 08:57
  • I just want to show live view of desktop, just like a movie, but in a window(form) and yes It is the same desktop the application is running at. – Gates Oct 27 '14 at 09:04
  • 1
    These articles can be useful for you: [1](http://stackoverflow.com/questions/531684/what-is-the-best-way-to-take-screenshots-of-a-window-with-c-in-windows) [2](http://stackoverflow.com/questions/664841/how-to-capture-a-video-of-my-desktop-with-net) – Ezee Oct 27 '14 at 09:13
  • http://qt-project.org/doc/qt-4.8/desktop-screenshot.html – Michał Walenciak Oct 27 '14 at 09:15
  • If you need a real live view - then you should look at platform-specific stuff. For example, directx things for grabbing screen and performing necessary scaling/transformatins. There are no out-of-box solution for Qt. – Dmitry Sazonov Oct 27 '14 at 09:18
  • You cannot reliably get window contents of windows that are obscured. Your requirement to run your application on the same desktop as the contents you want to display in your application cannot be met. – IInspectable Oct 27 '14 at 09:48
  • @Gates Bill, is that you?... – Jacob Krieg Oct 28 '14 at 10:22

1 Answers1

1

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

Here's a small example:

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

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

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.

The result is:

enter image description here

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.

Iuliu
  • 4,001
  • 19
  • 31
  • This is just what I want. Thanks! But theres one little problem I m working on dual monitor, So it takes screenshot of both as single one. How do I take screenshot of only primary screen? – Gates Oct 28 '14 at 05:27
  • @Gates Hmm...are you sure? I also have 2 monitors and it takes screenshot on just the primary one...the code `QScreen *screen = QGuiApplication::primaryScreen();` also suggests that...are you sure your setup is configured OK? Have you tried playing with `QGuiApplication::screens()` and tried to grab a different screen and see what happens? – Iuliu Oct 28 '14 at 09:57
  • This problem occurs on ubuntu not on windows 7! I'll figure it out. Thanks alot man! – Gates Oct 28 '14 at 12:12