From 9bb6d55687ffe45dffa1ad9a3cceb7957dd2bfbb Mon Sep 17 00:00:00 2001 From: Ivan Lebedev Date: Tue, 28 Apr 2026 16:33:03 +0200 Subject: [PATCH] refactor: Uses single QML engine for all QML code (#323) * refactor: Uses single QML engine for all QML code * fix: Adds missing `QPointer` include * Parents QML engine instance to plugin instance. --------- Co-authored-by: Ivan Lebedev Co-authored-by: Ivan Lebedev --- ChatView/ChatView.cpp | 18 ++++++++++++++---- ChatView/ChatView.hpp | 2 +- ChatView/ChatWidget.cpp | 13 ++++++++++--- ChatView/ChatWidget.hpp | 2 +- chat/ChatOutputPane.cpp | 4 ++-- chat/ChatOutputPane.h | 2 +- chat/NavigationPanel.cpp | 8 +++----- chat/NavigationPanel.hpp | 8 +++++++- qodeassist.cpp | 9 ++++++--- 9 files changed, 45 insertions(+), 21 deletions(-) diff --git a/ChatView/ChatView.cpp b/ChatView/ChatView.cpp index 01c1a58..827097b 100644 --- a/ChatView/ChatView.cpp +++ b/ChatView/ChatView.cpp @@ -3,6 +3,7 @@ #include "ChatView.hpp" +#include #include #include #include @@ -19,12 +20,21 @@ constexpr Qt::WindowFlags baseFlags = Qt::Window | Qt::WindowTitleHint | Qt::Win namespace QodeAssist::Chat { -ChatView::ChatView() - : m_isPin(false) +ChatView::ChatView(QQmlEngine* engine) + : QQuickView{engine, nullptr} + , m_isPin(false) { setTitle("QodeAssist Chat"); - engine()->rootContext()->setContextProperty("_chatview", this); - setSource(QUrl("qrc:/qt/qml/ChatView/qml/RootItem.qml")); + /// @note setup quick view content + { + auto context = new QQmlContext{engine, this}; + context->setContextProperty("_chatview", this); + + auto component = new QQmlComponent{engine, QUrl{"qrc:/qt/qml/ChatView/qml/RootItem.qml"}, this}; + auto rootItem = component->create(context); + + setContent(component->url(), component, rootItem); + } setResizeMode(QQuickView::SizeRootObjectToView); setMinimumSize({400, 300}); setFlags(baseFlags); diff --git a/ChatView/ChatView.hpp b/ChatView/ChatView.hpp index 74ece71..f1da3ea 100644 --- a/ChatView/ChatView.hpp +++ b/ChatView/ChatView.hpp @@ -13,7 +13,7 @@ class ChatView : public QQuickView Q_OBJECT Q_PROPERTY(bool isPin READ isPin WRITE setIsPin NOTIFY isPinChanged FINAL) public: - ChatView(); + ChatView(QQmlEngine* engine); bool isPin() const; void setIsPin(bool newIsPin); diff --git a/ChatView/ChatWidget.cpp b/ChatView/ChatWidget.cpp index 804eb4d..418e36b 100644 --- a/ChatView/ChatWidget.cpp +++ b/ChatView/ChatWidget.cpp @@ -8,10 +8,17 @@ namespace QodeAssist::Chat { -ChatWidget::ChatWidget(QWidget *parent) - : QQuickWidget(parent) +ChatWidget::ChatWidget(QQmlEngine* engine, QWidget *parent) + : QQuickWidget{engine, parent} { - setSource(QUrl("qrc:/qt/qml/ChatView/qml/RootItem.qml")); + /// @note setup quick view content + { + auto context = new QQmlContext{engine, this}; + auto component = new QQmlComponent{engine, QUrl{"qrc:/qt/qml/ChatView/qml/RootItem.qml"}, this}; + auto rootItem = component->create(context); + + setContent(component->url(), component, rootItem); + } setResizeMode(QQuickWidget::SizeRootObjectToView); } diff --git a/ChatView/ChatWidget.hpp b/ChatView/ChatWidget.hpp index 341bfc1..62c20e2 100644 --- a/ChatView/ChatWidget.hpp +++ b/ChatView/ChatWidget.hpp @@ -12,7 +12,7 @@ class ChatWidget : public QQuickWidget Q_OBJECT public: - explicit ChatWidget(QWidget *parent = nullptr); + explicit ChatWidget(QQmlEngine* engine, QWidget *parent = nullptr); ~ChatWidget() = default; Q_INVOKABLE void clear(); diff --git a/chat/ChatOutputPane.cpp b/chat/ChatOutputPane.cpp index 11b6d67..ecb92e5 100644 --- a/chat/ChatOutputPane.cpp +++ b/chat/ChatOutputPane.cpp @@ -7,9 +7,9 @@ namespace QodeAssist::Chat { -ChatOutputPane::ChatOutputPane(QObject *parent) +ChatOutputPane::ChatOutputPane(QQmlEngine* engine, QObject *parent) : Core::IOutputPane(parent) - , m_chatWidget(new ChatWidget) + , m_chatWidget{new ChatWidget{engine}} { setId("QodeAssistChat"); setDisplayName(Tr::tr("QodeAssist Chat")); diff --git a/chat/ChatOutputPane.h b/chat/ChatOutputPane.h index ca60d9e..d39f1be 100644 --- a/chat/ChatOutputPane.h +++ b/chat/ChatOutputPane.h @@ -13,7 +13,7 @@ class ChatOutputPane : public Core::IOutputPane Q_OBJECT public: - explicit ChatOutputPane(QObject *parent = nullptr); + explicit ChatOutputPane(QQmlEngine* engine, QObject *parent = nullptr); ~ChatOutputPane() override; QWidget *outputWidget(QWidget *parent) override; diff --git a/chat/NavigationPanel.cpp b/chat/NavigationPanel.cpp index dd95afb..c6af855 100644 --- a/chat/NavigationPanel.cpp +++ b/chat/NavigationPanel.cpp @@ -7,7 +7,8 @@ namespace QodeAssist::Chat { -NavigationPanel::NavigationPanel() +NavigationPanel::NavigationPanel(QQmlEngine* engine) + : m_engine{engine} { setDisplayName(tr("QodeAssist Chat")); setPriority(500); @@ -19,10 +20,7 @@ NavigationPanel::~NavigationPanel() {} Core::NavigationView NavigationPanel::createWidget() { - Core::NavigationView view; - view.widget = new ChatWidget; - - return view; + return {.widget = new ChatWidget{m_engine}}; } } // namespace QodeAssist::Chat diff --git a/chat/NavigationPanel.hpp b/chat/NavigationPanel.hpp index 5801471..29e9ae1 100644 --- a/chat/NavigationPanel.hpp +++ b/chat/NavigationPanel.hpp @@ -5,6 +5,9 @@ #include #include +#include + +class QQmlEngine; namespace QodeAssist::Chat { @@ -12,10 +15,13 @@ class NavigationPanel : public Core::INavigationWidgetFactory { Q_OBJECT public: - explicit NavigationPanel(); + explicit NavigationPanel(QQmlEngine* engine); ~NavigationPanel(); Core::NavigationView createWidget() override; + +private: + QPointer m_engine; }; } // namespace QodeAssist::Chat diff --git a/qodeassist.cpp b/qodeassist.cpp index 0620a37..349081a 100644 --- a/qodeassist.cpp +++ b/qodeassist.cpp @@ -151,11 +151,13 @@ public: UpdateDialog::checkForUpdatesAndShow(Core::ICore::mainWindow()); }); + m_engine = new QQmlEngine{this}; + if (Settings::chatAssistantSettings().enableChatInBottomToolBar()) { - m_chatOutputPane = new Chat::ChatOutputPane(this); + m_chatOutputPane = new Chat::ChatOutputPane{m_engine}; } if (Settings::chatAssistantSettings().enableChatInNavigationPanel()) { - m_navigationPanel = new Chat::NavigationPanel(); + m_navigationPanel = new Chat::NavigationPanel{m_engine}; } Settings::setupProjectPanel(); @@ -204,7 +206,7 @@ public: showChatViewAction.setIcon(QCODEASSIST_CHAT_ICON.icon()); showChatViewAction.addOnTriggered(this, [this] { if (!m_chatView) { - m_chatView.reset(new Chat::ChatView()); + m_chatView.reset(new Chat::ChatView{m_engine}); } if (!m_chatView->isVisible()) { @@ -302,6 +304,7 @@ private: QString m_lastRefactorInstructions; QScopedPointer m_chatView; QPointer m_mcpServerManager; + QPointer m_engine; }; } // namespace QodeAssist::Internal