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 .

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.4k 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.
  • U Offline
    U Offline
    umutgurbuz
    wrote on 12 Sept 2019, 10:58 last edited by umutgurbuz 9 Dec 2019, 11:54
    #1

    Hi guys, I am trying the make a mediaplayer app that has the basic mediaplayer features.
    It is all fine when dealing with mp3 files . I am using QMediaPlayer and QMediaPlaylist in my cpp code . I have a login page main.qml and when user id and password is correct it launches the mediplayer.qml where i call my cpp functions. The problem is that now i am trying to play a video but if i create an object of QVideoWidget , it causes my mediaplayer.qml not to launch and main.qml to close when i clicked the enter button of my login page. I do not even have to use to QVideoWidget object ,just creating it is enough for this problem to occur. What might be causing this problem. Sorry if I kept it too long .Thanks in advance .

    J 1 Reply Last reply 12 Sept 2019, 11:00
    0
    • S SGaist
      12 Sept 2019, 22:12

      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 13 Sept 2019, 05:32 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 umutgurbuz
        12 Sept 2019, 10:58

        Hi guys, I am trying the make a mediaplayer app that has the basic mediaplayer features.
        It is all fine when dealing with mp3 files . I am using QMediaPlayer and QMediaPlaylist in my cpp code . I have a login page main.qml and when user id and password is correct it launches the mediplayer.qml where i call my cpp functions. The problem is that now i am trying to play a video but if i create an object of QVideoWidget , it causes my mediaplayer.qml not to launch and main.qml to close when i clicked the enter button of my login page. I do not even have to use to QVideoWidget object ,just creating it is enough for this problem to occur. What might be causing this problem. Sorry if I kept it too long .Thanks in advance .

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 12 Sept 2019, 11:00 last edited by
        #2

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

        just creating it is enough for this problem to occur

        Can you show how you create it?
        Are there any warnings/errors in the console when running the app?

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

        1 Reply Last reply
        0
        • U Offline
          U Offline
          umutgurbuz
          wrote on 12 Sept 2019, 11:08 last edited by
          #3
           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)

          J 1 Reply Last reply 12 Sept 2019, 11:09
          0
          • U umutgurbuz
            12 Sept 2019, 11:08
             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)

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 12 Sept 2019, 11:09 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 12 Sept 2019, 11:21 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 12 Sept 2019, 18:21 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 12 Sept 2019, 20:13 last edited by umutgurbuz 9 Dec 2019, 20:22
                  #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"
                                    }
                                }
                            }
                  
                  
                  
                  J 1 Reply Last reply 12 Sept 2019, 20:19
                  0
                  • U umutgurbuz
                    12 Sept 2019, 20:13

                    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"
                                      }
                                  }
                              }
                    
                    
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 12 Sept 2019, 20:19 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 12 Sept 2019, 20:26
                    2
                    • J JonB
                      12 Sept 2019, 20:19

                      @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 12 Sept 2019, 20:26 last edited by
                      #9

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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 12 Sept 2019, 22:12 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 13 Sept 2019, 05:32
                        2
                        • U Offline
                          U Offline
                          umutgurbuz
                          wrote on 13 Sept 2019, 05:03 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
                          • S SGaist
                            12 Sept 2019, 22:12

                            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 13 Sept 2019, 05:32 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 13 Sept 2019, 05:49 last edited by
                              #13
                              This post is deleted!
                              1 Reply Last reply
                              0

                              4/13

                              12 Sept 2019, 11:09

                              9 unread
                              • Login

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