Important: Please read the Qt Code of Conduct -

Program Crashed using QGridLayout

  • Hey there ! I have a problem using QGridLayout. Here is what I want to implement :

    I want to create a program recovering a video from an Ip Camera AND The geolocalization of this camera. So I already have two working program, the video recovering from the camera and the map widget thanks to Marble.

    Now I want to integrate both of them in the same project. In order to lock their position I planned to use QGridLayout. So I created this layout and I first wanted to put the video on it, but it crashed and I dont understand why, so here is my code :

    SOURCES += \
        main.cpp \
    QT += core gui
    QT += widgets
    QT += multimedia multimediawidgets
    HEADERS += \
    TEMPLATE = app
    CONFIG += qt


    #ifndef MAFENETRE_H
    #define MAFENETRE_H
    #include <QtWidgets>
    #include <QtMultimedia>
    #include <QtMultimediaWidgets>
    class MaFenetre : public QWidget
        explicit MaFenetre(QWidget *parent = 0);
          QMediaPlaylist *playlist;
          QMediaPlayer *player;
          QVideoWidget *videoWidget;
    public slots:
        void stateChanged(QMediaPlayer::State state);
        void metaDataChanged();
        void metaDataChanged(const QString &key, const QVariant &value);
        void error(QMediaPlayer::Error error);
    #endif // MAFENETRE_H


    #include "mafenetre.h"
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsVideoItem>
    #include <QDebug>
    MaFenetre::MaFenetre(QWidget *parent) :
        //QString fichier = QCoreApplication::applicationDirPath() + QString("/test.mpg");
        qDebug() << QUrl("rtsp://root:thales@");
        player = new QMediaPlayer;
        // GUI 1
        QVBoxLayout *layoutPrincipal = new QVBoxLayout;
        videoWidget = new QVideoWidget(this);
        connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(stateChanged(QMediaPlayer::State)));
        //connect(player, SIGNAL(metaDataChanged()), this, SLOT(metaDataChanged()));
        connect(player, SIGNAL(metaDataChanged(const QString &, const QVariant &)), this, SLOT(metaDataChanged(const QString &, const QVariant &)));
        connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(error(QMediaPlayer::Error)));
    void MaFenetre::stateChanged(QMediaPlayer::State state)
        qDebug() << Q_FUNC_INFO << state;
        /*if (state == QMediaPlayer::StoppedState)
    void MaFenetre::metaDataChanged()
        qDebug() << player->availableMetaData();
        if (player->isMetaDataAvailable())
            qDebug() << player->metaData(QMediaMetaData::Title).toString();
            qDebug() << player->metaData(QMediaMetaData::Size);
            qDebug() << player->metaData(QMediaMetaData::Duration);
            qDebug() << player->metaData(QMediaMetaData::Resolution);
    void MaFenetre::metaDataChanged(const QString &key, const QVariant &value)
        // "AudioBitRate", "AudioCodec", "PixelAspectRatio", "Resolution", "VideoCodec"
        if(key == "AudioCodec")
            qDebug() << key << value;
        if(key == "VideoCodec")
            qDebug() << key << value;
        if(key == "Resolution")
            qDebug() << key << value;
    void MaFenetre::error(QMediaPlayer::Error error)
        qDebug() << Q_FUNC_INFO << player->errorString() << error;


    #include <QApplication>
    #include <QWidget>
    #include "mafenetre.h"
    //#include <marble/MarbleWidget.h>
    #include <QGridLayout>
    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        QWidget fenprincip;
        MaFenetre *video;//(&fenprincip);
        // Création du Layout Grid :
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(video, 0,0);
        fenprincip.setLayout(layout);//On indique à la fenetre d'utiliser le layout grid
        return app.exec();

    And this is my error :
    Starting /home/thales/build-Fenetre-Desktop-Debug/Fenetre...
    QLayout: Cannot add a null widget to QGridLayout/
    /home/thales/build-Fenetre-Desktop-Debug/Fenetre exited with code 0

    If someone can help me it would be lovely :D Thank you :)

  • Qt Champions 2019

    @Chanchan said in Program Crashed using QGridLayout:

    layout->addWidget(video, 0,0);

    video pointer is uninitialized so what do you expect?

  • @Chanchan

        MaFenetre *video;//(&fenprincip);
        // Création du Layout Grid :
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(video, 0,0);

    video is not set to anything, and happens to be "null". You then try to add that to your QGridLayout. Hence QLayout: Cannot add a null widget to QGridLayout/.

  • Oh I see... Ehm so how should I modify this to create a widget from that video. I have to initialized it in my main I guess ?

    (thanks for your fast answer :) )

  • @Chanchan
    Purely at a guess, since I don't claim to know what is going on in your code. MaFenetre *video; simply creates a pointer to a MaFenetre widget, but does not create a MaFenetre instance nor point to one. You probably intend one of:

    MaFenetre video;
    layout->addWidget(&video, 0,0);


    MaFenetre *video = new MaFenetre;
    layout->addWidget(video, 0,0);

  • Ok ok I think I got it, i will try with it, thank you for your help, I will come back when I will succeed to make this work ;)
    thank you again :D

  • @JonB Your second solution worked perfectly so thank you very much <3 I put "solved" on the subject :)

Log in to reply