Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Creating an object of QVideoWidget closes my mediaplayer app .

    QML and Qt Quick
    c++ qvideowidget mediaplayer
    4
    13
    630
    Loading More Posts
    • 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 last edited by umutgurbuz

      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 .

      jsulm 1 Reply Last reply Reply Quote 0
      • U
        umutgurbuz @SGaist last edited by umutgurbuz

        @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 Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @umutgurbuz last edited by

          @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 Reply Quote 0
          • U
            umutgurbuz last edited by

             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)

            jsulm 1 Reply Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @umutgurbuz last edited by

              @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 Reply Quote 0
              • U
                umutgurbuz last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • U
                    umutgurbuz last edited by 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"
                                      }
                                  }
                              }
                    
                    
                    
                    JonB 1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB @umutgurbuz last edited by

                      @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 Reply Quote 2
                      • U
                        umutgurbuz @JonB last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          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 Reply Quote 2
                          • U
                            umutgurbuz last edited by

                            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 Reply Quote 0
                            • U
                              umutgurbuz @SGaist last edited by umutgurbuz

                              @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 Reply Quote 0
                              • U
                                umutgurbuz last edited by

                                This post is deleted!
                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post