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 <ilebedev@flightpath3d.com>
Co-authored-by: Ivan Lebedev <ilebedev1988@gmail.com>
This commit is contained in:
Ivan Lebedev
2026-04-28 16:33:03 +02:00
committed by GitHub
parent bbb9c47cbb
commit 9bb6d55687
9 changed files with 45 additions and 21 deletions

View File

@@ -3,6 +3,7 @@
#include "ChatView.hpp" #include "ChatView.hpp"
#include <QQmlComponent>
#include <QQmlContext> #include <QQmlContext>
#include <QQmlEngine> #include <QQmlEngine>
#include <QSettings> #include <QSettings>
@@ -19,12 +20,21 @@ constexpr Qt::WindowFlags baseFlags = Qt::Window | Qt::WindowTitleHint | Qt::Win
namespace QodeAssist::Chat { namespace QodeAssist::Chat {
ChatView::ChatView() ChatView::ChatView(QQmlEngine* engine)
: m_isPin(false) : QQuickView{engine, nullptr}
, m_isPin(false)
{ {
setTitle("QodeAssist Chat"); setTitle("QodeAssist Chat");
engine()->rootContext()->setContextProperty("_chatview", this); /// @note setup quick view content
setSource(QUrl("qrc:/qt/qml/ChatView/qml/RootItem.qml")); {
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); setResizeMode(QQuickView::SizeRootObjectToView);
setMinimumSize({400, 300}); setMinimumSize({400, 300});
setFlags(baseFlags); setFlags(baseFlags);

View File

@@ -13,7 +13,7 @@ class ChatView : public QQuickView
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool isPin READ isPin WRITE setIsPin NOTIFY isPinChanged FINAL) Q_PROPERTY(bool isPin READ isPin WRITE setIsPin NOTIFY isPinChanged FINAL)
public: public:
ChatView(); ChatView(QQmlEngine* engine);
bool isPin() const; bool isPin() const;
void setIsPin(bool newIsPin); void setIsPin(bool newIsPin);

View File

@@ -8,10 +8,17 @@
namespace QodeAssist::Chat { namespace QodeAssist::Chat {
ChatWidget::ChatWidget(QWidget *parent) ChatWidget::ChatWidget(QQmlEngine* engine, QWidget *parent)
: QQuickWidget(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); setResizeMode(QQuickWidget::SizeRootObjectToView);
} }

View File

@@ -12,7 +12,7 @@ class ChatWidget : public QQuickWidget
Q_OBJECT Q_OBJECT
public: public:
explicit ChatWidget(QWidget *parent = nullptr); explicit ChatWidget(QQmlEngine* engine, QWidget *parent = nullptr);
~ChatWidget() = default; ~ChatWidget() = default;
Q_INVOKABLE void clear(); Q_INVOKABLE void clear();

View File

@@ -7,9 +7,9 @@
namespace QodeAssist::Chat { namespace QodeAssist::Chat {
ChatOutputPane::ChatOutputPane(QObject *parent) ChatOutputPane::ChatOutputPane(QQmlEngine* engine, QObject *parent)
: Core::IOutputPane(parent) : Core::IOutputPane(parent)
, m_chatWidget(new ChatWidget) , m_chatWidget{new ChatWidget{engine}}
{ {
setId("QodeAssistChat"); setId("QodeAssistChat");
setDisplayName(Tr::tr("QodeAssist Chat")); setDisplayName(Tr::tr("QodeAssist Chat"));

View File

@@ -13,7 +13,7 @@ class ChatOutputPane : public Core::IOutputPane
Q_OBJECT Q_OBJECT
public: public:
explicit ChatOutputPane(QObject *parent = nullptr); explicit ChatOutputPane(QQmlEngine* engine, QObject *parent = nullptr);
~ChatOutputPane() override; ~ChatOutputPane() override;
QWidget *outputWidget(QWidget *parent) override; QWidget *outputWidget(QWidget *parent) override;

View File

@@ -7,7 +7,8 @@
namespace QodeAssist::Chat { namespace QodeAssist::Chat {
NavigationPanel::NavigationPanel() NavigationPanel::NavigationPanel(QQmlEngine* engine)
: m_engine{engine}
{ {
setDisplayName(tr("QodeAssist Chat")); setDisplayName(tr("QodeAssist Chat"));
setPriority(500); setPriority(500);
@@ -19,10 +20,7 @@ NavigationPanel::~NavigationPanel() {}
Core::NavigationView NavigationPanel::createWidget() Core::NavigationView NavigationPanel::createWidget()
{ {
Core::NavigationView view; return {.widget = new ChatWidget{m_engine}};
view.widget = new ChatWidget;
return view;
} }
} // namespace QodeAssist::Chat } // namespace QodeAssist::Chat

View File

@@ -5,6 +5,9 @@
#include <coreplugin/inavigationwidgetfactory.h> #include <coreplugin/inavigationwidgetfactory.h>
#include <QObject> #include <QObject>
#include <QPointer>
class QQmlEngine;
namespace QodeAssist::Chat { namespace QodeAssist::Chat {
@@ -12,10 +15,13 @@ class NavigationPanel : public Core::INavigationWidgetFactory
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit NavigationPanel(); explicit NavigationPanel(QQmlEngine* engine);
~NavigationPanel(); ~NavigationPanel();
Core::NavigationView createWidget() override; Core::NavigationView createWidget() override;
private:
QPointer<QQmlEngine> m_engine;
}; };
} // namespace QodeAssist::Chat } // namespace QodeAssist::Chat

View File

@@ -151,11 +151,13 @@ public:
UpdateDialog::checkForUpdatesAndShow(Core::ICore::mainWindow()); UpdateDialog::checkForUpdatesAndShow(Core::ICore::mainWindow());
}); });
m_engine = new QQmlEngine{this};
if (Settings::chatAssistantSettings().enableChatInBottomToolBar()) { if (Settings::chatAssistantSettings().enableChatInBottomToolBar()) {
m_chatOutputPane = new Chat::ChatOutputPane(this); m_chatOutputPane = new Chat::ChatOutputPane{m_engine};
} }
if (Settings::chatAssistantSettings().enableChatInNavigationPanel()) { if (Settings::chatAssistantSettings().enableChatInNavigationPanel()) {
m_navigationPanel = new Chat::NavigationPanel(); m_navigationPanel = new Chat::NavigationPanel{m_engine};
} }
Settings::setupProjectPanel(); Settings::setupProjectPanel();
@@ -204,7 +206,7 @@ public:
showChatViewAction.setIcon(QCODEASSIST_CHAT_ICON.icon()); showChatViewAction.setIcon(QCODEASSIST_CHAT_ICON.icon());
showChatViewAction.addOnTriggered(this, [this] { showChatViewAction.addOnTriggered(this, [this] {
if (!m_chatView) { if (!m_chatView) {
m_chatView.reset(new Chat::ChatView()); m_chatView.reset(new Chat::ChatView{m_engine});
} }
if (!m_chatView->isVisible()) { if (!m_chatView->isVisible()) {
@@ -302,6 +304,7 @@ private:
QString m_lastRefactorInstructions; QString m_lastRefactorInstructions;
QScopedPointer<Chat::ChatView> m_chatView; QScopedPointer<Chat::ChatView> m_chatView;
QPointer<Mcp::McpServerManager> m_mcpServerManager; QPointer<Mcp::McpServerManager> m_mcpServerManager;
QPointer<QQmlEngine> m_engine;
}; };
} // namespace QodeAssist::Internal } // namespace QodeAssist::Internal