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 :
    Fen.pro

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

    Fenetre.h

    #ifndef MAFENETRE_H
    #define MAFENETRE_H
    
    #include <QtWidgets>
    
    #include <QtMultimedia>
    #include <QtMultimediaWidgets>
    
    class MaFenetre : public QWidget
    {
        Q_OBJECT
    public:
        explicit MaFenetre(QWidget *parent = 0);
        ~MaFenetre();
    
    private:
          QMediaPlaylist *playlist;
          QMediaPlayer *player;
          QVideoWidget *videoWidget;
    
    signals:
    
    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
    
    
    
    

    Fenetre.cpp

    #include "mafenetre.h"
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsVideoItem>
    #include <QDebug>
    
    MaFenetre::MaFenetre(QWidget *parent) :
        QWidget(parent)
    {
        //QString fichier = QCoreApplication::applicationDirPath() + QString("/test.mpg");
        qDebug() << QUrl("rtsp://root:thales@169.254.162.23/axis-media/media.amp");
    
    
        player = new QMediaPlayer;
        player->setMedia(QUrl("rtsp://root:xxxxxxx@169.254.xxxx.xx/axis-media/media.amp"));
    
        // GUI 1
        QVBoxLayout *layoutPrincipal = new QVBoxLayout;
        videoWidget = new QVideoWidget(this);
        layoutPrincipal->addWidget(videoWidget);
        player->setVideoOutput(videoWidget);
        setLayout(layoutPrincipal);
    
    
        setWindowTitle(QString::fromUtf8("player"));
        //setFixedWidth(960);
        //setFixedHeight(540);
    
    
        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)));
    
        player->play();
    }
    
    MaFenetre::~MaFenetre()
    {
    
    }
    
    void MaFenetre::stateChanged(QMediaPlayer::State state)
    {
        qDebug() << Q_FUNC_INFO << state;
        /*if (state == QMediaPlayer::StoppedState)
        {
            videoWidget->update();
        }*/
    }
    
    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;
    }
    

    main.cpp

    #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;
        //fenprincip.setFixedSize(1600,900);
        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
    
        fenprincip.show();
    
    
        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 2018

    @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);
    

    or

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.