gpt4 book ai didi

qt - 将 QML(QQuickView) 添加到现有 UI

转载 作者:行者123 更新时间:2023-12-04 12:55:13 27 4
gpt4 key购买 nike

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

Qt project - Qt Widgets Application. Build on Qt Creator 3.2.1 (opensource), based on Qt 5.3.2. Using Visual Studio 2013 Professional, the Qt Addin installed.



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

我能够从代码运行 QML,但它在不同的窗口上启动。在第一张图片中,QML 窗口(Qt Canvas )显示在我的程序 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
}

最佳答案

请看一下我项目中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() );
}

为了管理 Controller ,我有具有以下实现的 Gui 主管类:
#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 - 将 QML(QQuickView) 添加到现有 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533277/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com