Creating an object of QVideoWidget closes my mediaplayer app .



  • 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 .



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


  • Qt Champions 2018

    @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?



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


  • Qt Champions 2018

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



  • 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.


  • Lifetime Qt Champion

    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.



  • 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"
                      }
                  }
              }
    
    
    


  • @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?



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


  • Lifetime Qt Champion

    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 ?



  • 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 ?



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



  • This post is deleted!