将QML(QQuickView)添加到现有UI

5

目前,我正在开发一个需要集成到C++ Visual Studio项目中的Qt类。

Qt项目 - Qt Widgets应用程序。基于Qt Creator 3.2.1(开源),基于Qt 5.3.2构建。使用Visual Studio 2013 Professional,安装了Qt插件。

我尝试了来自互联网和其他Stack帖子的解决方案,但都没有成功。我不认为我的帖子是重复的,因为其他帖子没有解决我的问题。

我能够从代码中运行QML,但它会在另一个窗口中启动。在第一张图片中,显示了QML窗口(Qt画布)覆盖在我的程序UI上。

QML on a separate window

我需要将QML集成到我的程序UI中。如果有帮助,我可以使用QGraphicsView

ProgramUi

简单的QML示例。canvas.qml

import QtQuick 2.0
Rectangle {
    id: rectangle
    color: "red"
    width: 600
    height: 600 
}

1
你是如何加载QML文件的?你使用QQuickWidget吗? - peppe
是的。我正在使用QQuickWidget。 - Alexandru Vasiliu
1个回答

1
请看我项目中MVC实现的一部分。这里有一个类来在Qt5.6中可视化QML代码。希望它能有所帮助。
QmlViewBase::QmlViewBase( QWindow* parent, const std::string& qmlFilePath)
{
    this->m_pEngine = QQmlEnginePtr( new QQmlEngine() );
    this->m_pView = QQuickViewPtr ( new QQuickView( this->m_pEngine.get(), parent ));
    this->m_pView->setResizeMode( QQuickView::SizeRootObjectToView );
    this->m_pView->setSource( QUrl( qmlFilePath.c_str() ));
    this->m_pView->setVisible( false );
    this->m_pView->setMinimumSize(QSize(640, 480));
}

QmlViewBase::~QmlViewBase()
{
    try {
        this->m_pView.reset();
    }catch(...) {

    }
}

void QmlViewBase::show()
{
    this->m_pView->show();
}

void QmlViewBase::hide()
{
    this->m_pView->hide();
}

bool QmlViewBase::isVisible()
{
    return this->m_pView->isVisible();
}

bool QmlViewBase::close()
{
    return this->m_pView->close();
}

QObject * const QmlViewBase::getSlotsSignalsObject() const
{
    return reinterpret_cast<QObject* const >( this->m_pView->rootObject() );
}

为了管理控制器,我有一个Gui director类,具体实现如下:
#ifndef de91_a97_4a2d_b906_01070cbfdd47
#define de91_a97_4a2d_b906_01070cbfdd47

#include "gui_director.h"
#include "utility/event_handler/event_handler.h"
#include "utility/exceptions.h"
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <map>
#include <QApplication>

template<typename ControllerId>
class GuiDirectorImpl : public GuiDirector<ControllerId>,
                        public EventHandler<
                              Event<ModelToUIParameters, ServerToClientEventType> >
{
public:
   typedef boost::shared_ptr<GuiDirectorImpl<ControllerId> > pointer;
   typedef boost::weak_ptr<GuiDirectorImpl<ControllerId> > weak_pointer;

public:
   virtual ~GuiDirectorImpl()
   {
   }
   ;
   GuiDirectorImpl(QApplication *app)
   {
      m_app = app;
      m_currentActiveController.reset();
   }

   virtual void addController(ControllerId controllerId,
                              Controller::pointer controller)
   {
      if (isControllerExist( controllerId )) {
         BOOST_THROW_EXCEPTION( argument_error()
                                << error_description( "controller with such id already added" ) );
      }

      m_idToController[controllerId] = controller;
   }

   virtual void setActive(ControllerId controllerId)
   {
      if (!isControllerExist( controllerId )) {
         BOOST_THROW_EXCEPTION( argument_error()
                                << error_description( "controller with such id doesn't exeist" ) );
      }

      Controller::pointer oldController = m_currentActiveController;

      m_currentActiveController = m_idToController[controllerId];
      if(NULL != oldController)
      {
          oldController->prepareViewToHide();
      }
      m_currentActiveController->prepareViewToShow();

      m_currentActiveController->startShowView();

      if (NULL != oldController) {
         oldController->stopShowView();
      }
   }

   virtual void handleEvent(Event<ModelToUIParameters, ServerToClientEventType>::pointer event_)
   {
      if (NULL == m_currentActiveController) {
         BOOST_THROW_EXCEPTION( error()
                                << error_description( "no active controller, cant handle event" ) );
      }

      m_currentActiveController->handleEvent( event_ );
   }

   virtual void quit()
   {
      m_app->quit();
   }

private:
   bool isControllerExist(ControllerId controllerId)
   {
      typename std::map<ControllerId, Controller::pointer>::const_iterator iter = m_idToController.find( controllerId );

      if (m_idToController.end() == iter) {
         return false;
      }

      return true;
   }

private:
   QApplication *m_app;
   Controller::pointer m_currentActiveController;
   typename std::map<ControllerId, Controller::pointer> m_idToController;
};

#endif /* de91_a97_4a2d_b906_01070cbfdd47 */

我喜欢你的方法,我认为Qt所谓的“MVC”更多地是一个用于控件的MVC框架,而不是用于构建大型应用程序的通用框架。您如何处理视图之间的切换? - M.G.
好的,我有一个所谓的GUI导演类。我会把它放到我的回答中。 - Dmitry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接