Qt 5.0: Combining QML code in a QMainWindow-derived main window



  • Hi,

    what is the preferred method of having a QMainWindow application window, but the UI in QML? I would really like to use the new MediaPlayer and the VideoSurface elements in QtQuick 2.0, I'm assuming I can't host such a qml file in a QDeclarativeView-based environment?

    Any pointers whatsoever would be highly appreciated.

    Regards,
    Daniel



  • I ended up finding a class (somewhere) called QWindowContainer. Looks like this:

    QWindowContainer.h
    @class QWindowContainerPrivate;

    class Q_WIDGETS_EXPORT QWindowContainer : public QWidget
    {
    Q_OBJECT
    Q_DECLARE_PRIVATE(QWindowContainer)

    public:
    explicit QWindowContainer(QWidget *parent = 0, Qt::WindowFlags f = 0);
    ~QWindowContainer();

    void setWindow(QWindow *window);
    QWindow *window() const;
    

    protected:
    bool event(QEvent *ev);
    };@

    QWindowContainer.cpp
    @class QWindowContainerPrivate : public QWidgetPrivate
    {
    public:
    Q_DECLARE_PUBLIC(QWindowContainer)

    QWindowContainerPrivate() : window(0) { }
    ~QWindowContainerPrivate() { }
    
    QWindow *window;
    

    };

    QWindowContainer::QWindowContainer(QWidget *parent, Qt::WindowFlags flags)
    : QWidget(*new QWindowContainerPrivate, parent, flags)
    {
    }

    QWindowContainer::~QWindowContainer()
    {
    }

    void QWindowContainer::setWindow(QWindow *window)
    {
    Q_D(QWindowContainer);

    Q_ASSERT(window && !d->window);
    QWidget *tlw = QWidget::window();
    tlw->winId(); // make sure the tlw has a platform window
    window->setParent(tlw->windowHandle());
    window->setGeometry(geometry());
    d->window = window;
    

    }

    QWindow *QWindowContainer::window() const
    {
    Q_D(const QWindowContainer);
    return d->window;
    }

    bool QWindowContainer::event(QEvent *e)
    {
    Q_D(QWindowContainer);
    if (!d->window)
    return QWidget::event(e);

    QEvent::Type type = e->type();
    switch (type) {
    case QEvent::Show:
        d->window->show();
        break;
    case QEvent::Hide:
        d->window->hide();
        break;
    case QEvent::Move: {
        QMoveEvent *me = static_cast<QMoveEvent *>(e);
        d->window->setPosition(me->pos());
    }
        break;
    case QEvent::Resize: {
        QResizeEvent *re = static_cast<QResizeEvent *>(e);
        d->window->resize(re->size());
        break;
    }
    default:
        break;
    }
    
    return QWidget::event(e);
    

    }@

    Then you have some code that looks like
    @QMainWindow window;
    QQuickView* view = new QQuickView;
    QWindowContainer* container = new QWindowContainer;
    container->setWindow(view);
    window.setCentralWidget(container);@

    It has issues though. At the moment I can not get QAction global shortcuts to work if the focus is on QQuickView.



  • It is probably this one: https://codereview.qt-project.org/#change,41539
    Please let us know if you find other issues with it. The plan is to include it or something similar with 5.1



  • That's the one. Discovered today that shortcuts for QAction don't work when the focus is on the QQuickItem. I have raised a question about it in another post.



  • Hi There,

    I am trying to integrate this class in my program on window machine and getting link errors. Link errors are given below. Also I am unable to include qwidget_p.h file.

    Any clue??

    mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QWindowContainer::setWindow(class QWindow *)" (_imp?setWindow@QWindowContainer@@QAEXPAVQWindow@@@Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
    mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QWindowContainer::QWindowContainer(class QWidget *,class QFlags<enum Qt::WindowType>)" (_imp??0QWindowContainer@@QAE@PAVQWidget@@V?$QFlags@W4WindowType@Qt@@@@@Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
    mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QWindowContainer::metaObject(void)const " (?metaObject@QWindowContainer@@UBEPBUQMetaObject@@XZ)
    mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall QWindowContainer::qt_metacast(char const *)" (?qt_metacast@QWindowContainer@@UAEPAXPBD@Z)
    mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall QWindowContainer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QWindowContainer@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    mainwindow.obj : error LNK2001: unresolved external symbol "protected: virtual bool __thiscall QWindowContainer::event(class QEvent *)" (?event@QWindowContainer@@MAE_NPAVQEvent@@@Z)
    mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QWindowContainer::~QWindowContainer(void)" (_imp??1QWindowContainer@@UAE@XZ) referenced in function "public: virtual void * __thiscall QWindowContainer::`scalar deleting destructor'(unsigned int)" (??_GQWindowContainer@@UAEPAXI@Z)



  • In cmake I have this
    @INCLUDE_DIRECTORIES(
    ${Qt5Widgets_PRIVATE_INCLUDE_DIRS}
    )

    QT5_USE_MODULES(Gui Widgets OpenGL Xml Quick)
    @



  • I am building this using QTCreator (2.6.1) and not sure what is equivalent of $Qt5Widgets_PRIVATE_INCLUDE_DIRS

    I search but could not found this as a system variable. I guess I have to find out proper file in mkspecs?
    any help?

    I am also afraid : https://bugreports.qt-project.org/browse/QTBUG-26869



  • I think it is just
    @
    QT += widget widget-private
    @



  • No Same errors.


Log in to reply
 

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