How to create some independent windows in QML - qt

I want to create a start window like in Wolfram Mathematica, where the user can create/open file, after this window get hidden, and the program create a new window with the file editor, after closing the editor, the editor window get closed and the program show again the start window. How to make some independent windows in QML?

Since you want to have them static, and independent, it might be one idea to create multiple windows from C++.
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QUrl("./main_0.qml")));
engine.load(QUrl(QUrl("./main_1.qml")));
if (engine.rootObjects().isEmpty()) {
qDebug() << "crap";
}
return app.exec();
}
Then use any kind of common data model (e.g. context properties, singletons ...) to set them visible.

you can do something like this:
Start.qml
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
visible: true
width: 640
height: 480
title: qsTr("Start Window")
onClosing: {
var component = Qt.createComponent("qrc:/Editor.qml");
component.createObject();
}
}
Editor.qml
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
visible: true
width: 640
height: 480
title: qsTr("Editor")
onClosing: {
var component = Qt.createComponent("qrc:/Start.qml");
component.createObject();
}
}
Closing one view will make the other view appear. So you probably should add another button for the case that the user wants to completely quit the application.
If you want your views to be created only once, you can ignore the close event and set the visibility instead. You can for example do something like this:
Start.qml
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
id: mainStartView
visible: true
width: 640
height: 480
title: qsTr("Start Window")
property var editorView
Component.onCompleted: {
editorView = editorComponent.createObject(mainStartView);
}
onClosing: {
close.accepted = false
editorView.visible = true
mainStartView.visible = false
}
Component {
id: editorComponent
Editor {
visible: false
}
}
}
Editor.qml
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
width: 640
height: 480
title: qsTr("Editor")
onClosing: {
close.accepted = false
mainStartView.visible = true
visible = false
}
}

Related

Transport QML Image via Bridge

