Want to connect QML types across the QML files in PyQt6 - qml

This is a simple example of what I want to do.
// main.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material
ApplicationWindow {
visible: true
width: 640
height: 480
title: "Example App"
Material.theme: Material.Dark
MyLabel {
id: idMyLabel
anchors.centerIn: parent
}
MyButton {
anchors.top: idMyLabel.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
}
// MyLabel.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material
RowLayout {
spacing: 40
Label {
id: idInnerLabel1
text: "Label 1"
}
Label {
id: idInnerLabel2
text: "Label 2"
}
Label {
id: idInnerLabel3
text: "Label 3"
}
}
// MyButton.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material
Page {
Button {
text: "Change"
onClicked: idInnerLabel2.text = "Changed"
}
}
But, I got an error when I run this example.
ReferenceError: idInnerLabel2 is not defined
I wanted to communicate between nested QML files. Putting them together in one file worked fine. So I was confused and I couldn't understand its inner working.
How can I resolve this issue?

Since both MyButton and MyLabel appear in main.qml the simplest solution would be to create a property in main.qml that both can access.
import QtQuick
import QtQuick.Controls
Page {
property string txt1: "Label 1"
property string txt2: "Label 2"
property string txt3: "Label 3"
MyLabel {
id: idMyLabel
anchors.centerIn: parent
}
MyButton {
anchors.top: idMyLabel.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
}
// MyLabel.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material
RowLayout {
spacing: 40
Label {
text: txt1
}
Label {
text: txt2
}
Label {
text: txt3
}
}
// MyButton.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material
Page {
Button {
text: "Change"
onClicked: txt2 = "Changed"
}
}
You can Try it Online!

Related

How to add link icon to title area in react native component?

There is a Event component in my RN (0.59.9) app. Here is the constructor part of the code:
import React, { Component} from 'react';
import { SectionList, View, Image, StyleSheet, Text, TouchableOpacity, Platform, TouchableHighlight, AppRegistry } from 'react-native';
import {Icon } from "react-native-elements";
import DeviceInfo from 'react-native-device-info';
import helper from "../../lib/helper";
import GLOBAL from "../../lib/global";
export default class Event extends React.Component {
static navigationOptions = {
title: 'Events',
};
constructor(props) {
super(props);
this._isMounted = true;
console.log("props in Event : ", this.props);
this.state = {
activeEvents: [],
user: this.props.myself,
token: this.props.token,
group_id: this.props.group_id,
};
this._onPress = this._onPress.bind(this);
};
.......
I would like to insert a icon link into title area to the right. The icon link links to another component called Group with react navigation.
Here is how to add a button in title:
https://reactnavigation.org/docs/en/header-buttons.html

Attribute selectors in emotion

The following code customizes the google maps DrawingManager crosshair cursor. It works but I would like to be able to pass an svg cursor as a prop. I was trying to get it working using emotion but couldn't find anything similar in the emotion docs and my attempts failed. Can it be done in emotion?
Code: There are really only two relevant lines here (indicated) - the rest is given for context.
import React, { useEffect, useContext } from 'react'
import { GoogleMapContext, MapBox } from '#googlemap-react/core'
import ThemeSwitcher from './theme-switcher'
import styles from './styles/styles'
import './map.css' <--------------------------------------------------
export default function Map(props) {
const { state: { map } } = useContext(GoogleMapContext);
useEffect(() => map && map.setOptions({ draggableCursor: props.cursor }));
return <>
<MapBox
className='crosshair-cursor' <---------------------
apiKey={process.env.REACT_APP_GMAPS}
opts={{
center: { lat: -37.815018, lng: 144.946014 },
zoom: 11,
styles: styles[localStorage.getItem('mapTheme')],
}}
useDrawing
useGeometry
useGoogleApi
onClick={props.onClick}
onRightClick={props.onRightClick}
LoadingComponent={null}
/>
<ThemeSwitcher bindingPosition='TOP_LEFT' map={map} />
{props.children}
</>
}
map.css:
.crosshair-cursor [style*='crosshair'] {
cursor: url("https://i.stack.imgur.com/mA4e2.jpg?s=48&g=1")24 24, crosshair !important;
}
My attempt:
import React, { useEffect, useContext } from 'react'
import { GoogleMapContext, MapBox } from '#googlemap-react/core'
import ThemeSwitcher from './theme-switcher'
import styles from './styles/styles'
import './map.css'
import {css} from '#emotion/core'
const cursor = css`[style*='crosshair'] {
cursor: url("https://i.stack.imgur.com/mA4e2.jpg?s=48&g=1")24 24, crosshair !important;
}`
export default function Map(props) {
const { state: { map } } = useContext(GoogleMapContext);
useEffect(() => map && map.setOptions({ draggableCursor: props.cursor }));
return <>
<MapBox
// className='crosshair-cursor'
css={cursor}
apiKey={process.env.REACT_APP_GMAPS}
opts={{
center: { lat: -37.815018, lng: 144.946014 },
zoom: 11,
styles: styles[localStorage.getItem('mapTheme')],
}}
useDrawing
useGeometry
useGoogleApi
onClick={props.onClick}
onRightClick={props.onRightClick}
LoadingComponent={null}
/>
<ThemeSwitcher bindingPosition='TOP_LEFT' map={map} />
{props.children}
</>
}

How to create an object in react native?

I just started learning react native and I am having issues with creating an object. Here is the code
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
} from 'react-native';
export default class object {
name: 'joe'
age: 27
country: 'France'
}
And when I try to instantiate it in another class like this
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
} from 'react-native';
import object from './object.js'
export default class MyComponent extends Component {
var man = new Object();
render() {
return (
<View style={styles.container}>
<Text>{man.age}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
})
I get a syntax error Unexpected token var ->man = new Object(). How do I fix this?
Try to create your object in typescript file(object.ts) for exemple:
export class object {
name: 'joe',
age: 27,
country: 'France'
constructor(obj) {
this.name=obj.key;
this.age=obj.nom;
this.country=obj.prenom;
}
}
the wrong part is you export default class object, FYI: CLASS is not OBJECT
the correct using of export is
export const object = {
name: 'joe',
age: 27,
country: 'France'
}
then you can import it with
import {object} from './object.js'

Get anchors to fill remaining area in the parent

I have a QML component called MyComponent and I have the following instantiations:
MyComponent {
id: rightComponent
SettingsScreenHeader {
id: settingsHeader
}
SettingsScreen {
id: settingsScreen
}
StackView {
id: settingsStack
anchors.fill: parent
initialItem: settingsScreen
}
}
SettingsScreenHeader and SettingsScreen are two other components. At the moment when I do anchors.fill: parent in the StackView object, it takes all the space and the header object gets obscured. Is there a way to tell it to fill all the remaining space in the parent?
Try using ColumnLayout:
MyComponent {
id: rightComponent
ColumnLayout {
anchors.fill: parent
SettingsScreenHeader {
id: settingsHeader
Layout.fillWidth: true
}
SettingsScreen {
id: settingsScreen
Layout.fillWidth: true
}
StackView {
id: settingsStack
Layout.fillHeight: true
Layout.fillWidth: true
initialItem: settingsScreen
}
}
}
See the documentation for further details.

QML StatusBar in the upper portion of ApplicationWindow - why?

I have following QML's ApplicationWindow:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtMultimedia 5.0
import QtQuick.Layouts 1.0
import QtTest 1.1
import "gui/windows"
import "gui/items"
ApplicationWindow
{
id: ueWindowMain
title: qsTr("testApp")
width: Screen.desktopAvailableWidth
height: Screen.desktopAvailableWidth
visible: true
opacity: 1.0
contentOrientation: Qt.LandscapeOrientation
color: "black"
UeKeypad
{
id: ueLoginKeypad
} // ueLoginKeypad
StatusBar
{
id: ueStatusBar
RowLayout
{
spacing: 8
UeStatusIndicator
{
id: ueStatusIndicatorDatabaseConnected
ueParamImageStatusOn: "qrc:///ueIcons/icons/ueDbConnectionOk.png"
ueParamImageStatusOff: "qrc:///ueIcons/icons/ueDbConnectionError.png"
} // ueStatusIndicatorDatabaseConnected
} // RowLayout
} // ueStatusBar
} // ueWindowMain
And here is screenshot:
As you can see, I've added QML StatusBar into QML ApplicationWindow, but StatusBar is not positioned in the lower part of ApplicationWindow, but in upper. Why?
This may give you a hint:
ApplicationWindow {
statusBar: StatusBar {
RowLayout {
anchors.fill: parent
Label { text: "Read Only" }
}
}
}