简体   繁体   中英

Screen Recording in Qt

I have two displays (one monitor, another projector) let's say display1 , display2 . I'm trying to show these to monitors on two QLabels Label1 & Label2 . For this, I use the following code in QT.

QScreen *screen = QGuiApplication::primaryScreen();
QPixmap originalPixmap = screen->grabWindow(0);
ui->label->setPixmap(originalPixmap);

this I made pushbutton specific ie when I click on "start pushbutton" the above code will run and if I click on "stop PushButton" I'm clearing label1 ie ui->label->clear();

By this I'm able to get the screenshots of images, but I want it to be live, so I thought to use QTimer to keep loading images onto the label and keep clearing it, but it doesn't seem working. Here's my code

#include "ScreenCapture.h"
#include "ui_ScreenCapture.h"

ScreenCapture::ScreenCapture(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::ScreenCapture)
{
    ui->setupUi(this);

    CaptureTimer = new QTimer(this);
    CaptureTimer->setInterval(30);
    connect(CaptureTimer, SIGNAL(timeout()), this, SLOT(load_an_Image()));
    CaptureTimer->start();
}
void ScreenCapture::on_pbtn_start_clicked()
{
    load_an_Image();
}

void ScreenCapture::on_pbtn_stop_clicked()
{
    ui->label->clear();
}

void ScreenCapture::load_an_Image()
{
    on_pbtn_stop_clicked();
    QScreen* screen = QGuiApplication::primaryScreen();
    QPixmap originalPixmap = screen->grabWindow(0);
    ui->label->setPixmap(originalPixmap);
}

This isn't working as I'm getting the picture inside a picture ie Picture

In this way, I thought to do for two different screens. So now my question is, is a better way of showing the screens live to QLabels and later record them and store it.

The reason for this overlay is that label clearing is not completed instantaneously (asynchronous), on the other hand you are taking the next shot just immediately, thus previous label pixmap appears and so on ...! You need to refine your screen shots by suitable wait states, using another timer or any mechanism you find suitable.

The essense of the solution is to set a suitable interval between housekeeping work (clearing label ...etc.) and taking the screen shot.

for example:

ScreenCapture::ScreenCapture(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScreenCapture)
{
    ui->setupUi(this);
    CaptureTimer=new QTimer(this);
    CaptureTimer->setInterval(200);
    connect(CaptureTimer, &QTimer::timeout, this, &ScreenCapture::on_pbtn_start_clicked);
    CaptureTimer->start();
}
void ScreenCapture::on_pbtn_start_clicked()
{
    ui->label->clear();
    QTimer::singleShot(20,this, &ScreenCapture::load_an_Image);
}
void ScreenCapture::on_pbtn_stop_clicked()
{
    ui->label->clear();
}

void ScreenCapture::load_an_Image()
{
    screen = QGuiApplication::primaryScreen();
    QPixmap originalPixmap = screen->grabWindow(0);
    ui->label->setPixmap(originalPixmap);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM