How to get dimensions of video files [Solved]

  • Hi everyone,

    I have a dialog box which has a button to call QFileDialog::getOpenFileName() to choose a video file.
    And the Phonon::VideoPlayer will use the path to play the video.
    Now I want to get the width and height of the video. I try to use VideoPlayer->sizeHint() to get the size while the file path is set. The code is like:

    @void VideoDialog::addVideo() // A slot called by clicked() signal of the button
    QMessageBox::information(this, "0", QString::number(m_videoPlayer->sizeHint().width()) + " x " + QString::number(m_videoPlayer->sizeHint().height()));
    QString videoFile = QFileDialog::getOpenFileName(.....); // The function arguments are removed
    QMessageBox::information(this, "1", QString::number(m_videoPlayer->sizeHint().width()) + " x " + QString::number(m_videoPlayer->sizeHint().height()));
    The 1st problem is that after I set the video path, the sizeHint is not updated. But if I press the button again, the sizeHint will be the right value. Is there a way to get the sizeHint immediately?

    And the 2nd problem is when I hide the VideoPlayer widget, I only get 1 x 1 every time on sizeHint.
    I see the doc says "The default implementation of sizeHint() returns an invalid size if there is no layout for this widget. "
    But I do need to hide the widget on initial. Any help please?
    Thanks in advance.

  • Absolute positioning has many disadvantages:
    • The user cannot resize the window.
    • Some text may be truncated if the user chooses an unusually large font or
    if the application is translated into another language.
    • The widgets might have inappropriate sizes for some styles.
    • The positions and sizes must be calculated manually. This is tedious and
    error-prone, and makes maintenance painful.

    So use i.e QStackedLayout and than add to it your hidden widgets!

  • Thanks for your suggestion. But the problem here is how to get the dimensions of a video.
    And QStackedLayout is not very suitable for my UI.
    Btw I'm using QGridLayout which is relative positioning I think.

  • I am not sure if I understood your problem, but if so, I would try to load your media as QMovie and then obtain data from QMovie::currentImage.width (...).height if it is a simple animation, or simply use QVideoFrame.width (...).height if it is a 'normal' video.
    Have you seen videowidgetsurface.cpp example?

  • Yes I just want to get the video width and height of a video file, like avi, wmv or mp4, etc. QMovie looks work for me. And I'll try both of them.

    Currently I found a tricky way to do this with phonon. I update the dimensions using sizeHint after state changed signal since very time I reload a new movie will change the state of the media object. This works very well, too.

    Thanks for your help.

  • Done with pleasure ;)
    Then, please mark the topic as [solved].

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.