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. Create and show the object of my QWidget (heir) in the main window (heir of QMainWindow)

Create and show the object of my QWidget (heir) in the main window (heir of QMainWindow)

Scheduled Pinned Locked Moved General and Desktop
qwidgetqmainwindowvideocaptureopencvqt designer
4 Posts 3 Posters 1.2k 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.
  • A Offline
    A Offline
    Algorithm0
    wrote on 26 Nov 2019, 11:49 last edited by Algorithm0
    #1

    I am new in Qt, so I ask you for help. I also warn that my English can be bad. My problem:
    I created an heir from QWidget. Separately, the objects of this class work the way I want. If I instantiate this class and call show().
    I want to divide my main window into four parts, so that in each of the parts my widget (video stream) works. But at the same time I get an empty window. Here are my files:

    #ifndef VIDEOWINDOW_H
    #define VIDEOWINDOW_H
    
    #include <QWidget>
    #include <QTimer>
    #include <QLabel>
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    class VideoWindow: public QWidget
    {
       Q_OBJECT
       public:
    
           VideoWindow(QWidget* parent = nullptr): QWidget(parent), cap(nullptr) {};
           VideoWindow(char* adr, QWidget* parent = nullptr): QWidget(parent), cap(nullptr)
           {
               loadVideoCapture(adr);
           }
    
           void loadVideoCapture(char* adr)
           {
               cap = new cv::VideoCapture(adr);
               frameRate = (int) cap->get(cv::CAP_PROP_FPS);
               br = new QBrush();
               plt = new QPalette(this->palette());
    
               timer = new QTimer(this);
               connect(timer, SIGNAL(timeout()), this, SLOT(updatePicture()));
               timer->start(int(frameRate/10000));
           }
    
           ~VideoWindow()
           {
               cap->release();
               delete cap;
               image.release();
               delete br;
               delete plt;
           }
    
    
       public slots:
           void updatePicture()
           {
               *cap >> image;
               W = this->size().width();
               H = this->size().height();
               if (H < 10) H = 10;
               if (W < 10) W = 10;
               cv::resize(image, image, cv::Size(W, H), 0, 0, cv::INTER_LINEAR);
               cvtColor(image, image, cv::COLOR_BGR2RGB);
               image1 = QImage((uchar*) image.data, image.cols, image.rows, image.step, QImage::Format_RGB888);
    
               br->setTextureImage(image1);
               plt->setBrush(QPalette::Background, *br);
               this->setPalette(*plt);
           }
    
       private:
           int W, H;
           int frameRate;
           QTimer * timer;
           QBrush *br;
           QPalette *plt;
           QImage image1;
           cv::VideoCapture *cap;
           cv::Mat image;
    };
    
    
    #endif // VIDEOWINDOW_H
    

    mainwindow.cpp:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QWidget* mainWidget = new QWidget( this );
    
        QGridLayout *mainLayout = new QGridLayout(mainWidget);
    
        A = new VideoWindow* [4];
        A[0] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=1&stream=0:network-caching=0.sdp?", mainWidget);
        A[1] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=2&stream=0:network-caching=0.sdp?", mainWidget);
        A[2] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=3&stream=0:network-caching=0.sdp?", mainWidget);
        A[3] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=4&stream=0:network-caching=0.sdp?", mainWidget);
    
        mainLayout->addWidget(A[0],0,0,1,1);
        mainLayout->addWidget(A[1],0,1,1,1);
        mainLayout->addWidget(A[2],1,0,1,1);
        mainLayout->addWidget(A[3],1,1,1,1);
    
        mainWidget->setLayout(mainLayout);
        setCentralWidget( mainWidget );
    }
    

    Снимок экрана от 2019-11-26 14-48-33.png
    What am I doing wrong? Thanks in advance!
    The VideoWindow.h file was created as a .h file. Perhaps the problem is that I did not use Qtr Designer?

    J 1 Reply Last reply 26 Nov 2019, 13:29
    0
    • A Algorithm0
      26 Nov 2019, 11:49

      I am new in Qt, so I ask you for help. I also warn that my English can be bad. My problem:
      I created an heir from QWidget. Separately, the objects of this class work the way I want. If I instantiate this class and call show().
      I want to divide my main window into four parts, so that in each of the parts my widget (video stream) works. But at the same time I get an empty window. Here are my files:

      #ifndef VIDEOWINDOW_H
      #define VIDEOWINDOW_H
      
      #include <QWidget>
      #include <QTimer>
      #include <QLabel>
      #include <opencv2/core/core.hpp>
      #include <opencv2/imgproc/imgproc.hpp>
      #include <opencv2/highgui/highgui.hpp>
      
      class VideoWindow: public QWidget
      {
         Q_OBJECT
         public:
      
             VideoWindow(QWidget* parent = nullptr): QWidget(parent), cap(nullptr) {};
             VideoWindow(char* adr, QWidget* parent = nullptr): QWidget(parent), cap(nullptr)
             {
                 loadVideoCapture(adr);
             }
      
             void loadVideoCapture(char* adr)
             {
                 cap = new cv::VideoCapture(adr);
                 frameRate = (int) cap->get(cv::CAP_PROP_FPS);
                 br = new QBrush();
                 plt = new QPalette(this->palette());
      
                 timer = new QTimer(this);
                 connect(timer, SIGNAL(timeout()), this, SLOT(updatePicture()));
                 timer->start(int(frameRate/10000));
             }
      
             ~VideoWindow()
             {
                 cap->release();
                 delete cap;
                 image.release();
                 delete br;
                 delete plt;
             }
      
      
         public slots:
             void updatePicture()
             {
                 *cap >> image;
                 W = this->size().width();
                 H = this->size().height();
                 if (H < 10) H = 10;
                 if (W < 10) W = 10;
                 cv::resize(image, image, cv::Size(W, H), 0, 0, cv::INTER_LINEAR);
                 cvtColor(image, image, cv::COLOR_BGR2RGB);
                 image1 = QImage((uchar*) image.data, image.cols, image.rows, image.step, QImage::Format_RGB888);
      
                 br->setTextureImage(image1);
                 plt->setBrush(QPalette::Background, *br);
                 this->setPalette(*plt);
             }
      
         private:
             int W, H;
             int frameRate;
             QTimer * timer;
             QBrush *br;
             QPalette *plt;
             QImage image1;
             cv::VideoCapture *cap;
             cv::Mat image;
      };
      
      
      #endif // VIDEOWINDOW_H
      

      mainwindow.cpp:

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          QWidget* mainWidget = new QWidget( this );
      
          QGridLayout *mainLayout = new QGridLayout(mainWidget);
      
          A = new VideoWindow* [4];
          A[0] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=1&stream=0:network-caching=0.sdp?", mainWidget);
          A[1] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=2&stream=0:network-caching=0.sdp?", mainWidget);
          A[2] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=3&stream=0:network-caching=0.sdp?", mainWidget);
          A[3] = new VideoWindow((char*)"rtsp://192.168.168.230:554/user=admin&password=&channel=4&stream=0:network-caching=0.sdp?", mainWidget);
      
          mainLayout->addWidget(A[0],0,0,1,1);
          mainLayout->addWidget(A[1],0,1,1,1);
          mainLayout->addWidget(A[2],1,0,1,1);
          mainLayout->addWidget(A[3],1,1,1,1);
      
          mainWidget->setLayout(mainLayout);
          setCentralWidget( mainWidget );
      }
      

      Снимок экрана от 2019-11-26 14-48-33.png
      What am I doing wrong? Thanks in advance!
      The VideoWindow.h file was created as a .h file. Perhaps the problem is that I did not use Qtr Designer?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Nov 2019, 13:29 last edited by jsulm
      #2

      @Algorithm0 Hi and welcome!
      You should call show() on your A[0..3] widgets.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 27 Nov 2019, 05:30
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 26 Nov 2019, 20:12 last edited by
        #3

        Hi,

        You should also provide the code of your custom widget.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J jsulm
          26 Nov 2019, 13:29

          @Algorithm0 Hi and welcome!
          You should call show() on your A[0..3] widgets.

          A Offline
          A Offline
          Algorithm0
          wrote on 27 Nov 2019, 05:30 last edited by Algorithm0
          #4

          @jsulm Hello! Sorry, but that doesn't work. I tried to insert a loop in different places, but the result remains the same. I also tried to display the main widget.
          Снимок экрана от 2019-11-27 08-27-43.png

          @SGaist Hello. So the VideoWindow.h file (VideoWindow class) is my custom widget. This is the first piece of code that I presented.

          1 Reply Last reply
          0

          1/4

          26 Nov 2019, 11:49

          • 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