All Projects → lbwa → qwebchannel-bridge

lbwa / qwebchannel-bridge

Licence: other
🌉How to intergrate @qt qwebchannel with @vuejs

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
Vue
7211 projects
HTML
75241 projects

Projects that are alternatives of or similar to qwebchannel-bridge

qtorm
Object-Relational Mapping Module for Qt
Stars: ✭ 16 (-5.88%)
Mutual labels:  qt5
gamestudio
2D game engine and editor
Stars: ✭ 207 (+1117.65%)
Mutual labels:  qt5
3d-nii-visualizer
A NIfTI (nii.gz) 3D Visualizer using VTK and Qt5
Stars: ✭ 86 (+405.88%)
Mutual labels:  qt5
LibreMines
A Free/Libre and Open Source Software Qt based Minesweeper game available for GNU/Linux, FreeBSD and Windows systems.
Stars: ✭ 55 (+223.53%)
Mutual labels:  qt5
DrawingBoard
NJU_Graphics_Homework / 计算机图形学课程大作业
Stars: ✭ 37 (+117.65%)
Mutual labels:  qt5
Morpheus
A Matrix client written in Go-QT
Stars: ✭ 20 (+17.65%)
Mutual labels:  qt5
dockingpanes
A Visual Studio style docking windows library for Qt Widgets based applications
Stars: ✭ 52 (+205.88%)
Mutual labels:  qt5
QSourceHighlite
Lightweight syntax highlighter written in Qt
Stars: ✭ 39 (+129.41%)
Mutual labels:  qt5
studio
GAMS Studio
Stars: ✭ 25 (+47.06%)
Mutual labels:  qt5
HandsomeMod
IOT freedom for end users!
Stars: ✭ 39 (+129.41%)
Mutual labels:  qt5
qt5-tutorials
graphics dojo Qt5
Stars: ✭ 29 (+70.59%)
Mutual labels:  qt5
erk
Ərk is an open source, cross-platform IRC client written in Python 3, Qt 5, and Twisted.
Stars: ✭ 21 (+23.53%)
Mutual labels:  qt5
ModBuddy
Mod buddy is a mod manager created with extensibility in mind
Stars: ✭ 30 (+76.47%)
Mutual labels:  qt5
sugaroid
The not-that intelligent, but cute Artificially Intelligent bot, created when I was bored.
Stars: ✭ 12 (-29.41%)
Mutual labels:  qt5
idasix
IDAPython compatibility library. idasix aims to create a smooth ida development process and allow a single codebase to function with multiple IDA/IDAPython versions
Stars: ✭ 24 (+41.18%)
Mutual labels:  qt5
QtDemos
This is a demo about Qt5, including Qt Custom Widget, Qt Multithreaded Downloader, QML Video Player(using OpenGL, FFmpeg and SDL2)
Stars: ✭ 18 (+5.88%)
Mutual labels:  qt5
diagnostictools
Plugin for Qt Creator (Memory and CPU usage visualizer)
Stars: ✭ 41 (+141.18%)
Mutual labels:  qt5
Crusta
Fast, modern and minimal desktop web browser with rich features
Stars: ✭ 53 (+211.76%)
Mutual labels:  qt5
geotagging
Photography geotagging tool
Stars: ✭ 17 (+0%)
Mutual labels:  qt5
DeadAscend
A 2D point'n'click-like adventure game written in Qt/QML and Javascript
Stars: ✭ 37 (+117.65%)
Mutual labels:  qt5

QWebChannel bridge

This is an integration approach for QWebChannel with Vue.js v2, consisted of a message broker layer.

中文指南

Prerequisites

Qt side should provide one or more QObject which include all information shared with JS side.

  1. A Cpp function named emitEmbeddedPageLoad

    class WebBridge: public QObject {
      Q_OBJECT
      public slots:
        void emitEmbeddedPageLoad() {
            QMessageBox::information(NULL,"emitEmbeddedPageLoad", "I'm called by client JS!");
        }
    };
    
    WebBridge *webBridge = new WebBridge();
    QWebChannel *channel = new QWebChannel(this);
    channel->registerObject('keyNamedContext', webBridge);
    view->page()->setWebChannel(channel);
  2. In JS side, you should provide a init function when QWebChannel initialized.

    new QWebChannel(window.qt.webChannelTransport, function(channel) {
      const published = channel.objects.keyNamedContext
      Vue.prototype.$_bridge = published
    
      // This function calling will notify Qt server asynchronously
      published.emitEmbeddedPageLoad('', function(payload: string) {
        // This payload has included dispatcher name and its parameters.
        dispatch(payload)
        console.info(`
            Bridge load !
          `)
      })
    })

    Advance: You can also create a process like these implementation for function calling or properties reading with abstract namespace.

  3. dispatch function should include all navigation logic.

Once QWebChannel initialized, dispatch will be invoked when Cpp function named emitEmbeddedPageLoad return a value async notification. dispatch function would play a navigator role in JS side.

How to navigate

  1. In Qt side, all entry point should be based on root path - https://<YOUR_HOST>/. All navigation will be distributed by JS side (vue-router, a kind of front-end router) rather than Qt. Qt side would has more opportunities to focus on other business logic.

    • When Qt side receives a initial message from JS side, it should return a value which syntax should be like:

      interface InitialProps {
        type: string
        payload: any
      }
      // Actual value
      {
        type: [JS_SIDE_DISPATCHER_NAME],
        payload: [OPTIONAL_PAYLOAD]
      }

    type property will be used to invoke dispatcher in the dispatchersMap, then payload property including any messages from Qt side will passed dispatcher. dispatcher in the dispatchersMap plays a navigator role in front-end, and developer should add navigation logic into here. This is all secrets about front-end navigation without Qt routing.

    Above all process has described how to initialize Vue.js app in the QWebEngine, and how navigation works in the Vue.js with QWebEngine.

  2. Be careful any external link and redirect uri from any external web site like Alipay online payment links. If you want to respect any redirect uri and prevent navigation from above dispatch function, you MUST provide non-root redirect uri (eg. https://<YOUR_HOST>/#/NOT_EMPTY_PATH). You can find more details from dispatch function here.

How to push message from JS side

If you want to push messages from JS side to Qt side, you can invoke the mapping of Qt methods in JS side directly:

channel.object[QObjectJSMappingKey].methodNameMappingFromQtSide(
  payload,
  callback
)

Enhance: the following logic is based on these implementation:

// A QObject named `QObjectJSMappingKey` (as an abstract namespace) in Qt/JS side
// in the vue instance
this.$$pusher.QObjectJSMappingKey({
  action: 'QT_QOBJECT_KEY',
  payload: 'CALLING_PAYLOAD'
})

How to push messages from Qt side

Qt signal listener mechanism is a kind of good solution for communicate from Qt side to JS side. You may be wondering why we don't use signal mechanism directly to handle first frontend navigation? Because Qt side never known when frontend router is available until JS side push loaded message to Qt side positively.

class WebBridge: public QObject {
  Q_OBJECT
  public slots:
    void emitEmbeddedPageLoad();

  // define your own signal
  signals:
    void signalMessageFromQt(const QString &str);
};

Always define all available signal listeners in config/bridge.ts:

interface SignalCallbacks {
  [targetSignal: string]: Function
}

All signal would be handled automatically by these codes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].