Declare specific global objects in QML - qt

I am trying to follow this answer, but for more complicated objects:
https://stackoverflow.com/a/25123824/1036082
My files are:
qml.qrc:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>Uconsts.qml</file>
<file>Ucolors.qml</file>
<file>qmldir</file>
</qresource>
</RCC>
main.qml:
import QtQuick 2.9
import QtQuick.Window 2.2
import "."
Window
{
width: 600
height: 400
visible: true
color: Uconsts.colDay.canvas;
}
qmldir:
# qmldir
singleton Uconsts Uconsts.qml
Ucolors.qml:
import QtQuick 2.0
Item
{
property color canvas: "#FFFFFF";
}
Uconsts.qml:
pragma Singleton
import QtQuick 2.9
QtObject
{
property Ucolors colDay:
{
canvas: "#eaedf1";
}
}
When running the program, in runtime I get the following errors from the QML side:
qrc:/main.qml:10: TypeError: Cannot read property 'canvas' of null
qrc:/Uconsts.qml:6:2: Unable to assign QString to Ucolors_QMLTYPE_0*
What am I doing wrong here?

QML does not deduct the type when you use {}, you must create the item explicitly
pragma Singleton
import QtQuick 2.9
QtObject
{
property Ucolors colDay: Ucolors{ canvas: "#eaedf1";}
}

Related

Is it possible to add a Uv cube mapping to a CuboidMesh in QML?

I am developping a 3D cube with 6 different textures and I wanted to know if it's possible to use a cube Uv mapping for a CuboidMesh in QML.
If it's possible, how can I do it ?
I try it but I have the following result :
Here is my code for all the scene including the cube :
import Qt3D.Core 2.12
import Qt3D.Render 2.12
import Qt3D.Extras 2.12
import Qt3D.Input 2.12
import QtQuick 2.12
import QtQuick.Scene3D 2.12
import QtQuick.Window 2.12
import QtQuick 2.15
Entity {
id: root
Entity {
id: cubeEntity
Texture2D {
id: cubeTexture
TextureImage {
source: "qrc:/texture.png"
}
}
CuboidMesh {
id: cubeMesh
xExtent: 1
yExtent: 1
zExtent: 1
}
Transform {
id: cubeTransform
}
ObjectPicker {
id: cubePicker
}
NormalDiffuseMapMaterial{
id: cubeMaterial
diffuse: cubeTexture
normal: cubeTexture
specular: "black"
shininess: 50
}
components: [cubeMesh, cubeTransform, cubeMaterial, cubePicker]
}
}
It works with the Mesh type but I want to use that mapping with a CuboidMesh.
My texture.png is the following image :

QML nextItemInFocusChain() not working, always refers to self

Calling nextItemInFocusChain() from a child QML component seems to always return the current component. Understandably, calling forceActiveFocus() on that next item does nothing. Note that navigation via the tab key still works as expected.
Minimally Reproducible Example:
qml.qrc
<RCC>
<qresource prefix="/qml">
<file>main.qml</file>
<file>Child.qml</file>
</qresource>
</RCC>
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/qml/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
} // main
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.12
ApplicationWindow {
width: 800
height: 600
visible: true
visibility: "FullScreen"
title: "HelloWorld"
RowLayout {
spacing: 30
width: 600
Child {
Layout.fillWidth: true
id: test1
}
Child {
Layout.fillWidth: true
id: test2
}
Child {
Layout.fillWidth: true
id: tes3
}
}
onActiveFocusItemChanged: print("activeFocusItem: ", activeFocusItem)
}
child.qml
import QtQuick 2.12
import QtQuick.Controls 2.15
Item {
function focusNext()
{
console.log("focusing next...")
nextItemInFocusChain().forceActiveFocus()
}
Keys.onReturnPressed: focusNext()
TextField {
text: "TEST"
}
}
Putting the logic directly in the TextField results in the correct behavior:
TextField {
...
Keys.onReturnPressed: nextItemInFocusChain().forceActiveFocus()
}
It's unclear to me why the parent item cannot redirect focus in this way.

Communication between 2 different objects in 2 different QML files

I have one main.qml file and I have other "example.qml" file. When I push a button from "example.qml" file I want to change a text in "main.qml" file. I tried defining the source of the text. I tried send signal. I tried using loader but always came to a dead end.
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Imagine 2.12
import QtQuick.Window 2.0
//main.qml
Window {
visible: true
width: 1080
height: 720
color: "black"
title: qsTr("MY GUI")
Text {
id: deneme
x: 100
y: 400
color: "white"
text: "Trial"
}
}
//example.qml
Item {
id: difflock
Rectangle{
id: diffLockRect
width: 1080
height: 720
color: "red"
signal mySignal
Button{.
onClicked: main.deneme.text = "Finally"
}
}
}
Create a new qml named by Example.qml(first letter should be capital)
Define in main.qml
Example.qml can access the objects in main.qml
main.qml
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Imagine 2.12
import QtQuick.Window 2.0
Window {
visible: true
width: 1080
height: 720
color: "black"
title: qsTr("MY GUI")
Example{id:rfrnc} // You can also reach the other qml objects by using this id
Text {
id: deneme
x: 100
y: 400
color: "white"
text: "Trial"
}
}
Example.qml
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Imagine 2.12
import QtQuick.Window 2.0
Item {
id: difflock
Rectangle{
id: diffLockRect
width: 1080
height: 720
color: "red"
signal mySignal
Button{
onClicked: deneme.text = "Finally"
}
}
}

ASP.NET Core React-with-Redux template dispatch issue

I am working on this project where we're using this template from MS and it's all working nice but for this particular task I am in a need for 'default' dispatch method from redux so I can focus on some form inputs and I cannot figure out how to get it.
import { ApplicationState } from '../../../store/index';
import { connect } from 'react-redux';
import * as GlobalState from '../../../store/Global';
import { Field } from 'redux-form';
import SelectHour from '../Selects/SelectHour';
import Select from '../Selects/Select';
import MultiSelectComponent from '../Selects/MultiSelectComponent';
import Days from '../Days';
import { getFormInitialValues } from 'redux-form';
import { Field, focus, blur } from 'redux-form';
import SelectHour from './SelectHour';
class WorkingDays extends React.Component<Props, State> {
...
}
export default connect(
(state: ApplicationState) => state.global,
GlobalState.actionCreators
)(WorkingDays) as typeof WorkingDays;
How can I get access to dispatch so I can use it something like this?
this.props.dispatch.focus(something, something)
dispatch is a function, so you can't make this.props.dispatch.focus(something, something) work.
But can get this.props.focus(something, something) by this way:
export default connect(
(state: ApplicationState) => state.global,
(dispath) => ({
focus: (value1, value2) => {
const action = yourAction(value1, value2)
dispath(action)
}
})
)(WorkingDays) as typeof WorkingDays;
I have never worked with MS react-redux template so I do not know what your GlobalState is look like!

upload mesh QML

I'am new to Qt and I am trying to upload a mesh (.obj) in my QML file but it does not work. no error and no results.
how can I make it work.
import QtQuick 2.9
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Logic 2.0
import Qt3D.Extras 2.0
import Qt3D.Animation 2.9
import QtQuick.Window 2.2
Viewport{
id:viewroot
normalizedRect: Qt.rect(0, 0, 1, 1)
Entity {
id: sceneRoot
Mesh{
id:carmesh
source:"Bibliothèques\Documents\mercedes.obj"
}
}
}

Resources