C

Qt VNC Server - VncChat Example

Demonstrates how to make a simple application that uses Qt VNC Server.

VncChat demonstrates the use of the Qt VNC Server module.

The example is a very basic and disorganized chat application, which consists of a text editor that is exposed to VNC®-compatible clients and can be edited by anyone at any time.

The text editor and its frame is wrapped by a VncItem which causes it to automatically be shared over the connection:

VncItem {
    id: grabber
    anchors.fill: parent

    vncPort: -1

    Rectangle {
        id: editorFrame
        anchors.fill: parent
        anchors.margins: window.width * 0.1
        radius: window.width * 0.1

        color: "gainsboro"
        TextArea {
            id: textArea
            anchors.fill: parent
            anchors.margins: window.width * 0.1
            background: null
            placeholderText: "Say something nice"
            placeholderTextColor: "darkslategrey"
        }
    }
}

Only the contents of the VncItem will be shared, so the colored frame around it and application title will only be visible locally.

The requested port of the VncItem is set to -1.

vncPort: -1

This means the server will be cycling through ports 5900 to 5999 until it finds an available port. Automatically selecting a port can be convenient if the server is running on a machine where the default 5900 port may already be in use by other applications, such as a remote desktop sharing mechanism. Information about the actually selected port is displayed, so that it can be shared with the clients.

Label {
    id: portLabel
    anchors.top: titleLabel.bottom
    anchors.horizontalCenter: activeRect.horizontalCenter
    text: grabber.serverState === VncItem.Connected
          || grabber.serverState === VncItem.Listening
          ? "(Port " + grabber.vncPort + " is open for business)"
          : (grabber.serverState === VncItem.InitializationFailed ? "(Error initializing server)" : "")
    font.pixelSize: window.height * 0.015
}

Finally, the locally running application has a frame around the VncItem which changes color depending on whether a client is connected or not. In the screenshot, this is dark sea green, indicating that a client has connected. When no client is connected, it will be the color 'salmon':

color: grabber.serverState === VncItem.Connected ? "darkseagreen" : "salmon"

Files:

Available under certain Qt licenses.
Find out more.