[UNSOLVEABLE] Again problems with QWidget in QML this time HWND
-
Hi,
I'm trying to create custom QWidget to use in QML. Basically I want VLC to play in QML.
All I get is the sound from video, without picture.And in QML designer it says that there is no property for anchors, width or height for my VLC extension.
I need to use QWidget because of the HWND property. Maybe QML has one of it's own?
Here is my vlc_player.h
@#ifndef VLC_PLAYER_H
#define VLC_PLAYER_H#include <QtDeclarative/QDeclarativeExtensionPlugin>
#include <QtDeclarative/qdeclarative.h>
#include <QtGui/QGraphicsProxyWidget>
#include <QtGui/QWidget>
#include <QDebug>
#include <vlc/vlc.h>class vlc_player : public QGraphicsProxyWidget
{
Q_OBJECT
Q_PROPERTY(QString texts WRITE play)public:
vlc_player(QGraphicsItem* parent = 0);
libvlc_instance_t * inst;
libvlc_media_player_t * mp;
libvlc_media_t * m;void play(const QString &texts;);
private:
QWidget *vlcvideo;
};#endif // VLC_PLAYER_H
@vlc_player.cpp
@#include "vlc_player.h"
#include <QtGui/QWidget>
#include <QDebug>vlc_player::vlc_player(QGraphicsItem* parent)
: QGraphicsProxyWidget(parent)
{
vlcvideo = new QWidget();
vlcvideo->setObjectName(QString::fromUtf8("vlcvideo"));
vlcvideo->setGeometry(QRect(10, 90, 431, 291));vlcvideo->setAttribute(Qt::WA_NoSystemBackground); vlcvideo->setStyleSheet("background-color:black;"); vlcvideo->setMinimumSize(400,400); this->setWidget(vlcvideo); qDebug()<<vlcvideo->winId(); /* Load the VLC engine */ inst = libvlc_new (0, NULL); setFlag(QGraphicsItem::ItemHasNoContents, false);
}
void vlc_player::play(const QString &texts;) { // if(isPlaying==true) stop(); /* Create a new item */ m = libvlc_media_new_path (inst, texts.toStdString().c_str()); /* Create a media player playing environement */ mp = libvlc_media_player_new_from_media (m); /* No need to keep the media now */ libvlc_media_release (m); //Ievietojam vlc QWidget rāmi + ar pārbaudi kas tev par sistemu #if defined(Q_WS_X11) libvlc_media_player_set_xwindow(mp, vlcvideo->winId()); #elif defined(Q_WS_MAC) libvlc_media_player_set_nsobject(mp, vlcvideo->winId()); #elif defined(Q_WS_WIN) libvlc_media_player_set_hwnd(mp, vlcvideo->winId()); #else #error "unsupported platform" #endif /* play the media_player */ libvlc_media_player_play (mp); }
--
@and part of my qml
@ QWidget {
id: vlc1
texts: "nasa.wmv"
anchors.fill: parent
width:400
height:400}@
Best regards
Raivis -
The QML property is define with Q_PROPERTY(..) statement, so if you want to use anchors, width or height, you need to declare them and write the "Read", "Write" method at least. The common QML item is inherits from QDeclarativeItem, so they have those properties
And, QWidget is not a right name for custom QML element below:
@
QWidget {
id: vlc1
texts: "nasa.wmv"
anchors.fill: parent
width:400
height:400
}
@ -
With just a QWidget my code works OK. Now I've made my extension with QDeclarativeItem, it seems that QDeclarativeItem doesn't have setWidget command.
Basically the same effect, no video but I have a sound.
Here is my full code:
VlcPlayer.h
@#ifndef VLCPLAYER_H
#define VLCPLAYER_H#include <QtDeclarative/QDeclarativeExtensionPlugin>
#include <QtDeclarative/qdeclarative.h>
#include <QDeclarativeItem>
#include <QtGui/QGraphicsProxyWidget>
#include <QtGui/QWidget>
#include <QDebug>
#include <vlc/vlc.h>class VlcPlayer : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(QString media READ playing WRITE play)public:
VlcPlayer(QDeclarativeItem* parent = 0);
libvlc_instance_t * inst;
libvlc_media_player_t * mp;
libvlc_media_t * m;void play(const QString &media;); QString playing() const;
private:
QWidget *vlcvideo;
QString m_name;};
#endif // VLCPLAYER_H
@vlcplayer.cpp
@#include "vlcplayer.h"
#include <QtGui/QWidget>
#include <QDebug>VlcPlayer::VlcPlayer(QDeclarativeItem* parent)
: QDeclarativeItem(parent)
{
vlcvideo = new QWidget();
//setFlag(QDeclarativeItem::ItemHasNoContents, false);//setWidget(vlcvideo); /* Load the VLC engine */ inst = libvlc_new (0, NULL);
}
QString VlcPlayer::playing() const
{
return m_name;
}void VlcPlayer::play(const QString &texts;) { m_name = texts; // if(isPlaying==true) stop(); /* Create a new item */ m = libvlc_media_new_path (inst, texts.toStdString().c_str()); /* Create a media player playing environement */ mp = libvlc_media_player_new_from_media (m); /* No need to keep the media now */ libvlc_media_release (m); //Ievietojam vlc QWidget rāmi + ar pārbaudi kas tev par sistemu #if defined(Q_WS_X11) libvlc_media_player_set_xwindow(mp, vlcvideo->winId()); #elif defined(Q_WS_MAC) libvlc_media_player_set_nsobject(mp, vlcvideo->winId()); #elif defined(Q_WS_WIN) libvlc_media_player_set_hwnd(mp, vlcvideo->winId()); #else #error "unsupported platform" #endif /* play the media_player */ libvlc_media_player_play (mp); }
@
main.cpp
@#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "vlcplayer.h"
#include <qdeclarative.h>
#include <QDeclarativeView>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<VlcPlayer>("Vlc", 1, 0, "VlcPlayer");QmlApplicationViewer viewer;
viewer.setMainQmlFile(QLatin1String("qml/widget/main.qml"));
viewer.showExpanded();return app.exec();
}
@main.qml
@import QtQuick 1.0
import Vlc 1.0Rectangle {
width: 360
height: 360VlcPlayer { id: vlc1 clip: false anchors.fill: parent media: "nasa.wmv" }
}
@Now in Design mode I don't have any errors. As I mentioned no video either, just the sound!
Here is a screen shot what I get when I run my app:
!http://morf.lv/qt_vlc.jpg(No video just audio)! -
-
Sorry when I made that conclusion, I thought I could implement QGraphicsSCene with valid winId, actually you can't.
Only the main windows has valid WinId, so the VLC or maybe even phonon draws on top of the QML viewer.
I can give you code for how to implement Qgraphicscene without winid, you will be able to draw circles rectangles or anything you want. But you won't get a valid winId.
And if you use phonon, maybe check out Qt Multimedia. I once came across, that QML or Qt Quick has built in video/audio player. If I remember correctly the library was called Qt Multimedia.
-
AFAIK, there is a possibility to fore a widget to be a real windows window. Then it will have a valid winId(). I think it was something like:
@
x->setAttribute(Qt::WA_NativeWindow, true);
x->setAttribute(Qt::WA_DontCreateNativeAncestors, true);
@where x is the widget that should show the video.
as description, have a look at "native vs. alient widgets":http://doc.qt.nokia.com/latest/qwidget.html#native-widgets-vs-alien-widgets
-
Hello
We built QML plugin that used Phonon as video play engine and it works correct in our QML project. Here are worked QML Phonon component sources:
@ class QmlVideo : public QDeclarativeItem
{
Q_OBJECT
public:
explicit QmlVideo(QDeclarativeItem *parent = 0);
public slots:
void play();
private:
Phonon::VideoPlayer *m_pPhononPlayer;
};
QmlVideo::QmlVideo(QDeclarativeItem parent) :
QDeclarativeItem(parent)
{
m_pPhononPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory);
QGraphicsProxyWidget proxy = new QGraphicsProxyWidget(this);
proxy->setWidget(m_pPhononPlayer);
}void QmlVideo::play() { m_pPhononPlayer->play(QUrl("C:\\test.avi")); }
@
Now we decided to replace Phonon on another video play engine. This another video play engine draws video in external lib and requires only HWND window where to paint. So we replaced Phonon widget to usual QWidget in our QML video component and passed QWidget winId() to video lib but it doesn't work. We can hear sound but video is not visible (WM_PAINT doesn't come to this widget). Below are NOT worked QML component with using external video lib:
@ class QmlVideo : public QDeclarativeItem
{
Q_OBJECT
public:
explicit QmlVideo(QDeclarativeItem *parent = 0);
public slots:
void play();
private:
QWidget *m_pWidget;
IPlayerCore *m_pPlayerCore;
};
QmlVideo::QmlVideo(QDeclarativeItem *parent) :
QDeclarativeItem(parent)
{
m_pWidget = new QWidget();
m_pWidget->resize(300, 300);
m_pWidget->show();QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget(this); proxy->setWidget(m_pWidget); } void QmlVideo::play() { WId hwnd = m_pWidget->winId(); if (CreatePlayerCore(hwnd, &m_pPlayerCore) != IPlayerCore::STATUS_OK || m_pPlayerCore == 0) { qDebug() << "Failed to create player core!\n"; } if (m_pPlayerCore->Subscribe(new IPlayerCoreNotifyImpl(hwnd, m_pPlayerCore)) < 0) { qDebug() << "Failed to subscribe to core events!\n"; } if (m_pPlayerCore->Load(false, L"C:\\test.avi") != IPlayerCore::STATUS_OK) { qDebug() << Q_FUNC_INFO << "error"; } }
@
In the same time we can see video in this m_pWidget if comment line
//proxy->setWidget(m_pWidget);
but then window is not part of QML scene.So how is to get video painted from external video lib on QWidget that will be a part of scene as QML component?