Custom desktop - a Windows form dashboard
This example illustrates some of the key concepts required in order to build a custom desktop. This includes how to launch and shut down Qlik Sense as a background process, how to show Qlik visualizations in a windows form, how to make a Qlik Sense .NET SDK application sensitive to change notifications, and how to synchronize the state of a Qlik Sense .NET SDK application with mashups.
We recommend that you download and run the appropriate example before you continuing reading. All the sample code for the connection examples are available on Github.
Source code found on Github (only in English)
Launch the engine
This section launches Qlik Sense and opens a connection.
private static Process LaunchQlikSense() { Process process = null; try { // Launch QlikSense Desktop process = Process.Start(PathToQlikSense); } catch (Exception e) { MessageBox.Show(e.Message, "Engine Launch Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(1); } return process; }
Prepare app
This example demonstrates how to create a custom desktop that uses a basic Qlik Sense App containing a bar chart and a pie chart as data sources. This Qlik Sense app is shipped with the code as a resource file, and before opening the app, the example will copy the app file to the default path where Qlik Sense apps are stored. (C:\Users\<user>\Documents\Qlik\Sense\Apps)
Example:
private static IApp PrepareApp(ILocation location) { File.WriteAllBytes(DesktopAppPath, Resources.CustomDesktopApp); var appIdentifier = location.AppWithNameOrDefault(CustomDesktopAppName); return location.App(appIdentifier); }
Event handling
The custom desktop example and the web browsers used to visualize the charts need to reflect the same selection state in order to interact correctly. Selection state is part of the information managed by sessions, so the desktop and the two browsers need to connect to the same session. The use of the methods AppWithNameOrDefault and SingleUrl ensures that they all connect to the default session.
The app object contains an event called Changed that will be triggered every time the engine registers a change in the state of the specific object. We add a custom event handler to make the Qlik Sense .NET SDK app react to such events, and then update the state of the selection buttons according to the current selection state of the app. To start the flow of change notifications, we need to perform a calculation on the app, and in this example we do this by retrieving the layout of the app.
Example: A notification will be sent when a change occurs
_theApp.Changed += TrackSelectionState; _theApp.GetAppLayout();
Load
This section opens the mashup in the web browser. It sets up a subscription to get a notification when the mashup is updated.
Example:
private void DesktopWindow_Load(object sender, EventArgs e) { // Add change notification handler and evaluate clean state to start change // notification flow. _theApp.Changed += TrackSelectionState; _theApp.GetAppLayout(); // Add single mashups of the two objects we want to add to the desktop. var visualizations = TheApp.GetSheets().First().Children.ToArray(); var barchart = visualizations.OfType<Barchart>().First(); var piechart = visualizations.OfType<Piechart>().First(); webBrowser1.Navigate(barchart.SingleUrl().Fix()); webBrowser2.Navigate(piechart.SingleUrl().Fix()); }
Closed
When the desktop is closed, it will shut down the Qlik Sense process and delete the desktop app it created in the setup stage.
Example:
private void DesktopWindow_Closed(object sender, FormClosedEventArgs formClosedEventArgs) { Hide(); // Perform clean shutdown of the Qlik Sense process and delete the app resource. TheLocation.Hub().ShutdownProcess(); TheProcess.WaitForExit(); File.Delete(DesktopAppPath); }
Buttons
Use the buttons Forward, Back, and Clear to navigate in the app.
Learn more
- QixClassBase.Changed Event API reference.