Version

Goal:Control an embedded Harfang view
Difficulty:Intermediate
Duration:20 minutes
Language:Python

This tutorial will only focus on the Harfang-Qt integration. Basic knowledge of the Qt library is also assumed.

Requirements

This tutorial make use of the following additional Python packages:

  • PyQt5

Preparing the Qt interface

After designing the application interface in QT Designer, convert the .ui file to Python with the pyuic5 tool: pyuic5 window_plotter.ui -o ui_window_plotter.py.

Note: The pyuic5 tool can be found in the PyQt5 installation binary folder.

Engine initialization

Provide access to the engine core resources, see The file system.

import harfang as hg
hg.MountFileDriver(hg.StdFileDriver("../pkg.core"), "@core")

Create a renderer and initialize it in quiet mode. This mode creates a default window but does not show it to the user.

renderer = hg.CreateRenderer()
if not renderer.Open(8, 8, 32, hg.VideoWindowedQuiet):
    sys.exit(0)

We now must instantiate the Qt interface and embed a new renderer window in it.

Instantiating the interface and embedding an engine window

Import the interface code generated by the pyuic5 tool.

from ui_window_plotter import Ui_WindowPlotter

Declare a class deriving from QMainWindow which will manage the interface.

from PyQt5.QtWidgets import QMainWindow

class WindowPlotter(QMainWindow):

When instantiating the class we pass it the Renderer and RenderSystem instances to use.

    def __init__(self, renderer, render_system):
        super().__init__()

        self.renderer, self.render_system = renderer, render_system

Create the window interface from the generated Python code.

        self.ui = Ui_WindowPlotter()  # instantiate the interface
        self.ui.setupUi(self)  # setup internal connections

Retrieve the Qt widget handle.

        self.view_handle = hg.int_to_voidp(int(self.ui.viewport_widget.winId()))

Create a new renderer output window over the Qt widget handle.

        self.window = renderer.NewOutputWindow(16, 16, 32, hg.VideoWindowed, self.view_handle)
        self.aspect_ratio = hg.Vector2(1, 1)

At this point the renderer current output window is our newly created window and all rendering operations will happen there.

You can create more output windows and switch between them using the [Renderer_SetOutputWindow] method.