Important: Please read the Qt Code of Conduct -

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
           VideoWindow(QWidget* parent = nullptr): QWidget(parent), cap(nullptr) {};
           VideoWindow(char* adr, QWidget* parent = nullptr): QWidget(parent), cap(nullptr)
           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()));
               delete cap;
               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.cols, image.rows, image.step, QImage::Format_RGB888);
               plt->setBrush(QPalette::Background, *br);
           int W, H;
           int frameRate;
           QTimer * timer;
           QBrush *br;
           QPalette *plt;
           QImage image1;
           cv::VideoCapture *cap;
           cv::Mat image;
    #endif // VIDEOWINDOW_H


    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        QWidget* mainWidget = new QWidget( this );
        QGridLayout *mainLayout = new QGridLayout(mainWidget);
        A = new VideoWindow* [4];
        A[0] = new VideoWindow((char*)"rtsp://", mainWidget);
        A[1] = new VideoWindow((char*)"rtsp://", mainWidget);
        A[2] = new VideoWindow((char*)"rtsp://", mainWidget);
        A[3] = new VideoWindow((char*)"rtsp://", mainWidget);
        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?

  • Lifetime Qt Champion

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

  • Lifetime Qt Champion


    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.