0

I have the next QML:

import Qt.labs.platform 1.0 SystemTrayIcon { visible: true iconSource: "qrc:/icons/ic_tray.png" menu: Menu { MenuItem { text: qsTr("Settings") onTriggered: { // Don't create a new object if it exists, just show var settings = Qt.createComponent("main.qml") var form = settings.createObject(this) form.show() } } MenuItem { text: qsTr("Quit") onTriggered: Qt.quit() // Just hide an existing } } } 

How to create main.qml one time only and after just show/hide?
P.S. I'm learning Qt including QtQuick 2 only

2 Answers 2

1

Depending on how your application is structured, the best way could be to pass in the window that the tray icon shall control as a property from "further up" your user interface structure.

First, extend your tray icon component and add a "window" property to it:

import QtQuick 2.9 import QtQuick.Window 2.2 import Qt.labs.platform 1.0 SystemTrayIcon { id: trayIcon // this property holds the window the tray icon controls: property Window window visible: true iconSource: "qrc:/icons/ic_tray.png" menu: Menu { MenuItem { text: qsTr("Settings") onTriggered: { trayIcon.window.show(); } } MenuItem { text: qsTr("Quit") onTriggered: Qt.quit() // Just hide an existing } } } 

Now, you could instantiate your tray icon e.g. in your main window like this:

import QtQuick 2.9 import QtQuick.Window 2.2 Window { id: mainWindow width: 800 height: 600 TrayIcon { window: mainWindow } } 

In this case, the tray icon would control the main window itself; however, you can easily create a single instance of a settings window within the main window and pass that one to the tray icon.

Sign up to request clarification or add additional context in comments.

Comments

1

You can create the component in the onCompleted.

SystemTrayIcon { visible: true iconSource: "qrc:/icons/ic_tray.png" menu: Menu { MenuItem { text: qsTr("Settings") property var form onTriggered: { form.show() } Component.onCompleted: { // Don't create a new object if it exists, just show var settings = Qt.createComponent("Test.qml") form = settings.createObject(this) } } MenuItem { text: qsTr("Quit") onTriggered: Qt.quit() // Just hide an existing } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.