Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGridLayout showing different alignment for each run



  • Hi all,

    I'm new to Qt5 C++ development. I have created a grid of QMediaplayer (like CCTV look), but it's gets aligned differently for each time. Please find the code below.

    I'm getting an error while attaching my screenshots please get it from my drive.

    screenshot 1
    https://drive.google.com/file/d/1B0LduliRFirxnfl5b5NTnRer8PcE49Fq/view?usp=sharing

    screenshot 2
    https://drive.google.com/file/d/17SeZdqKqoYbJFZchyonHExZq-hkF2xNx/view?usp=sharing

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <qdebug.h>
    #include <QGridLayout>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        player1 = new QMediaPlayer();
        player2 = new QMediaPlayer();
        player3 = new QMediaPlayer();
        player4 = new QMediaPlayer();
        player5 = new QMediaPlayer();
        player6 = new QMediaPlayer();
        player7 = new QMediaPlayer();
        player8 = new QMediaPlayer();
        player9 = new QMediaPlayer();
        player10 = new QMediaPlayer();
        player11 = new QMediaPlayer();
        player12 = new QMediaPlayer();
    
        video1 = new QVideoWidget();
        video2 = new QVideoWidget();
        video3 = new QVideoWidget();
        video4 = new QVideoWidget();
        video5 = new QVideoWidget();
        video6 = new QVideoWidget();
        video7 = new QVideoWidget();
        video8 = new QVideoWidget();
        video9 = new QVideoWidget();
        video10 = new QVideoWidget();
        video11 = new QVideoWidget();
        video12 = new QVideoWidget();
    
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(video1,0,0,1,1,Qt::AlignTop);
        layout->addWidget(video2,0,1,1,1,Qt::AlignTop);
        layout->addWidget(video3,0,2,1,1,Qt::AlignTop);
        layout->addWidget(video4,0,3,1,1,Qt::AlignTop);
        layout->addWidget(video5,1,0,1,1,Qt::AlignTop);
        layout->addWidget(video6,1,1,1,1,Qt::AlignTop);
        layout->addWidget(video7,1,2,1,1,Qt::AlignTop);
        layout->addWidget(video8,1,3,1,1,Qt::AlignTop);
        layout->addWidget(video9,2,0,1,1,Qt::AlignTop);
        layout->addWidget(video10,2,1,1,1,Qt::AlignTop);
        layout->addWidget(video11,2,2,1,1,Qt::AlignTop);
        layout->addWidget(video12,2,3,1,1,Qt::AlignTop);
    
        QWidget *win = new QWidget();
        win->setLayout(layout);
        setCentralWidget(win);
    
        player1->setVideoOutput(video1);
        player2->setVideoOutput(video2);
        player3->setVideoOutput(video3);
        player4->setVideoOutput(video4);
        player5->setVideoOutput(video5);
        player6->setVideoOutput(video6);
        player7->setVideoOutput(video7);
        player8->setVideoOutput(video8);
        player9->setVideoOutput(video9);
        player10->setVideoOutput(video10);
        player11->setVideoOutput(video11);
        player12->setVideoOutput(video12);
    
    
        player1->setMedia(QUrl("rtsp://localhost:8554/test"));
        player2->setMedia(QUrl("rtsp://localhost:8554/test"));
        player3->setMedia(QUrl("rtsp://localhost:8554/test"));
        player4->setMedia(QUrl("rtsp://localhost:8554/test"));
        player5->setMedia(QUrl("rtsp://localhost:8554/test"));
        player6->setMedia(QUrl("rtsp://localhost:8554/test"));
        player7->setMedia(QUrl("rtsp://localhost:8554/test"));
        player8->setMedia(QUrl("rtsp://localhost:8554/test"));
        player9->setMedia(QUrl("rtsp://localhost:8554/test"));
        player10->setMedia(QUrl("rtsp://localhost:8554/test"));
        player11->setMedia(QUrl("rtsp://localhost:8554/test"));
        player12->setMedia(QUrl("rtsp://localhost:8554/test"));
    
    
        player1->play();
        player2->play();
        player3->play();
        player4->play();
        player5->play();
        player6->play();
        player7->play();
        player8->play();
        player9->play();
        player10->play();
        player11->play();
        player12->play();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    


  • @Venkateswaran
    I don't know anything about your problem, sorry. But if you are "new to [...] C++ development", just a heads-up: if your code used arrays for all your occurences of player..., & video..., your code would reduce about 90% and be easier to play with. You may already know this, but some here do not, so we give out tips like this....


  • Moderators

    hi @Venkateswaran

    first of, you don't need to go the way over a new QWIdget just to set a layout.

    remove

    QWidget *win = new QWidget();
        win->setLayout(layout);
        setCentralWidget(win);
    

    and change

    QGridLayout *layout = new QGridLayout;
    

    to

    QGridLayout *layout = new QGridLayout(this);
    

    this will assign your Gridlayout to you centralWidget which is by default a plain QWidget.

    From what I see, you shouldn't have a problem with the layout.
    Do you see this also with a file loaded from local drive, instead of an url ?



  • This post is deleted!


  • @JonB Thanks for your suggestion. 'II change it in my code.



  • @J.Hilk I have removed the Qwidget from my code, but it's shown below error.

    QLayout: Attempting to add QLayout "" to MosaicScreen "MosaicScreen", which already has a layout
    

    Please find my updated code as suggested by you and @JonB
    am I facing this issue because of video resolution.?

    #include "mosaicscreen.h"
    #include "ui_mosaicscreen.h"
    #include <iostream>
    
    MosaicScreen::MosaicScreen(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MosaicScreen)
    {
        ui->setupUi(this);
    
        //creating array of 12 QVideoWidget objects
        for(int i = 0; i < 12; i++){
            videos[i] = new QVideoWidget();
        }
    
        //creating array of 12 QMediaPlayer objects
        for(int i = 0; i < 12; i++){
            players[i] = new QMediaPlayer();
            players[i]->setVideoOutput(videos[i]);
            players[i]->setMedia(QUrl("rtsp://localhost:8554/test"));
        }
    
        QGridLayout *layout = new QGridLayout(this);
        layout->addWidget(videos[0],0,0,1,1,Qt::AlignTop);
        layout->addWidget(videos[1],0,1,1,1,Qt::AlignTop);
        layout->addWidget(videos[2],0,2,1,1,Qt::AlignTop);
        layout->addWidget(videos[3],0,3,1,1,Qt::AlignTop);
        layout->addWidget(videos[4],1,0,1,1,Qt::AlignTop);
        layout->addWidget(videos[5],1,1,1,1,Qt::AlignTop);
        layout->addWidget(videos[6],1,2,1,1,Qt::AlignTop);
        layout->addWidget(videos[7],1,3,1,1,Qt::AlignTop);
        layout->addWidget(videos[8],2,0,1,1,Qt::AlignTop);
        layout->addWidget(videos[9],2,1,1,1,Qt::AlignTop);
        layout->addWidget(videos[10],2,2,1,1,Qt::AlignTop);
        layout->addWidget(videos[11],2,3,1,1,Qt::AlignTop);
    
        //QWidget *win = new QWidget();
        //win->setLayout(layout);
        //setCentralWidget(win);
    
        for(int i = 0; i < 12; i++){
            players[i]->play();
        }
    }
    
    MosaicScreen::~MosaicScreen()
    {
        delete ui;
    }
    

  • Moderators

    @Venkateswaran
    ok, I didn't expect that you assigned a layout via the Designer, when the first thing you do in the constructor is to replace the centralWidget with your own.

    What did you do in(with) the designer ?



  • @J.Hilk I've created a new project like Projects->Apllications->Qt widgets applications -> QMainWindow and renamed the class to MosaicScreen. Now, I got to know that we can't add layout over QMainwindow.

    Now I've created a Qwidets instead of QMainWindow and your suggestion works without error. But, I'm still facing that alignment issue.

    Please find the below screenshot.

    0_1558359261795_mosaic-min.jpg

    Code:

    #include "mosaic.h"
    #include "ui_mosaic.h"
    
    Mosaic::Mosaic(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Mosaic)
    {
        ui->setupUi(this);
    
        //creating array of 12 QVideoWidget objects
        for(int i = 0; i < 12; i++){
            videos[i] = new QVideoWidget();
        }
    
        //creating array of 12 QMediaPlayer objects
        for(int i = 0; i < 12; i++){
            players[i] = new QMediaPlayer();
            players[i]->setVideoOutput(videos[i]);
            players[i]->setMedia(QUrl("rtsp://localhost:8554/test"));
        }
    
        QGridLayout *layout = new QGridLayout(this);
        layout->addWidget(videos[0],0,0,1,1,Qt::AlignTop);
        layout->addWidget(videos[1],0,1,1,1,Qt::AlignTop);
        layout->addWidget(videos[2],0,2,1,1,Qt::AlignTop);
        layout->addWidget(videos[3],0,3,1,1,Qt::AlignTop);
        layout->addWidget(videos[4],1,0,1,1,Qt::AlignTop);
        layout->addWidget(videos[5],1,1,1,1,Qt::AlignTop);
        layout->addWidget(videos[6],1,2,1,1,Qt::AlignTop);
        layout->addWidget(videos[7],1,3,1,1,Qt::AlignTop);
        layout->addWidget(videos[8],2,0,1,1,Qt::AlignTop);
        layout->addWidget(videos[9],2,1,1,1,Qt::AlignTop);
        layout->addWidget(videos[10],2,2,1,1,Qt::AlignTop);
        layout->addWidget(videos[11],2,3,1,1,Qt::AlignTop);
    
        //QWidget *win = new QWidget();
        //win->setLayout(layout);
        //setCentralWidget(win);
    
        for(int i = 0; i < 12; i++){
            players[i]->play();
        }
    }
    
    Mosaic::~Mosaic()
    {
        delete ui;
    }![0_1558359215439_mosaic.png](Uploading 100%) 
    


  • @J.Hilk Thanks for your time to help me out. I've found a thread in StackOverflow which states that there is a bug in QMediaPlayer.
    https://stackoverflow.com/questions/38374158/qvideowidget-video-is-cut-off

    Also, I'm using Ubuntu 18.04 and this Qt document says that there is an issue with the x11 system.
    https://doc.qt.io/qt-5/application-windows.html#window-geometry

    Note: All the QVideowigets are getting aligned correctly if I minimize my app and maximize it again.


Log in to reply