I have at the moment a simple PySide6 app which uses qml for image capturing. I want to transport the image to our app but cannot figure out how. I am not sure, if saving QML image is an attempt in the right direction.
My PySide App
import sys
from pathlib import Path
from PySide6.QtCore import QObject, Slot
from PySide6.QtGui import QGuiApplication, QImage
from PySide6.QtQml import QQmlApplicationEngine, QmlElement
QML_IMPORT_NAME = "io.qt.textproperties"
QML_IMPORT_MAJOR_VERSION = 1
#QmlElement
class Bridge(QObject):
#Slot(QImage)
def capture(self, preview):
# Do something with the preview
print(type(preview))
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
# Get the path of the current directory, and then add the name
# of the QML file, to load it.
qml_file = Path(__file__).parent / "simpleCam.qml"
engine.load(qml_file)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
MY QML File
import QtQuick
import QtQuick.Controls
import QtMultimedia
import io.qt.textproperties
ApplicationWindow {
id: mainFrame
width: 640
height: 480
visible: true
title: qsTr("Cam Test")
Bridge {
id: bridge
}
Rectangle {
width: 640
height: 400
MediaDevices {
id: mediaDevices
}
CaptureSession {
imageCapture: ImageCapture {
id: capture
}
camera: Camera {
id: camera
}
videoOutput: output
}
VideoOutput {
id: output
anchors.fill: parent
}
Button {
id: startCamButton
text: "Start Cam"
anchors.top: output.bottom
anchors.left: output.left
onClicked: {
camera.start()
camImage.opacity = 0
}
}
Button {
id: takePicButton
text: "take pic"
anchors.top: output.bottom
anchors.left: startCamButton.right
onClicked: {
capture.capture()
camImage.opacity = 1
}
}
Image {
id: camImage
anchors.fill: parent
source: capture.preview
}
}
}
The Result
What is missing?
In the Button with id: takePicButton I want to transport the image to the PySide6 Bridge. If I add to theonClicked Signal bridge.capture(capture.preview) I got the Error:
TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed.`
How can I solve this problem?
You should use the imageCaptured signal.
Change:
imageCapture: ImageCapture {
id: capture
}
To:
imageCapture: ImageCapture {
id: capture
onImageCaptured: function(req_id, preview){bridge.capture(req_id, preview)}
}
And change in main.py:
#Slot(QImage)
def capture(self, preview):
# Do something with the preview
print(type(preview))
To:
#Slot(int, QImage)
def capture(self,req_id, preview):
print(req_id)
print(type(preview))
Working example here.
You might want to look at this answer for other use cases.

Popup item loses opaquness

I have a weird problem with Popup QML component from QtQuick2 , when I do open() it, it shows with transparent background , and it should be opaque.
This is how I call the Popup component from the main.qml file :
NewUser {
id: new_user_form
}
This is the component's source:
// File: NewUser.qml
Popup {
id: new_user_popup
modal: true
focus: true
x: 10
y: 10
width: 300
height: 200
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "red"
}
}
And this is the output:
Now, I can fix this error by moving the source of the NewUser.qml into main.qml and everything looks fine now:
This is the now 'fixed' main.qml:
Popup {
id: new_user_form
modal: true
focus: true
x: 10
y: 10
width: 300
height: 200
}
See, the popup is completely OPAQUE:
So, why if I move the source code of the component into a separate file from main.qml it loses the opaquness ? My main.qml has a lot of other stuff, but I believe it is not related to the popup, also the ID is unique. I hope this is not some issue with QT 5.8 RC (which I am using for development) , it is not yet official but soon it will be.
EDIT:
I believe I found a bug. The bug is reproducible with Qt 5.8 and Qt 5.7
To reproduce, create a project with the following files:
Main file:
//main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
NewUser {
id: new_user_form
}
Button {
text: "open popup"
onClicked: {
new_user_form.open()
}
}
}
The component file (NewUser.qml)
//File: NewUser.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import QtQuick.Templates 2.0
Popup {
id: new_user_popup
modal: true
focus: true
x: 10; y:10;
height: 200; width: 300;
Button {
text: "Test button"
}
}
//File: qtquickcontrols2.conf
; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
[Controls]
Style=Material
[Universal]
Theme=Light
;Accent=Steel
[Material]
Theme=Light
;Accent=BlueGrey
;Primary=BlueGray
//File: main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
That does the bug do? If you click on the button "open popup" you will see a dimmed screen but no popup at all.
This is not a bug. See, you are not using a namespace for the templates import.
import QtQuick.Templates 2.0 as T
For the sake of clarity, there is a one-to-one mapping between the types provided by the QtQuick.Templates and QtQuick.Controls imports. For every type available in the QtQuick.Controls import, a non-visual template type by the same name exists in the QtQuick.Templates import. It is recommended to use a namespace for the templates import to avoid overlap with the types provided by the QtQuick.Controls import.

QML animation - moving the application window

I want to create an animation to move (or resize) my application Window built using QML.
I have the following code (most of it created by default when we create a QT quick Controls application:
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
MainForm.ui.qml
import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Layouts 1.3
Item {
width: 640
height: 480
property alias button1: button1
property alias button2: button2
RowLayout {
anchors.centerIn: parent
Button {
id: button1
text: qsTr("Press Me 1")
}
Button {
id: button2
text: qsTr("Press Me 2")
}
}
}
main.qml
import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Dialogs 1.2
ApplicationWindow {
id: mainWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
flags: Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("&Open")
onTriggered: console.log("Open action triggered");
}
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
MainForm {
anchors.fill: parent
button1.onClicked: Qt.quit();
button2.onClicked: state = "other";
}
transitions: [
Transition {
from: "*"
to: "other"
NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad; duration: 2000 }
}
]
states: [
State {
name: "other"
PropertyChanges {
target: mainWindow
x: x + 200
}
}
]
MessageDialog {
id: messageDialog
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}
With this code, I was simply trying to move my window 200 pixels to the right. When I try to run it, I get qrc:/main.qml:42 Cannot assign to non-existent property "states". I believe that is weird, because when I start typing "states" and choose to auto-complete, it builds to me the whole structure, so I thought it should exist...
I'm new to QML, and I'm not fully familiar with the several options of animations that exists. This one I've tried based on the example that comes with QT creator (animation.pro - code from transitions).
I believe it should be quite simple, right? Could you help me with this?
Qt Creator has a feature where you can insert code snippets using certain keywords. You can see which snippets are available by going to Tools > Options > Text Editor > Snippets.
Snippets will show up as red in the auto completion popup, and regular properties (or types, as is the case below) will show up as green:
So, ApplicationWindow doesn't have a states property. If you're ever in doubt, go to the documentation for the type you're interested in (e.g. http://doc.qt.io/qt-5/qml-qtquick-controls-applicationwindow.html) and click on the link that says "List of all members, including inherited members". This will show you all properties, functions, etc. belonging
to that type.
If you want to animate the window's position, you can use a NumberAnimation without using states:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.5
ApplicationWindow {
id: window
width: 400
height: 400
visible: true
NumberAnimation {
id: xyAnimation
target: window
properties: "x,y"
easing.type: Easing.InOutQuad
duration: 2000
to: 500
}
Button {
text: "Start"
onClicked: xyAnimation.start()
}
}
state is a property in Item, however ApplicationWindow is not an Item. To add state/transition to an non-Item type, use StateGroup:
ApplicationWindow {
id: mainWindow
//your code...
MainForm {
button2.onClicked: { myWindowStates.state = "other";}
}
StateGroup {
id: myWindowStates
transitions: [
Transition {
from: "*"; to: "other"
NumberAnimation {
properties: "x,y"; easing.type: Easing.Linear;
duration: 2000
}
}
]
states: [
State {
name: "other"
PropertyChanges {
target: mainWindow
x: mainWindow.x + 200
explicit: true //Remember to set this
}
}
]
}
}
Remember to set PropertyChange.explict to true, otherwise the state behavior is wrong and your window will disappear after the transition finished.

How to get NavigationKey.up value in qml and c++?

Im beginner in qml. I have set KeyNavigation.up (in item up) to an id of another item (down).
Why i could't retrieve KeyNavigation.up like this in qml ?!
var x = down.KeyNavigation.up
UPDATE:
This is an example. why i couldn't get a.KeyNavigation.up ?
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
TextField {
id: up
KeyNavigation.down: down
}
TextField {
id: down
KeyNavigation.up: up
}
function fun(){
var x = up.KeyNavigation.down
}
}
Your code actually works.
I don't see where you're calling fun(), but if you for example add the line onActiveFocusItemChanged: fun() after visible: true you will see the x variable is OK.
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
visible: true
width: 500; height: 500
onActiveFocusItemChanged: fun()
function fun(){
var x = up.KeyNavigation.down
print("x = up.KeyNavigation.down: " + x.text)
var y = x.KeyNavigation.up
print("y = x.KeyNavigation.up: " + y.text)
}
TextField {
id: up
y: 50
KeyNavigation.down: down
}
TextField {
id: down
y: 100
KeyNavigation.up: up
}
}
In qml, items have properties. And you should declare properties. Without your whole code i cannot really give you a more detailed answer. Just have a look at this
In QML, ids must begin with a lowercase letter. If you're using Qt Creator, you'd see this problem as soon as you typed it. When you run the program, you'd see this error:
IDs cannot start with an uppercase letter
So, either you typed your code in manually for this question (bad idea) and you mistyped the ids, or you didn't run it, which would be strange.
Using a lowercase id works:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
Component.onCompleted: print(a.KeyNavigation.up)
Item {
id: A
KeyNavigation.up: b
}
Item {
id: b
}
}
Output:
qml: QQuickItem(0x596dcc9180)

Qt Expected Character ')' Error

I am getting the following error:
Debugging starts QML debugging is enabled. Only use this in a safe
environment. QML Debugger: Waiting for connection on port 55186...
QQmlApplicationEngine failed to load component qrc:/main.qml:23
Expected token `)'
Line 23
QFile, file("C://new.txt");
The Code
#include <QIODevice>
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQml 2.2
ApplicationWindow {
title: qsTr("File Editor")
width: 640
height: 480
visible: true
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: {
var message = ("Hello World!");
QFile, file("C://new.txt");
file.open(QIODevice::ReadWrite);
QTextStream out(&file);
out << %message%;
}
}
MenuItem {
text: qsTr("E&xit")
onTriggered: Qt.quit();
}
}
}
MainForm {
anchors.fill: parent
button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
button3.onClicked: messageDialog.show(qsTr("Button 3 pressed"))
}
MessageDialog {
id: messageDialog
title: qsTr("May I have your attention, please?")
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}
As stated by Simon Warta and BaCaRoZzo in the comments, you cannot use C++ in QML. You need to use Javascript and create your own custom type for handling file input and output.
Please see this answer.

Resources