Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT Timer issue

QT Timer issue

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 241 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VJ01
    wrote on 9 Sept 2022, 07:22 last edited by
    #1

    Hi

    I’m hoping someone could help me, I have used QT before but not a lot so I’m still new to it and am having an issue with my timer well I believe it is a timer issue. I have a feed from a camera which is live, of course I first used a while loop to update my frames however that caused the Ui to stop working, so I decided to use a timer to grab the frames from the camera.

    i will post my code below
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "GxIAPI.h"
    #include "DxImageProc.h"
    #include <opencv4/opencv2/core/core.hpp>
    #include <opencv4/opencv2/imgproc/imgproc.hpp>
    #include <opencv4/opencv2/opencv.hpp>
    #include <opencv4/opencv2/highgui/highgui.hpp>
    #include <QTimer>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public:
    void framegrab(cv::Mat src);
    void take_picture();
    
    protected:
        void timerEvent(QTimerEvent *event);
    
    
    private:
        Ui::MainWindow *ui;
        void DisplayImage();
        int timerId;
    };
    
    
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "GxIAPI.h"
    #include "DxImageProc.h"
    #include "qdebug.h"
    #include <opencv4/opencv2/highgui/highgui.hpp>
    #include <iostream>
    #include <opencv4/opencv2/core/core.hpp>
    #include <opencv4/opencv2/imgproc/imgproc.hpp>
    #include <opencv4/opencv2/opencv.hpp>
    #include "qthread.h"
    #include <QTimer>
    
    //-----------------------Camera Variables--------------------------
    
    GX_OPEN_PARAM stOpenParam;
    GX_DEV_HANDLE g_hDevice = NULL;
    GX_FRAME_DATA g_frameData;
    pthread_t g_hThreadAcq;
    bool g_bGetImg = false;
    
    bool m_Is_implemented;
    int64_t m_pixel_color;
    char *m_rgb_image = NULL;
    cv::Mat m_image;
    
    int live_or_picture = 1;
    
    
    
    
    
     //-----------------------OpenCV Variables------------------------
    
    
    using namespace cv;
    using namespace std;
    
    
     //-----------------------general Variables-----------------------
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        /////////////////////////////start////////////////////////
    
    
      timerId = startTimer(1);
    
        ui->label->setGeometry(10, 0, 641, 361);
        ui->label->setScaledContents( true );
        ui->label->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
    
    //-----------------------Get Camera Version-----------------------
    
        GX_STATUS status = GX_STATUS_SUCCESS;
        status = GXInitLib();
    
        if (status != GX_STATUS_SUCCESS)
        {
        qDebug() << "Libary Error";
        }
        else{
            qDebug() << "Libary Loaded";
        }
    
        uint32_t nDeviceNum = 0;
    
    
        status = GXUpdateDeviceList(&nDeviceNum, 1000);
    
        if(nDeviceNum <=0)
            {
                qDebug() << "No device is currently available";
            }
        else
            {
            GX_DEVICE_BASE_INFO *pBaseinfo = new GX_DEVICE_BASE_INFO[nDeviceNum];
                    size_t iSize = nDeviceNum * sizeof(GX_DEVICE_BASE_INFO);
                    GXGetAllDeviceBaseInfo(pBaseinfo, &iSize);
                    qDebug() << "Serial number of the first device : " << pBaseinfo[0].szSN;
                    qDebug() << "Model number of the first device : " << pBaseinfo[0].szDisplayName;
    
                    //Opens the device via a serial number
                    stOpenParam.accessMode = GX_ACCESS_EXCLUSIVE;
                    stOpenParam.openMode = GX_OPEN_INDEX;
                    //stOpenParam.pszContent = pBaseinfo[0].szSN;
                    stOpenParam.pszContent = "1";
    
                    status = GXOpenDevice(&stOpenParam, &g_hDevice);
                    //status = GXOpenDeviceByIndex(1, &g_hDevice);
    
                    if(status == GX_STATUS_SUCCESS)
                    {
                        qDebug() << "Open the device successfully";
    
                        size_t unSize = 128;
                         char  pszContent[128] = {'\0'};
                        status = GXGetString(g_hDevice, GX_STRING_DEVICE_FIRMWARE_VERSION, pszContent,&unSize);
                        qDebug("Firmware version:%s", pszContent);
    
                        size_t unSize2 = 128;
                        char  pszContent2[128] = {'\0'};
                        status = GXGetString(g_hDevice, GX_STRING_DEVICE_VERSION, pszContent2, &unSize2);
                        qDebug("FPGA version:%s", pszContent2);
    
                        int64_t width,height;
                        status = GXGetInt(g_hDevice,GX_INT_WIDTH,&width);
                        status = GXGetInt(g_hDevice,GX_INT_HEIGHT,&height);
                        qDebug() << "Width:" << width;
                        qDebug() << "height" << height;
    
                        status=GXIsImplemented(g_hDevice,GX_ENUM_PIXEL_COLOR_FILTER,&m_Is_implemented);
    
                        if(m_Is_implemented)
                        {
                             qDebug() << "Colour Mode";
                             status= GXGetEnum(g_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_pixel_color);
    
                        }else{
    
                             qDebug() << "Mono Mode";
                        }
    
                        status = GXSetEnum(g_hDevice, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
                        status = GXSetEnum(g_hDevice, GX_ENUM_TRIGGER_MODE, GX_TRIGGER_MODE_OFF);
                        int64_t nPayLoadSize = 0;
                        status = GXGetInt(g_hDevice, GX_INT_PAYLOAD_SIZE, &nPayLoadSize);
                        g_frameData.pImgBuf = malloc(nPayLoadSize);
                        g_bGetImg = true;
                        status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_START);
    
    if(live_or_picture == 1){
                //--------------------------------------------------LIVE VERSION-------------------------------------------------
    
        qDebug() << "Live Feed Start";
    
    
            //--------------------------------------------------END LIVE VERSION-------------------------------------------------
    }else{
            //--------------------------------------------------PICTURE VERSION------------------------------------------------
    
        qDebug() << "Take Picture";
        take_picture();
    
              //--------------------------------------------------END PICTURE VERSION------------------------------------------------
    }
                    }else
                    {
                       //
                        qDebug() << "Failed to open device";
                    }
    
        }
    
    
    
    //----------------------------------------------------------------
    
    }
    
    MainWindow::~MainWindow()
    {
         killTimer(timerId);
    
        GX_STATUS status = GX_STATUS_SUCCESS;
    
        //--------------------------------------------------Add For Live Version-------------------------------------------------
        if(live_or_picture == 1){
            status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_STOP);
            free(g_frameData.pImgBuf);
        }
        //--------------------------------------------------END-------------------------------------------------
    
        status = GXCloseDevice(g_hDevice);
        if (status == GX_STATUS_SUCCESS)
        {
        qDebug() << "Closed the device successfully";
        }else{
        qDebug() << "Device not closed successfully";
        }
        status = GXCloseLib();
        qDebug() << "Closed the library successfully";
        qDebug() << "GOODBYE";
        delete ui;
    }
    
    void MainWindow::framegrab(cv::Mat src)
    {
        //QApplication::processEvents();
    
        QImage img = QImage(src.data,g_frameData.nWidth,g_frameData.nHeight, QImage::Format_RGB888).rgbSwapped().copy();
        //cv::imwrite("test.jpg", src);
    
        QPixmap pixel;
        pixel = QPixmap::fromImage(img);
    
        ui->label->setPixmap(pixel);
        ui->label->update();
    }
    
    void MainWindow::take_picture(){
        GX_STATUS status = GX_STATUS_SUCCESS;
    
        while(GXGetImage(g_hDevice, &g_frameData, 100) != GX_STATUS_SUCCESS)
         {
         QThread::sleep(1);
         }
        if (g_frameData.nStatus == GX_FRAME_STATUS_SUCCESS)
        {
         printf("Collection succeeded: Width:%d Height:%d\n", g_frameData.nWidth, g_frameData.nHeight);
    
         //Acquiring image is successful.
         cv::Mat src;
         m_rgb_image = new char[g_frameData.nWidth*g_frameData.nHeight*3];
         src.create(g_frameData.nHeight,g_frameData.nWidth,CV_8UC3);
         memcpy(src.data,g_frameData.pImgBuf,g_frameData.nWidth*g_frameData.nHeight);
         DxRaw8toRGB24(g_frameData.pImgBuf,m_rgb_image,g_frameData.nWidth, g_frameData.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERGB),false);
         memcpy(src.data,m_rgb_image,g_frameData.nWidth*g_frameData.nHeight*3);
    
         framegrab(src);
    
        }else
        {
            printf("Collection exception: exception code:%d\n", g_frameData.nStatus);
        }
        status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_STOP);
        free(g_frameData.pImgBuf);
    }
    
    
    
    void MainWindow::timerEvent(QTimerEvent *event)
    {
    
        GX_STATUS status = GX_STATUS_SUCCESS;
    
        if(g_frameData.pImgBuf == NULL)
          {
           //continue;
          }else{
    
    
    status = GXGetImage(g_hDevice, &g_frameData, 100);
    
    if(status == GX_STATUS_SUCCESS)
    {
    
     if(g_frameData.nStatus == 0)
     {
    cv::Mat src;
         m_rgb_image = new char[g_frameData.nWidth*g_frameData.nHeight*3];
         src.create(g_frameData.nHeight,g_frameData.nWidth,CV_8UC3);
         memcpy(src.data,g_frameData.pImgBuf,g_frameData.nWidth*g_frameData.nHeight);
         DxRaw8toRGB24(g_frameData.pImgBuf,m_rgb_image,g_frameData.nWidth, g_frameData.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERGB),false);
         memcpy(src.data,m_rgb_image,g_frameData.nWidth*g_frameData.nHeight*3);
    
         framegrab(src);
     }
    }
    }
    }
    

    my code starts a timer to grab a frame from the camera and pass it to framegrab(), it works well until a certain point after say a minute my code just crashes and I get an error on my console output saying killed. now I’m not sure if it’s my camera or I have completely messed up the whole timer section, as it is my first attempt in using the timer class. if anyone could help that would be great

    Thank you

    Christian EhrlicherC JonBJ 2 Replies Last reply 9 Sept 2022, 07:29
    0
    • V VJ01
      9 Sept 2022, 07:22

      Hi

      I’m hoping someone could help me, I have used QT before but not a lot so I’m still new to it and am having an issue with my timer well I believe it is a timer issue. I have a feed from a camera which is live, of course I first used a while loop to update my frames however that caused the Ui to stop working, so I decided to use a timer to grab the frames from the camera.

      i will post my code below
      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include "GxIAPI.h"
      #include "DxImageProc.h"
      #include <opencv4/opencv2/core/core.hpp>
      #include <opencv4/opencv2/imgproc/imgproc.hpp>
      #include <opencv4/opencv2/opencv.hpp>
      #include <opencv4/opencv2/highgui/highgui.hpp>
      #include <QTimer>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      public:
      void framegrab(cv::Mat src);
      void take_picture();
      
      protected:
          void timerEvent(QTimerEvent *event);
      
      
      private:
          Ui::MainWindow *ui;
          void DisplayImage();
          int timerId;
      };
      
      
      
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "GxIAPI.h"
      #include "DxImageProc.h"
      #include "qdebug.h"
      #include <opencv4/opencv2/highgui/highgui.hpp>
      #include <iostream>
      #include <opencv4/opencv2/core/core.hpp>
      #include <opencv4/opencv2/imgproc/imgproc.hpp>
      #include <opencv4/opencv2/opencv.hpp>
      #include "qthread.h"
      #include <QTimer>
      
      //-----------------------Camera Variables--------------------------
      
      GX_OPEN_PARAM stOpenParam;
      GX_DEV_HANDLE g_hDevice = NULL;
      GX_FRAME_DATA g_frameData;
      pthread_t g_hThreadAcq;
      bool g_bGetImg = false;
      
      bool m_Is_implemented;
      int64_t m_pixel_color;
      char *m_rgb_image = NULL;
      cv::Mat m_image;
      
      int live_or_picture = 1;
      
      
      
      
      
       //-----------------------OpenCV Variables------------------------
      
      
      using namespace cv;
      using namespace std;
      
      
       //-----------------------general Variables-----------------------
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          /////////////////////////////start////////////////////////
      
      
        timerId = startTimer(1);
      
          ui->label->setGeometry(10, 0, 641, 361);
          ui->label->setScaledContents( true );
          ui->label->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
      
      //-----------------------Get Camera Version-----------------------
      
          GX_STATUS status = GX_STATUS_SUCCESS;
          status = GXInitLib();
      
          if (status != GX_STATUS_SUCCESS)
          {
          qDebug() << "Libary Error";
          }
          else{
              qDebug() << "Libary Loaded";
          }
      
          uint32_t nDeviceNum = 0;
      
      
          status = GXUpdateDeviceList(&nDeviceNum, 1000);
      
          if(nDeviceNum <=0)
              {
                  qDebug() << "No device is currently available";
              }
          else
              {
              GX_DEVICE_BASE_INFO *pBaseinfo = new GX_DEVICE_BASE_INFO[nDeviceNum];
                      size_t iSize = nDeviceNum * sizeof(GX_DEVICE_BASE_INFO);
                      GXGetAllDeviceBaseInfo(pBaseinfo, &iSize);
                      qDebug() << "Serial number of the first device : " << pBaseinfo[0].szSN;
                      qDebug() << "Model number of the first device : " << pBaseinfo[0].szDisplayName;
      
                      //Opens the device via a serial number
                      stOpenParam.accessMode = GX_ACCESS_EXCLUSIVE;
                      stOpenParam.openMode = GX_OPEN_INDEX;
                      //stOpenParam.pszContent = pBaseinfo[0].szSN;
                      stOpenParam.pszContent = "1";
      
                      status = GXOpenDevice(&stOpenParam, &g_hDevice);
                      //status = GXOpenDeviceByIndex(1, &g_hDevice);
      
                      if(status == GX_STATUS_SUCCESS)
                      {
                          qDebug() << "Open the device successfully";
      
                          size_t unSize = 128;
                           char  pszContent[128] = {'\0'};
                          status = GXGetString(g_hDevice, GX_STRING_DEVICE_FIRMWARE_VERSION, pszContent,&unSize);
                          qDebug("Firmware version:%s", pszContent);
      
                          size_t unSize2 = 128;
                          char  pszContent2[128] = {'\0'};
                          status = GXGetString(g_hDevice, GX_STRING_DEVICE_VERSION, pszContent2, &unSize2);
                          qDebug("FPGA version:%s", pszContent2);
      
                          int64_t width,height;
                          status = GXGetInt(g_hDevice,GX_INT_WIDTH,&width);
                          status = GXGetInt(g_hDevice,GX_INT_HEIGHT,&height);
                          qDebug() << "Width:" << width;
                          qDebug() << "height" << height;
      
                          status=GXIsImplemented(g_hDevice,GX_ENUM_PIXEL_COLOR_FILTER,&m_Is_implemented);
      
                          if(m_Is_implemented)
                          {
                               qDebug() << "Colour Mode";
                               status= GXGetEnum(g_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_pixel_color);
      
                          }else{
      
                               qDebug() << "Mono Mode";
                          }
      
                          status = GXSetEnum(g_hDevice, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
                          status = GXSetEnum(g_hDevice, GX_ENUM_TRIGGER_MODE, GX_TRIGGER_MODE_OFF);
                          int64_t nPayLoadSize = 0;
                          status = GXGetInt(g_hDevice, GX_INT_PAYLOAD_SIZE, &nPayLoadSize);
                          g_frameData.pImgBuf = malloc(nPayLoadSize);
                          g_bGetImg = true;
                          status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_START);
      
      if(live_or_picture == 1){
                  //--------------------------------------------------LIVE VERSION-------------------------------------------------
      
          qDebug() << "Live Feed Start";
      
      
              //--------------------------------------------------END LIVE VERSION-------------------------------------------------
      }else{
              //--------------------------------------------------PICTURE VERSION------------------------------------------------
      
          qDebug() << "Take Picture";
          take_picture();
      
                //--------------------------------------------------END PICTURE VERSION------------------------------------------------
      }
                      }else
                      {
                         //
                          qDebug() << "Failed to open device";
                      }
      
          }
      
      
      
      //----------------------------------------------------------------
      
      }
      
      MainWindow::~MainWindow()
      {
           killTimer(timerId);
      
          GX_STATUS status = GX_STATUS_SUCCESS;
      
          //--------------------------------------------------Add For Live Version-------------------------------------------------
          if(live_or_picture == 1){
              status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_STOP);
              free(g_frameData.pImgBuf);
          }
          //--------------------------------------------------END-------------------------------------------------
      
          status = GXCloseDevice(g_hDevice);
          if (status == GX_STATUS_SUCCESS)
          {
          qDebug() << "Closed the device successfully";
          }else{
          qDebug() << "Device not closed successfully";
          }
          status = GXCloseLib();
          qDebug() << "Closed the library successfully";
          qDebug() << "GOODBYE";
          delete ui;
      }
      
      void MainWindow::framegrab(cv::Mat src)
      {
          //QApplication::processEvents();
      
          QImage img = QImage(src.data,g_frameData.nWidth,g_frameData.nHeight, QImage::Format_RGB888).rgbSwapped().copy();
          //cv::imwrite("test.jpg", src);
      
          QPixmap pixel;
          pixel = QPixmap::fromImage(img);
      
          ui->label->setPixmap(pixel);
          ui->label->update();
      }
      
      void MainWindow::take_picture(){
          GX_STATUS status = GX_STATUS_SUCCESS;
      
          while(GXGetImage(g_hDevice, &g_frameData, 100) != GX_STATUS_SUCCESS)
           {
           QThread::sleep(1);
           }
          if (g_frameData.nStatus == GX_FRAME_STATUS_SUCCESS)
          {
           printf("Collection succeeded: Width:%d Height:%d\n", g_frameData.nWidth, g_frameData.nHeight);
      
           //Acquiring image is successful.
           cv::Mat src;
           m_rgb_image = new char[g_frameData.nWidth*g_frameData.nHeight*3];
           src.create(g_frameData.nHeight,g_frameData.nWidth,CV_8UC3);
           memcpy(src.data,g_frameData.pImgBuf,g_frameData.nWidth*g_frameData.nHeight);
           DxRaw8toRGB24(g_frameData.pImgBuf,m_rgb_image,g_frameData.nWidth, g_frameData.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERGB),false);
           memcpy(src.data,m_rgb_image,g_frameData.nWidth*g_frameData.nHeight*3);
      
           framegrab(src);
      
          }else
          {
              printf("Collection exception: exception code:%d\n", g_frameData.nStatus);
          }
          status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_STOP);
          free(g_frameData.pImgBuf);
      }
      
      
      
      void MainWindow::timerEvent(QTimerEvent *event)
      {
      
          GX_STATUS status = GX_STATUS_SUCCESS;
      
          if(g_frameData.pImgBuf == NULL)
            {
             //continue;
            }else{
      
      
      status = GXGetImage(g_hDevice, &g_frameData, 100);
      
      if(status == GX_STATUS_SUCCESS)
      {
      
       if(g_frameData.nStatus == 0)
       {
      cv::Mat src;
           m_rgb_image = new char[g_frameData.nWidth*g_frameData.nHeight*3];
           src.create(g_frameData.nHeight,g_frameData.nWidth,CV_8UC3);
           memcpy(src.data,g_frameData.pImgBuf,g_frameData.nWidth*g_frameData.nHeight);
           DxRaw8toRGB24(g_frameData.pImgBuf,m_rgb_image,g_frameData.nWidth, g_frameData.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERGB),false);
           memcpy(src.data,m_rgb_image,g_frameData.nWidth*g_frameData.nHeight*3);
      
           framegrab(src);
       }
      }
      }
      }
      

      my code starts a timer to grab a frame from the camera and pass it to framegrab(), it works well until a certain point after say a minute my code just crashes and I get an error on my console output saying killed. now I’m not sure if it’s my camera or I have completely messed up the whole timer section, as it is my first attempt in using the timer class. if anyone could help that would be great

      Thank you

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Sept 2022, 07:29 last edited by
      #2

      @VJ01 said in QT Timer issue:

      it works well until a certain point after say a minute my code just crashes

      That's what a debugger is for.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • V VJ01
        9 Sept 2022, 07:22

        Hi

        I’m hoping someone could help me, I have used QT before but not a lot so I’m still new to it and am having an issue with my timer well I believe it is a timer issue. I have a feed from a camera which is live, of course I first used a while loop to update my frames however that caused the Ui to stop working, so I decided to use a timer to grab the frames from the camera.

        i will post my code below
        mainwindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include "GxIAPI.h"
        #include "DxImageProc.h"
        #include <opencv4/opencv2/core/core.hpp>
        #include <opencv4/opencv2/imgproc/imgproc.hpp>
        #include <opencv4/opencv2/opencv.hpp>
        #include <opencv4/opencv2/highgui/highgui.hpp>
        #include <QTimer>
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        
        public:
        void framegrab(cv::Mat src);
        void take_picture();
        
        protected:
            void timerEvent(QTimerEvent *event);
        
        
        private:
            Ui::MainWindow *ui;
            void DisplayImage();
            int timerId;
        };
        
        
        
        #endif // MAINWINDOW_H
        
        

        mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "GxIAPI.h"
        #include "DxImageProc.h"
        #include "qdebug.h"
        #include <opencv4/opencv2/highgui/highgui.hpp>
        #include <iostream>
        #include <opencv4/opencv2/core/core.hpp>
        #include <opencv4/opencv2/imgproc/imgproc.hpp>
        #include <opencv4/opencv2/opencv.hpp>
        #include "qthread.h"
        #include <QTimer>
        
        //-----------------------Camera Variables--------------------------
        
        GX_OPEN_PARAM stOpenParam;
        GX_DEV_HANDLE g_hDevice = NULL;
        GX_FRAME_DATA g_frameData;
        pthread_t g_hThreadAcq;
        bool g_bGetImg = false;
        
        bool m_Is_implemented;
        int64_t m_pixel_color;
        char *m_rgb_image = NULL;
        cv::Mat m_image;
        
        int live_or_picture = 1;
        
        
        
        
        
         //-----------------------OpenCV Variables------------------------
        
        
        using namespace cv;
        using namespace std;
        
        
         //-----------------------general Variables-----------------------
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            /////////////////////////////start////////////////////////
        
        
          timerId = startTimer(1);
        
            ui->label->setGeometry(10, 0, 641, 361);
            ui->label->setScaledContents( true );
            ui->label->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
        
        //-----------------------Get Camera Version-----------------------
        
            GX_STATUS status = GX_STATUS_SUCCESS;
            status = GXInitLib();
        
            if (status != GX_STATUS_SUCCESS)
            {
            qDebug() << "Libary Error";
            }
            else{
                qDebug() << "Libary Loaded";
            }
        
            uint32_t nDeviceNum = 0;
        
        
            status = GXUpdateDeviceList(&nDeviceNum, 1000);
        
            if(nDeviceNum <=0)
                {
                    qDebug() << "No device is currently available";
                }
            else
                {
                GX_DEVICE_BASE_INFO *pBaseinfo = new GX_DEVICE_BASE_INFO[nDeviceNum];
                        size_t iSize = nDeviceNum * sizeof(GX_DEVICE_BASE_INFO);
                        GXGetAllDeviceBaseInfo(pBaseinfo, &iSize);
                        qDebug() << "Serial number of the first device : " << pBaseinfo[0].szSN;
                        qDebug() << "Model number of the first device : " << pBaseinfo[0].szDisplayName;
        
                        //Opens the device via a serial number
                        stOpenParam.accessMode = GX_ACCESS_EXCLUSIVE;
                        stOpenParam.openMode = GX_OPEN_INDEX;
                        //stOpenParam.pszContent = pBaseinfo[0].szSN;
                        stOpenParam.pszContent = "1";
        
                        status = GXOpenDevice(&stOpenParam, &g_hDevice);
                        //status = GXOpenDeviceByIndex(1, &g_hDevice);
        
                        if(status == GX_STATUS_SUCCESS)
                        {
                            qDebug() << "Open the device successfully";
        
                            size_t unSize = 128;
                             char  pszContent[128] = {'\0'};
                            status = GXGetString(g_hDevice, GX_STRING_DEVICE_FIRMWARE_VERSION, pszContent,&unSize);
                            qDebug("Firmware version:%s", pszContent);
        
                            size_t unSize2 = 128;
                            char  pszContent2[128] = {'\0'};
                            status = GXGetString(g_hDevice, GX_STRING_DEVICE_VERSION, pszContent2, &unSize2);
                            qDebug("FPGA version:%s", pszContent2);
        
                            int64_t width,height;
                            status = GXGetInt(g_hDevice,GX_INT_WIDTH,&width);
                            status = GXGetInt(g_hDevice,GX_INT_HEIGHT,&height);
                            qDebug() << "Width:" << width;
                            qDebug() << "height" << height;
        
                            status=GXIsImplemented(g_hDevice,GX_ENUM_PIXEL_COLOR_FILTER,&m_Is_implemented);
        
                            if(m_Is_implemented)
                            {
                                 qDebug() << "Colour Mode";
                                 status= GXGetEnum(g_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_pixel_color);
        
                            }else{
        
                                 qDebug() << "Mono Mode";
                            }
        
                            status = GXSetEnum(g_hDevice, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
                            status = GXSetEnum(g_hDevice, GX_ENUM_TRIGGER_MODE, GX_TRIGGER_MODE_OFF);
                            int64_t nPayLoadSize = 0;
                            status = GXGetInt(g_hDevice, GX_INT_PAYLOAD_SIZE, &nPayLoadSize);
                            g_frameData.pImgBuf = malloc(nPayLoadSize);
                            g_bGetImg = true;
                            status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_START);
        
        if(live_or_picture == 1){
                    //--------------------------------------------------LIVE VERSION-------------------------------------------------
        
            qDebug() << "Live Feed Start";
        
        
                //--------------------------------------------------END LIVE VERSION-------------------------------------------------
        }else{
                //--------------------------------------------------PICTURE VERSION------------------------------------------------
        
            qDebug() << "Take Picture";
            take_picture();
        
                  //--------------------------------------------------END PICTURE VERSION------------------------------------------------
        }
                        }else
                        {
                           //
                            qDebug() << "Failed to open device";
                        }
        
            }
        
        
        
        //----------------------------------------------------------------
        
        }
        
        MainWindow::~MainWindow()
        {
             killTimer(timerId);
        
            GX_STATUS status = GX_STATUS_SUCCESS;
        
            //--------------------------------------------------Add For Live Version-------------------------------------------------
            if(live_or_picture == 1){
                status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_STOP);
                free(g_frameData.pImgBuf);
            }
            //--------------------------------------------------END-------------------------------------------------
        
            status = GXCloseDevice(g_hDevice);
            if (status == GX_STATUS_SUCCESS)
            {
            qDebug() << "Closed the device successfully";
            }else{
            qDebug() << "Device not closed successfully";
            }
            status = GXCloseLib();
            qDebug() << "Closed the library successfully";
            qDebug() << "GOODBYE";
            delete ui;
        }
        
        void MainWindow::framegrab(cv::Mat src)
        {
            //QApplication::processEvents();
        
            QImage img = QImage(src.data,g_frameData.nWidth,g_frameData.nHeight, QImage::Format_RGB888).rgbSwapped().copy();
            //cv::imwrite("test.jpg", src);
        
            QPixmap pixel;
            pixel = QPixmap::fromImage(img);
        
            ui->label->setPixmap(pixel);
            ui->label->update();
        }
        
        void MainWindow::take_picture(){
            GX_STATUS status = GX_STATUS_SUCCESS;
        
            while(GXGetImage(g_hDevice, &g_frameData, 100) != GX_STATUS_SUCCESS)
             {
             QThread::sleep(1);
             }
            if (g_frameData.nStatus == GX_FRAME_STATUS_SUCCESS)
            {
             printf("Collection succeeded: Width:%d Height:%d\n", g_frameData.nWidth, g_frameData.nHeight);
        
             //Acquiring image is successful.
             cv::Mat src;
             m_rgb_image = new char[g_frameData.nWidth*g_frameData.nHeight*3];
             src.create(g_frameData.nHeight,g_frameData.nWidth,CV_8UC3);
             memcpy(src.data,g_frameData.pImgBuf,g_frameData.nWidth*g_frameData.nHeight);
             DxRaw8toRGB24(g_frameData.pImgBuf,m_rgb_image,g_frameData.nWidth, g_frameData.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERGB),false);
             memcpy(src.data,m_rgb_image,g_frameData.nWidth*g_frameData.nHeight*3);
        
             framegrab(src);
        
            }else
            {
                printf("Collection exception: exception code:%d\n", g_frameData.nStatus);
            }
            status = GXSendCommand(g_hDevice, GX_COMMAND_ACQUISITION_STOP);
            free(g_frameData.pImgBuf);
        }
        
        
        
        void MainWindow::timerEvent(QTimerEvent *event)
        {
        
            GX_STATUS status = GX_STATUS_SUCCESS;
        
            if(g_frameData.pImgBuf == NULL)
              {
               //continue;
              }else{
        
        
        status = GXGetImage(g_hDevice, &g_frameData, 100);
        
        if(status == GX_STATUS_SUCCESS)
        {
        
         if(g_frameData.nStatus == 0)
         {
        cv::Mat src;
             m_rgb_image = new char[g_frameData.nWidth*g_frameData.nHeight*3];
             src.create(g_frameData.nHeight,g_frameData.nWidth,CV_8UC3);
             memcpy(src.data,g_frameData.pImgBuf,g_frameData.nWidth*g_frameData.nHeight);
             DxRaw8toRGB24(g_frameData.pImgBuf,m_rgb_image,g_frameData.nWidth, g_frameData.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERGB),false);
             memcpy(src.data,m_rgb_image,g_frameData.nWidth*g_frameData.nHeight*3);
        
             framegrab(src);
         }
        }
        }
        }
        

        my code starts a timer to grab a frame from the camera and pass it to framegrab(), it works well until a certain point after say a minute my code just crashes and I get an error on my console output saying killed. now I’m not sure if it’s my camera or I have completely messed up the whole timer section, as it is my first attempt in using the timer class. if anyone could help that would be great

        Thank you

        JonBJ Online
        JonBJ Online
        JonB
        wrote on 9 Sept 2022, 07:38 last edited by
        #3

        @VJ01 said in QT Timer issue:

        while(GXGetImage(g_hDevice, &g_frameData, 100) != GX_STATUS_SUCCESS)
         {
         QThread::sleep(1);
         }
        

        In addition to @Christian-Ehrlicher for you to determine where some "crash" is happening, I'm not sure such a loop in MainWindow is a good idea?

        1 Reply Last reply
        1
        • V Offline
          V Offline
          VJ01
          wrote on 15 Sept 2022, 07:17 last edited by
          #4

          Hi

          Thank you everyone it was a issue with the camera and how i was capturing the images, i wasnt doing it properly so the camera kept disconnecting

          Thank you

          1 Reply Last reply
          0

          1/4

          9 Sept 2022, 07:22

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved