Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program Crashed using QGridLayout
QtWS25 Last Chance

Program Crashed using QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 922 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chanchan
    wrote on last edited by
    #1

    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 :)

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Chanchan said in Program Crashed using QGridLayout:

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

      video pointer is uninitialized so what do you expect?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • C Chanchan

        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 :)

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

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

        1 Reply Last reply
        3
        • C Offline
          C Offline
          Chanchan
          wrote on last edited by
          #4

          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 :) )

          JonBJ 1 Reply Last reply
          0
          • C Chanchan

            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 :) )

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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);
            
            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chanchan
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chanchan
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved