Сообщение Qt 5.6.2 GUI от 21.06.2019 11:56
Изменено 21.06.2019 13:16 milkpot
Qt 5.6.2 GUI
Программа получает от устройства пакеты данных, используя Winpcap драйвер.
В ответ на посылку команды "получить данные" идет прием данных от устройства блоками по 662 байта.
Данные — это заполняемые на приеме два двухмерных массива данных 640Х480.
После получения данных в дочерней нити стоит вызов signal/slot функции отрисовывающей массивы данных, используя QGraphicsView/QGraphicsScene.
Отрисовка данных возможно замедляет прием данных.
Вопрос состоит в том, как организовать отрисовку получаемых данных в случае, когда данные приходят с большим темпом.
Фрагменты кода:
В ответ на посылку команды "получить данные" идет прием данных от устройства блоками по 662 байта.
Данные — это заполняемые на приеме два двухмерных массива данных 640Х480.
После получения данных в дочерней нити стоит вызов signal/slot функции отрисовывающей массивы данных, используя QGraphicsView/QGraphicsScene.
Отрисовка данных возможно замедляет прием данных.
Вопрос состоит в том, как организовать отрисовку получаемых данных в случае, когда данные приходят с большим темпом.
Фрагменты кода:
// Вызывается в дочерней нити
void read(pcap_t *adhandle,struct pcap_pkthdr *header,const u_char *pkt_data)
{
int i=0;
int res;
int i1;
int loc_counter=0;
bool b_array_8bit=true;
bool b_array_infrared=true;
QString str;
while(allow_lock &&(res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0) {;
// if((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0) {;
if(res == 0)
/* Timeout elapsed */
/*return*/ continue;
i=0;i1=header->len;
// pChar=(char* )pkt_data;
;
if( /*(*/pkt_data[6]==0x22 && pkt_data[7]==0x11 && pkt_data[8]==0x11 &&
pkt_data[9]==0x11 && pkt_data[10]==0x11 && pkt_data[11]==0x11/*) ||*/
/*pkt_data[0]==0x22 && pkt_data[1]==0x11 && pkt_data[2]==0x11 &&
pkt_data[3]==0x11 && pkt_data[4]==0x11 && pkt_data[5]==0x11*/) {
loc_counter++;
;
if(b_array_8bit==true)
{
if(loc_counter==1) b_array_infrared=false;
for(int i=22;i<i1;i++)
{// Заполнение массива данных
::imageArray[::ArrayCounter]=pkt_data[i];
::ArrayCounter++;
}
if(::ArrayCounter==::ArraySize)
{
::ArrayCounter=0;
loc_counter=0;
b_array_8bit=false;
b_array_infrared=true;
continue;
}
}
if(b_array_infrared==true)
{
for(int i=22;i<i1;i++)
{// Заполнение массива данных
::imageArrayInfraRed[::ArrayCounter]=pkt_data[i];
::ArrayCounter++;
::ArrayCounterCopy=::ArrayCounter;
}
if(::ArrayCounter==::ArraySize)
{
::ArrayCounter=0;
loc_counter=0;
b_array_8bit=true;
b_array_infrared=false;
// str=QString(tr(" Кадр инфра принят"));
// do_send_result3(str);
;
if(0x5!=::InterlockedExchangeAdd((PLONG) & ::v_operation_cntrl_value,0) )
::InterlockedExchange( (long *) & ::v_operation_value, 0 );
//---------------- Отрисовка данных
do_Display_data3();
do_Display_data31();
// write_images_to_file();
if(0x5==::InterlockedExchangeAdd((PLONG) & ::v_operation_cntrl_value,0) )
do_cycledataDisplay3();
}
;
}
}
;}
if(res == -1){
printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
return ;
}
}
class Worker3 : public QObject
{
Q_OBJECT
public:
// /*explicit */Worker(QObject *parent = 0);
// Worker::Worker(QObject *parent) : QObject(parent)
// {
// }
signals:
void resultReady3(const QString &result);
void dataDisplay3(/*const std::vector<unsigned char> &*/);
void dataDisplay31();
//void write_to_file();
void cycledataDisplay3();
public slots:
void do_send_result3(const QString &info_msg)
{
emit resultReady3(info_msg);
}
void do_Display_data3(/*const std::vector<unsigned char> &vect*/ )
{
emit dataDisplay3(/*vect*/);
}
void do_Display_data31(/*const std::vector<unsigned char> &vect*/ )
{
emit dataDisplay31(/*vect*/);
}
void do_cycledataDisplay3()
{
emit cycledataDisplay3();
}
class Controller3 : public QObject
{
Q_OBJECT
QThread workerThread;
QMutex localMutex;
// QTimer *work_Timer;
Worker3 *worker;
// friend MainWindow* window_internal;
public:
Controller3(MainWindow *window) {
worker = new Worker3();
// window_internal=window;
worker->moveToThread(&workerThread);
connect(this, &Controller3::operate, worker, &Worker3::doWork3);
// connect(worker, &Worker::resultReady, this, &Controller::handleResults);
connect(worker, &Worker3::resultReady3, window, &MainWindow::appendtoLogListBox);
// Отрисовка данных
connect(worker, &Worker3::dataDisplay3, window, &MainWindow::displayDataPixmap);
connect(worker, &Worker3::dataDisplay31, window, &MainWindow::displayDataPixmapInfraRed);
connect(worker, &Worker3::cycledataDisplay3, window, &MainWindow::on_pushButton_7_clicked);
qDebug() << "--- Controller3: Constructor";
}
void do_start(const QString ¶m )
{
workerThread.start();
// worker->doWork(QString(tr(" --- Controller: (1) do_start")));
// do_contrl_send_msg(QString(tr(" --- Controller: (2) do_start")));
do_contrl_send_msg(param);
}
void do_contrl_send_msg(const QString &msg)
{
emit operate(msg);
}
~Controller3() {
workerThread.quit();
workerThread.wait();
qDebug() << "--- Controller: Destructor";
}
public slots:
signals:
void operate(const QString &);
};
void MainWindow::displayDataPixmap(/*const std::vector<unsigned char> &data_vect*/)
{
;
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)&buffer_vect[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, QImage::Format_Indexed8 ) );
mutex.lock();
pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)& ::imageArray[0]/*::videoBuffer[0]*/ /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
graphicspixmapItem=scene->addPixmap(pixmap_copy);
mutex.unlock();
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *) ::videoBuffer3d /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)& ::fullVideoMatrix.videoMatrix[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
}
void MainWindow::displayDataPixmapInfraRed(/*const std::vector<unsigned char> &data_vect*/)
{
;
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)&buffer_vect[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, QImage::Format_Indexed8 ) );
mutex_2.lock();
/*pixmap_copy*/pixmap=QPixmap::fromImage( QImage( (unsigned char *)& ::imageArrayInfraRed[0]/*::videoBuffer[0]*/ /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
graphicspixmapItemSecond=scene_2->addPixmap(pixmap);
mutex_2.unlock();
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *) ::videoBuffer3d /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)& ::fullVideoMatrix.videoMatrix[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
}
static BYTE imageArray[307200/*640*480*/]={0};
static BYTE imageArrayInfraRed[307200/*640*480*/]={0};
static int ArraySize=307200;
static int ArrayCounter=0;
static int ArrayCounterCopy;
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
Ui::MainWindow *ui;
// QComboBoxImpl *ComboBoxImpl;
QGraphicsScene *scene;
QGraphicsScene *scene_2;
QRect rect1;
QSize rct_size;
// QPicture myPicx;
QTimer *timer;
QMutex mutex;
QMutex mutex_2;
...
};
Qt 5.6.2 GUI
Программа получает от устройства пакеты данных, используя Winpcap драйвер.
В ответ на посылку команды "получить данные" идет прием данных от устройства блоками по 662 байта.
Данные — это заполняемые на приеме два двухмерных массива данных 640Х480.
После получения данных в дочерней нити стоит вызов signal/slot функции отрисовывающей массивы данных, используя QGraphicsView/QGraphicsScene.
Отрисовка данных возможно замедляет прием данных.
Вопрос состоит в том, как организовать отрисовку получаемых данных в случае, когда данные приходят с большим темпом.
Фрагменты кода:
В ответ на посылку команды "получить данные" идет прием данных от устройства блоками по 662 байта.
Данные — это заполняемые на приеме два двухмерных массива данных 640Х480.
После получения данных в дочерней нити стоит вызов signal/slot функции отрисовывающей массивы данных, используя QGraphicsView/QGraphicsScene.
Отрисовка данных возможно замедляет прием данных.
Вопрос состоит в том, как организовать отрисовку получаемых данных в случае, когда данные приходят с большим темпом.
Фрагменты кода:
// Вызывается в дочерней нити
void read(pcap_t *adhandle,struct pcap_pkthdr *header,const u_char *pkt_data)
{
int i=0;
int res;
int i1;
int loc_counter=0;
bool b_array_8bit=true;
bool b_array_infrared=true;
QString str;
while(allow_lock &&(res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0) {;
// if((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0) {;
if(res == 0)
/* Timeout elapsed */
/*return*/ continue;
i=0;i1=header->len;
// pChar=(char* )pkt_data;
;
if( /*(*/pkt_data[6]==0x22 && pkt_data[7]==0x11 && pkt_data[8]==0x11 &&
pkt_data[9]==0x11 && pkt_data[10]==0x11 && pkt_data[11]==0x11/*) ||*/
/*pkt_data[0]==0x22 && pkt_data[1]==0x11 && pkt_data[2]==0x11 &&
pkt_data[3]==0x11 && pkt_data[4]==0x11 && pkt_data[5]==0x11*/) {
loc_counter++;
;
if(b_array_8bit==true)
{
if(loc_counter==1) b_array_infrared=false;
for(int i=22;i<i1;i++)
{// Заполнение массива данных
::imageArray[::ArrayCounter]=pkt_data[i];
::ArrayCounter++;
}
if(::ArrayCounter==::ArraySize)
{
::ArrayCounter=0;
loc_counter=0;
b_array_8bit=false;
b_array_infrared=true;
continue;
}
}
if(b_array_infrared==true)
{
for(int i=22;i<i1;i++)
{// Заполнение массива данных
::imageArrayInfraRed[::ArrayCounter]=pkt_data[i];
::ArrayCounter++;
::ArrayCounterCopy=::ArrayCounter;
}
if(::ArrayCounter==::ArraySize)
{
::ArrayCounter=0;
loc_counter=0;
b_array_8bit=true;
b_array_infrared=false;
// str=QString(tr(" Кадр инфра принят"));
// do_send_result3(str);
;
if(0x5!=::InterlockedExchangeAdd((PLONG) & ::v_operation_cntrl_value,0) )
::InterlockedExchange( (long *) & ::v_operation_value, 0 );
//---------------- Отрисовка данных
do_Display_data3();
do_Display_data31();
// write_images_to_file();
if(0x5==::InterlockedExchangeAdd((PLONG) & ::v_operation_cntrl_value,0) )
do_cycledataDisplay3();
}
;
}
}
;}
if(res == -1){
printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
return ;
}
}
class Worker3 : public QObject
{
Q_OBJECT
public:
// /*explicit */Worker(QObject *parent = 0);
// Worker::Worker(QObject *parent) : QObject(parent)
// {
// }
signals:
void resultReady3(const QString &result);
void dataDisplay3();
void dataDisplay31();
//void write_to_file();
void cycledataDisplay3();
public slots:
void do_send_result3(const QString &info_msg)
{
emit resultReady3(info_msg);
}
void do_Display_data3(/*const std::vector<unsigned char> &vect*/ )
{
emit dataDisplay3();
}
void do_Display_data31(/*const std::vector<unsigned char> &vect*/ )
{
emit dataDisplay31();
}
void do_cycledataDisplay3()
{
emit cycledataDisplay3();
}
class Controller3 : public QObject
{
Q_OBJECT
QThread workerThread;
QMutex localMutex;
// QTimer *work_Timer;
Worker3 *worker;
// friend MainWindow* window_internal;
public:
Controller3(MainWindow *window) {
worker = new Worker3();
// window_internal=window;
worker->moveToThread(&workerThread);
connect(this, &Controller3::operate, worker, &Worker3::doWork3);
// connect(worker, &Worker::resultReady, this, &Controller::handleResults);
connect(worker, &Worker3::resultReady3, window, &MainWindow::appendtoLogListBox);
// Отрисовка данных
connect(worker, &Worker3::dataDisplay3, window, &MainWindow::displayDataPixmap);
connect(worker, &Worker3::dataDisplay31, window, &MainWindow::displayDataPixmapInfraRed);
connect(worker, &Worker3::cycledataDisplay3, window, &MainWindow::on_pushButton_7_clicked);
qDebug() << "--- Controller3: Constructor";
}
void do_start(const QString ¶m )
{
workerThread.start();
// worker->doWork(QString(tr(" --- Controller: (1) do_start")));
// do_contrl_send_msg(QString(tr(" --- Controller: (2) do_start")));
do_contrl_send_msg(param);
}
void do_contrl_send_msg(const QString &msg)
{
emit operate(msg);
}
~Controller3() {
workerThread.quit();
workerThread.wait();
qDebug() << "--- Controller: Destructor";
}
public slots:
signals:
void operate(const QString &);
};
void MainWindow::displayDataPixmap(/*const std::vector<unsigned char> &data_vect*/)
{
;
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)&buffer_vect[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, QImage::Format_Indexed8 ) );
mutex.lock();
pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)& ::imageArray[0]/*::videoBuffer[0]*/ /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
graphicspixmapItem=scene->addPixmap(pixmap_copy);
mutex.unlock();
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *) ::videoBuffer3d /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)& ::fullVideoMatrix.videoMatrix[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
}
void MainWindow::displayDataPixmapInfraRed(/*const std::vector<unsigned char> &data_vect*/)
{
;
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)&buffer_vect[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, QImage::Format_Indexed8 ) );
mutex_2.lock();
/*pixmap_copy*/pixmap=QPixmap::fromImage( QImage( (unsigned char *)& ::imageArrayInfraRed[0]/*::videoBuffer[0]*/ /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
graphicspixmapItemSecond=scene_2->addPixmap(pixmap);
mutex_2.unlock();
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *) ::videoBuffer3d /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
// pixmap_copy=QPixmap::fromImage( QImage( (unsigned char *)& ::fullVideoMatrix.videoMatrix[0] /*imageData*/,rct_size.width() /*image_width*/,rct_size.height() /*image_height*/, /*QImage::Format_Indexed8*/QImage::Format_Grayscale8 ) );
}
static BYTE imageArray[307200/*640*480*/]={0};
static BYTE imageArrayInfraRed[307200/*640*480*/]={0};
static int ArraySize=307200;
static int ArrayCounter=0;
static int ArrayCounterCopy;
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
Ui::MainWindow *ui;
// QComboBoxImpl *ComboBoxImpl;
QGraphicsScene *scene;
QGraphicsScene *scene_2;
QRect rect1;
QSize rct_size;
// QPicture myPicx;
QTimer *timer;
QMutex mutex;
QMutex mutex_2;
...
};