Skip to content

Commit ed7c8a5

Browse files
committed
feat: web channel basic test
1 parent b575de4 commit ed7c8a5

7 files changed

Lines changed: 72 additions & 5 deletions

File tree

Editor/Include/Editor/Widget/ProjectHub.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77
#include <Editor/Widget/WebWidget.h>
88

99
namespace Editor {
10+
class ProjectHub;
11+
12+
class ProjectHubBridge final : public QObject {
13+
Q_OBJECT
14+
15+
public:
16+
explicit ProjectHubBridge(ProjectHub* parent = nullptr);
17+
18+
public Q_SLOTS:
19+
void CreateProject() const;
20+
};
21+
1022
class ProjectHub final : public WebWidget {
1123
Q_OBJECT
1224

1325
public:
1426
explicit ProjectHub(QWidget* inParent = nullptr);
27+
28+
private:
29+
ProjectHubBridge* bridge;
1530
};
1631
}

Editor/Include/Editor/Widget/WebWidget.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#pragma once
66

7+
#include <QWebChannel>
78
#include <QWebEngineView>
89

910
namespace Editor {
@@ -15,5 +16,11 @@ namespace Editor {
1516
~WebWidget() override;
1617

1718
void Load(const std::string& inUrl);
19+
20+
protected:
21+
QWebChannel* GetWebChannel() const;
22+
23+
private:
24+
QWebChannel* webChannel;
1825
};
1926
}

Editor/Src/Widget/ProjectHub.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22
// Created by johnk on 2025/8/3.
33
//
44

5+
#include <QWebChannel>
6+
57
#include <Editor/Widget/ProjectHub.h>
68
#include <Editor/Widget/moc_ProjectHub.cpp>
9+
#include <Core/Log.h>
710

811
namespace Editor {
12+
ProjectHubBridge::ProjectHubBridge(ProjectHub* parent)
13+
: QObject(parent)
14+
{
15+
}
16+
17+
void ProjectHubBridge::CreateProject() const
18+
{
19+
// TODO
20+
LogInfo(ProjectHub, "ProjectHubBridge::CreateProject");
21+
}
22+
923
ProjectHub::ProjectHub(QWidget* inParent)
1024
: WebWidget(inParent)
1125
{
1226
setFixedSize(800, 600);
1327
Load("/project-hub");
28+
29+
bridge = new ProjectHubBridge(this);
30+
GetWebChannel()->registerObject("bridge", bridge);
1431
}
1532
} // namespace Editor

Editor/Src/Widget/WebWidget.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Editor {
1818
WebWidget::WebWidget(QWidget* inParent)
1919
: QWebEngineView(inParent)
2020
{
21+
webChannel = new QWebChannel(this);
22+
page()->setWebChannel(webChannel);
2123
}
2224

2325
WebWidget::~WebWidget() = default;
@@ -31,4 +33,9 @@ namespace Editor {
3133
const std::string fullUrl = baseUrl + inUrl;
3234
load(QUrl(fullUrl.c_str()));
3335
}
36+
37+
QWebChannel* WebWidget::GetWebChannel() const
38+
{
39+
return webChannel;
40+
}
3441
} // namespace Editor

Editor/Web/src/pages/project-hub.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import { useEffect } from "react";
2+
import { QWebChannel } from '@/qwebchannel'
13
import { Tabs, Tab } from '@heroui/tabs';
24
import { User } from '@heroui/user';
35
import { Form } from '@heroui/form';
46
import { Button } from '@heroui/button';
57
import { Input } from '@heroui/input';
68

79
export default function ProjectHubPage() {
10+
useEffect(() => {
11+
new QWebChannel(window.qt.webChannelTransport, (channel: QWebChannel) : void => {
12+
window.bridge = channel.objects.bridge;
13+
})
14+
}, []);
15+
16+
function onCreateProject(): void
17+
{
18+
window.bridge.CreateProject();
19+
}
20+
821
return (
922
<div className='h-screen p-6'>
1023
<div className='mb-4'>
@@ -23,7 +36,7 @@ export default function ProjectHubPage() {
2336
<Input fullWidth isRequired label='Project Name' labelPlacement='outside' placeholder='HelloExplosion'/>
2437
<Input fullWidth isRequired label='Project Description' labelPlacement='outside' placeholder='A simple explosion game project.'/>
2538
<Input fullWidth isRequired label='Project Path' labelPlacement='outside' placeholder='/path/to/your/project'/>
26-
<Button color='primary'>Create</Button>
39+
<Button color='primary' onPress={onCreateProject}>Create</Button>
2740
</Form>
2841
</Tab>
2942
<Tab title='Recently Projects'>

Editor/Web/src/qwebchannel.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { QWebChannel } from './qwebchannel.js';
2+
3+
declare global {
4+
interface Window {
5+
qt: any;
6+
bridge: any;
7+
}
8+
}

Editor/Web/src/qwebchannel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ QObject.prototype.toJSON = function() {
448448
};
449449

450450
//required for use with nodejs
451-
if (typeof module === 'object') {
452-
module.exports = {
453-
QWebChannel: QWebChannel
454-
};
451+
//++[kindem] use ES6 export
452+
export {
453+
QWebChannel
455454
}
455+
//--[kindem]

0 commit comments

Comments
 (0)