Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Creating an object of QVideoWidget closes my mediaplayer app .
Forum Updated to NodeBB v4.3 + New Features

Creating an object of QVideoWidget closes my mediaplayer app .

Scheduled Pinned Locked Moved Solved QML and Qt Quick
c++qvideowidgetmediaplayer
13 Posts 4 Posters 1.5k Views 2 Watching
  • 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.
  • U umutgurbuz
     QVideoWidget *vw = new QVideoWidget;
    

    This is how i create it. There is no error or warning. I am creating it in my header file and now i realized if i create it in my cpp file it does not even launches main.qml(still no error or warnings)

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @umutgurbuz said in Creating an object of QVideoWidget closes my mediaplayer app .:

    QVideoWidget *vw = new QVideoWidget;

    This creates a local variable which is destroyed when it goes out of scope...

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • U Offline
      U Offline
      umutgurbuz
      wrote on last edited by
      #5

      Now , I am creating it in the function that i am using it . I can see the list in my mediplayer.qml and pick the media that i want to play . Now , it closes when i click on the media name.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #6

        Hi,

        You should share more of your code. It's currently next to impossible to find out what is happening based only on your explanation.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • U Offline
          U Offline
          umutgurbuz
          wrote on last edited by umutgurbuz
          #7

          Yes you are right , I should have thought of that .
          This is the function in my mediaplayer.cpp
          :```

            void Mediaplayer::playvideo(int num)
               {   
            flag=1;
            vidplaylist->setCurrentIndex(num);
            if(player->state()==QMediaPlayer::PlayingState)
            {
                player->stop();
            }
            QVideoWidget vw ;
            player->setVideoOutput(&vw);
            vw.setGeometry(100,100,640,360);
            vw.show();
            vidplayer->play();
            }
          

          this is my main.qml

          import QtQuick 2.0
          import QtQuick.Controls 2.3
          import qt.player 1.0
          import QtQuick.Window 2.10
          import idpasswd.qt 1.0
          ApplicationWindow {
              id: root
              width: 640
              height: 480
              visible: true
              title:("Mediaplayer Login Page")
          
              Idpasswd {
                  id: id
              }
              Idpasswd {
                  id: passwd
              }
          
              Button {
                  x: 40
                  y: 318
                  width: 97
                  height: 36
                  text: qsTr("Guest")
                  onClicked: {
                  var component = Qt.createComponent("guest.qml");
                  newone = component.createObject(rect);
                  newone.show();
                  }
              }
          
              TextField {
          
                  anchors.verticalCenterOffset: -133
                  anchors.horizontalCenterOffset: -180
                  placeholderText: qsTr("User name")
                  anchors.centerIn: parent
                  onTextChanged: id.userName = text
              }
              TextField {
                  x: 40
                  y: 167
          
                  placeholderText: qsTr("Password")
                  anchors.horizontalCenter:parent
                  onTextChanged: id.password=text
              }
          
              Rectangle{
                  x: 40
                  y: 256
                  width:97
                  height:36
                  id: rect
                  Button{
                  text: qsTr("Enter")
                  anchors.fill: parent
                  property variant newone;  
                      onClicked: {                
                         if(id.checkLogin(id.userName,id.password))
                         {
                             var component = Qt.createComponent("mediaplayer.qml");
                             newone = component.createObject(rect);
                             newone.show();
                          }
                         else
                         {
                             console.log("Invalid id or password")
                         }             
                      }
                  }
          }
          
              Button{
                  x: 167
                  y: 256
                  text: qsTr("Change Password")
                  font.pointSize: 8
                  font.family: "Times New Roman"
                  focusPolicy: Qt.NoFocus
                  width:97
                  height:36
              }
          
          }
          
                      
          

          And this is where i am trying to call the function in mediaplayer.qml.

               ScrollView {
                        id:mp4scroll
                        x: 50
                        y:96
                        width: 250; height: 200
                        visible:false
                        ListView {
                            id:listmp4
                            model: player.createplaylistmp4()
                            delegate: ItemDelegate {
                                id:listdelegmp4
                                text: player.sendNamesvid(index)
                                font.pixelSize: 10
                                onClicked: player.playvideo(index), playimage.source="qrc:/pause.png"
                            }
                        }
                    }
          
          
          
          JonBJ 1 Reply Last reply
          0
          • U umutgurbuz

            Yes you are right , I should have thought of that .
            This is the function in my mediaplayer.cpp
            :```

              void Mediaplayer::playvideo(int num)
                 {   
              flag=1;
              vidplaylist->setCurrentIndex(num);
              if(player->state()==QMediaPlayer::PlayingState)
              {
                  player->stop();
              }
              QVideoWidget vw ;
              player->setVideoOutput(&vw);
              vw.setGeometry(100,100,640,360);
              vw.show();
              vidplayer->play();
              }
            

            this is my main.qml

            import QtQuick 2.0
            import QtQuick.Controls 2.3
            import qt.player 1.0
            import QtQuick.Window 2.10
            import idpasswd.qt 1.0
            ApplicationWindow {
                id: root
                width: 640
                height: 480
                visible: true
                title:("Mediaplayer Login Page")
            
                Idpasswd {
                    id: id
                }
                Idpasswd {
                    id: passwd
                }
            
                Button {
                    x: 40
                    y: 318
                    width: 97
                    height: 36
                    text: qsTr("Guest")
                    onClicked: {
                    var component = Qt.createComponent("guest.qml");
                    newone = component.createObject(rect);
                    newone.show();
                    }
                }
            
                TextField {
            
                    anchors.verticalCenterOffset: -133
                    anchors.horizontalCenterOffset: -180
                    placeholderText: qsTr("User name")
                    anchors.centerIn: parent
                    onTextChanged: id.userName = text
                }
                TextField {
                    x: 40
                    y: 167
            
                    placeholderText: qsTr("Password")
                    anchors.horizontalCenter:parent
                    onTextChanged: id.password=text
                }
            
                Rectangle{
                    x: 40
                    y: 256
                    width:97
                    height:36
                    id: rect
                    Button{
                    text: qsTr("Enter")
                    anchors.fill: parent
                    property variant newone;  
                        onClicked: {                
                           if(id.checkLogin(id.userName,id.password))
                           {
                               var component = Qt.createComponent("mediaplayer.qml");
                               newone = component.createObject(rect);
                               newone.show();
                            }
                           else
                           {
                               console.log("Invalid id or password")
                           }             
                        }
                    }
            }
            
                Button{
                    x: 167
                    y: 256
                    text: qsTr("Change Password")
                    font.pointSize: 8
                    font.family: "Times New Roman"
                    focusPolicy: Qt.NoFocus
                    width:97
                    height:36
                }
            
            }
            
                        
            

            And this is where i am trying to call the function in mediaplayer.qml.

                 ScrollView {
                          id:mp4scroll
                          x: 50
                          y:96
                          width: 250; height: 200
                          visible:false
                          ListView {
                              id:listmp4
                              model: player.createplaylistmp4()
                              delegate: ItemDelegate {
                                  id:listdelegmp4
                                  text: player.sendNamesvid(index)
                                  font.pixelSize: 10
                                  onClicked: player.playvideo(index), playimage.source="qrc:/pause.png"
                              }
                          }
                      }
            
            
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #8

            @umutgurbuz said in Creating an object of QVideoWidget closes my mediaplayer app .:

            QVideoWidget vw ;
            player->setVideoOutput(&vw);
            vw.setGeometry(100,100,640,360);
            vw.show();
            vidplayer->play();
            }
            

            I don't use any of this, so I hope I'm right:
            That QVideoWidget vw goes out of scope at the }. player->setVideoOutput(&vw); still references it. Kerplunk?

            U 1 Reply Last reply
            2
            • JonBJ JonB

              @umutgurbuz said in Creating an object of QVideoWidget closes my mediaplayer app .:

              QVideoWidget vw ;
              player->setVideoOutput(&vw);
              vw.setGeometry(100,100,640,360);
              vw.show();
              vidplayer->play();
              }
              

              I don't use any of this, so I hope I'm right:
              That QVideoWidget vw goes out of scope at the }. player->setVideoOutput(&vw); still references it. Kerplunk?

              U Offline
              U Offline
              umutgurbuz
              wrote on last edited by
              #9

              @jonb I do not understand what you mean. What should i try ?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #10

                What @JonB means is that the lifetime of your QVideoWidget ends at the end of the method because you are creating it on the stack. Thus you are passing a pointer to an object that gets destroyed.

                The question is: why are you re-creating the QVideoWidget each time ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                U 1 Reply Last reply
                2
                • U Offline
                  U Offline
                  umutgurbuz
                  wrote on last edited by
                  #11

                  I was actually creating it in my header file once but i wanted to see what changes when i create it in my method. The result is that when i create it in my header, it closes when i click the enter button that launches my mediaplayer.qml. When i create QVideoWidget in my method, it can launch my mediaplayer.qml but this time it closes when i pick the video on the window(scroll view list view part). What is the right thing to do ? Where and how should i create it ?

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What @JonB means is that the lifetime of your QVideoWidget ends at the end of the method because you are creating it on the stack. Thus you are passing a pointer to an object that gets destroyed.

                    The question is: why are you re-creating the QVideoWidget each time ?

                    U Offline
                    U Offline
                    umutgurbuz
                    wrote on last edited by umutgurbuz
                    #12

                    @sgaist The problem was the QGuiApplication in my main.cpp , i turned it to QApplication and it works fine.

                    And lastly what should i do to use my applicationwindow as my videowidget. I want my video to play on my app like normal mediaplayers, not to open another window for it. Thanks for your kind effort.

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      umutgurbuz
                      wrote on last edited by
                      #13
                      This post is deleted!
                      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