QML Import external javascript file - qt

Inspired by this Answer QML - Import external JavaScript file I am trying to load a .js file from a external URL.
import QtQuick 2.9
import QtQuick.Window 2.2
import 'http://code.qt.io/cgit/qt/qtdeclarative.git/plain/examples/quick/demos/photoviewer/PhotoViewerCore/script/script.js' as Test
Window {
id: window
visible: true
width: 600
height: 600
Component.onCompleted: console.log('It is:', Test.calculateScale(100, 100, 200) + '!\nMagical!')
}
But with the import 'http://...' statement my application is not even starting / crashes immediately after start (without any error message).
I have the strong feeling that I am missing something to get the external import working, but I have no idea what exactly.

Related

Using offline data with QtLocation plugin

I have 500mb of offline data I would like to use with the OSM QtLocation plugin. I used the sample source code from this blog https://www.qt.io/blog/2017/05/24/qtlocation-using-offline-map-tiles-openstreetmap-plugin. The data in the sample app is embedded in the resource file, I would like to have it in a separate folder on the disk. For a start I would like to get the sample app working with the sample data located in a folder on disk and not as compiled in resource files. When executing the app it still downloads the data from the online server. I have read lots of articles online but none of the solutions are working. I am using Qt 5.15.2, Windows 10 with MSVC compiler.
This is how my code looks like.
import QtQuick 2.7
import QtQuick.Window 2.2
import QtLocation 5.8
Window {
id: win
objectName: "window"
visible: true
width: 512
height: 512
Map {
id: map
anchors.fill: parent
activeMapType: map.supportedMapTypes[1]
zoomLevel: 1
plugin: Plugin {
id: osmPLugin
name: 'osm';
PluginParameter { name: 'osm.mapping.offline.directory'; value: 'file:///c:/offline_tiles/' }
}
}
}
You have to use the path, not the uri:
PluginParameter { name: 'osm.mapping.offline.directory'; value: 'c:/offline_tiles/' }

Defining a QML structure that only holds functions

I'm new to QT, and I've had difficulty finding resources on the internet, so I thought I'd try my luck here. I am trying to create a QML file that only contains functions (slots?) and import that file into my main application. The functions would specifically be related to handling phone calls (e.g. placeCall, endCall, callDisconnected, etc.). My first thought was to use create a "CallManager.qml" file and use the Component type like so:
import QtQuick 2.12
import QtQml 2.3
Component {
function makeCall(number) { ... }
function endCall() { ... }
// etc.
}
Then, I would do the following in main.qml:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
Window {
Button {
x: 0; y: 0
text: "Call"
onClicked: callManager.placeCall("1234567890")
}
CallManager {
id: callManager
}
}
I just want to know if this is a valid way to solve the problem or if there are better practices for achieving the same results.
You can just create a javascript file ".JS" in your resources and then import it like you import any QML file and in this JS file just add your functions and call them from any other QML file

FileDialog is fine is debug mode, but not as expected in release mode

A QML FileDialog to save a file works fine in debug mode.
The code is:
import QtQuick 2.5
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.1
import QtQuick.Controls.Styles 1.4
import Qt.labs.platform 1.0
Item {
property string exportSceneName: "exported_scene"
property url exportFolder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)
signal startExport()
onStartExport: {
runLogic()
}
function runLogic() {
// ...
}
Button {
onClicked: {
fileDialog.open()
}
}
FileDialog {
id: fileDialog
folder: exportFolder
fileMode: FileDialog.SaveFile
title: qsTr("Export Scene As STL")
onAccepted: {
exportFolder = folder
var name = basename(file)
exportSceneName = name
startExport()
}
}
function basename(str) {
return (String(str).slice(String(str).lastIndexOf("/")+1))
}
}
Release mode
Surprisingly, in release mode, the dialog is open-type rather than save-type:
I have tried:
Change the QML imports versions
Clean the release build directory
Modify qtquickcontrols2.conf file
However, none of them worked! I have studied similar posts like this one, but suggestions didn't work. What else can I try? Thanks.
SOLUTION
Fixed by removing this import inside QML file:
import QtQuick.Dialogs 1.2
I'm going to guess that the issue is conflicting FileDialog definitions. Note that both imports QtQuick.Dialogs and Qt.labs.platform provide an object called FileDialog, but they do not use the same API. (There are several other objects like this in QML, and it's really annoying.) So it's probably trying to use one version of the dialog in debug mode, but for some reason choosing the other one in release mode.
The solution is to first of all make sure you remove any imports that you're not actually using. Then if you still need both, then you can label the imports:
import QtQuick.Dialogs 1.2 as QDiag
import Qt.labs.platform 1.0 as QPlat
Then when you create the FileDialog, you'll have to explicitly state which one you want to use.
QDiag.FileDialog {
}
QPlat.FileDialog {
}

Import .stories file into another .stories file with Storybook?

Can you import one .stories file into another .stories with Storybook?
Eg I have
/component1/component1.tsx
/component1/component1.stories.tsx
/component2/component2.tsx
/component2/component2.stories.tsx
I would like to also have a story for all of my components:
In /all-components/all-components.stories.tsx
import * as React from 'react';
import Component1Story from '../component1/component1.stories.tsx';
import Component2Story from '../component2/component2.stories.tsx';
export const Test = () => {
return (
<div>
<Component1Story />
<Component2Story />
</div>
);
};
export default {
title: 'Components',
};
I get this error:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of storyFn.
this should be doable as your stories are just React components. Your problem is happening because you're trying to import the default from your module, which is actually just an object:
export default {
title: 'Components',
};
All stories are named exports, and you should import them with destructuring:
import { Component1Story } from '../component1/component1.stories';
import { Component2Story } from '../component2/component2.stories';
I created an example for you which shows a working scenario here.
p.s. It's interesting to know that starting with Storybook 6 there's a new mechanism to simplify the creation and reuse of stories so stay tuned! It's called Args.

Problem with embedding QML into resource Qt application

I have some problem with embedding my QML component into resource of my application.
I have some gui.qml
import QtQuick 1.0
Rectangle {
width: 480
height: 525
color: "#ffff00"
}
My dirs
\main.cpp
\gui.qml
I tried run it:
view = new QDeclarativeView(m_GUI);
view->setSource(QUrl("qrc:/gui.qml"));
But i catch:
qrc:/gui.qml: File not found
What's wrong?
Thank all )
I solved it.
In resource i have "Prefix" for the root path.
view->setSource(QUrl("qrc:/MyPrefix/gui.qml"));

Resources