【Linux编程】用QT实现屏幕监视

Linux实验课 使用QT实现Windows端监视Linux端屏幕
注意工程目录路径不允许有中文出现

【Windows端】RemoteController

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
 void signalGetData(const QByteArray& data);

private slots:
 void onHandleData(const QByteArray& data);

protected slots:
    void readPendingDatagrams();
private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    QUdpSocket *udpSocket;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::Any, 7755);

    connect(udpSocket, SIGNAL(readyRead()),
           this, SLOT(readPendingDatagrams()));

    connect(this, SIGNAL(signalGetData(const QByteArray&)),
           this, SLOT(onHandleData(const QByteArray&)));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete udpSocket;

}

void MainWindow::onHandleData(const QByteArray& data)
{
    QPixmap pixmap;
    QByteArray udata = qUncompress(data);
    pixmap.loadFromData(udata);
    ui->label->setPixmap(
                pixmap.scaled(
                    ui->label->size(),
                    Qt::KeepAspectRatio,
                    Qt::SmoothTransformation));
}

void MainWindow::readPendingDatagrams()
{
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        udpSocket->readDatagram(datagram.data(), datagram.size(),
                                &sender, &senderPort);
        qDebug()<<"receive data "<writeDatagram(datagram, sender, senderPort);
}

mainwindow.ui


【Linux编程】用QT实现屏幕监视_第1张图片

【Linux端】UdpScreen
main.cpp

#include 
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void Screenshot();
    void SendData();

protected slots:
    void readPendingDatagrams();
    void Run();
private:
    Ui::MainWindow *ui;
    QUdpSocket *udpSocket;
    QPixmap originalPixmap;

private slots:
    void on_pushButton_clicked();
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 

#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::Any, 8888);

    connect(udpSocket, SIGNAL(readyRead()),
          this, SLOT(readPendingDatagrams()));

}

MainWindow::~MainWindow()
{
   delete ui;
   delete udpSocket;
}

void MainWindow::readPendingDatagrams()
{
   while (udpSocket->hasPendingDatagrams()) {
       QByteArray datagram;
       datagram.resize(udpSocket->pendingDatagramSize());
       QHostAddress sender;
       quint16 senderPort;

       udpSocket->readDatagram(datagram.data(), datagram.size(),
                               &sender, &senderPort);
       qDebug()<<"receive data "<winId());
}

void MainWindow::SendData()
{
    QSize size(600, 450);
    originalPixmap = originalPixmap.scaled(size);

    qDebug()<<"screen to bytes";
    QByteArray bytes;
    QBuffer buffer(&bytes);
    buffer.open(QIODevice::ReadWrite);
    originalPixmap.save(&buffer, "JPEG"); // writes pixmap into bytes in PNG format

    qDebug()<<"all bytes ori "<writeDatagram(
            bytes, sender, senderPort);
    qDebug()<<"send bytes "< 0)
//    {
//        if(len < send_len)
//        {
//            send_len = len;
//        }
//        qDebug()<writeDatagram(bytes.mid(pos, send_len), sender, senderPort);
//        len -= send_len;
//        pos += send_len;
//    }
}

void MainWindow::Run()
{
    Screenshot();
    SendData();
}

void MainWindow::on_pushButton_clicked()
{
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(Run()));
    timer->start(1000);
}

mainwindow.ui

【Linux编程】用QT实现屏幕监视_第2张图片

附录

  • 工程代码
    链接:http://pan.baidu.com/s/1sllVaod 密码:931q

你可能感兴趣的:(【Linux编程】用QT实现屏幕监视)