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:
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:
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 2000ms
and 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:
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.