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



  • 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?


  • Qt Champions 2019

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


  • Lifetime Qt Champion

    Hi,

    You should also provide the code of your custom widget.



  • @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.