Signals and threads in a Qt based application

61 views Asked by At

I'm building a project where a client sends an image buffer to the server and this server shows the received image using a QGraphicsView.

Everything works properly except for the signal calling part. 

.CPP

//source.cpp
#include "tela_monitoramentos.h"
#include <iostream>

char video_buffer[4096]; //Declaration of the buffer that contains the image binary
int main_func();//Declaration of main function

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

    //Instanciating signal
    connect(this, SIGNAL(call_frameUpdate()), this, SLOT(frame_update()));

}

tela_monitoramentos::~tela_monitoramentos()
{
    delete ui;
}

//Button that calls main function
void tela_monitoramentos::on_btn_iniciar_video_clicked()
{
    std::cout << "Calling main function" << std::endl;
    main_func();
}

int main_func(){
    
           class ThreadConect_video : public QThread { //Creating an othre thread
           private:
               tela_monitoramentos* chatInstance; //Instanciating signal

           public:
               void run() override {

                   //My example code
                 
                   while(true) {
                     
                      /*
                      #
                      # CODE THAT RECEIVES THE BUFFER CONTENT 
                      #
                      */
                        std::cout << "Calling function thar loads the buffer into the widget" << std::endl;
                        emit chatInstance->call_frameUpdate();                        
                        usleep(1000); //Waits for 1 second before loading the next frame

                    }//End of while
           };

           //Starting my new thread

           ThreadConect_video *thread = new ThreadConect_video;
           thread -> start(); 
    return 0;
}
void tela_monitoramentos::frame_update() //Function that loads the Bite Array into the widget
{
    std::cout << "Received Signal" << std::endl;
    scene = new QGraphicsScene(this);
    ui->tela_pc->setScene(scene);
    ui->tela_pc->scene()->addPixmap(QPixmap::fromImage(QImage::fromData(reinterpret_cast<unsigned char*>(video_buffer), sizeof(video_buffer))));
}

.H

//Header
namespace Ui {
class tela_monitoramentos;
}

class tela_monitoramentos : public QDialog
{
    Q_OBJECT

public:
    explicit tela_monitoramentos(QWidget *parent = nullptr);
    ~tela_monitoramentos();

private slots:

    void frame_update(); //The function I want to call
    
    void on_btn_iniciar_video_clicked(); 


private:
    Ui::tela_monitoramentos *ui;
    QTimer* timer;
    void keyPressEvent(QKeyEvent *event);

protected:
    void showEvent(QShowEvent* event) override;

signals:
    void call_frameUpdate(); //My signal
};

Calling:

scene = new QGraphicsScene(this);
ui->tela_pc->setScene(scene);
ui->tela_pc->scene()->addPixmap(QPixmap::fromImage(QImage::fromData(reinterpret_cast<unsigned char*>(video_buffer), 
                                sizeof(video_buffer))));

once from a simple button works properly by the way.

0

There are 0 answers