Undefined reference on a binding to id, Scoping rules - qt

I am trying to pass around an object required by some qml components. The problem is that I cannot reference this object without it saying its undefined.
Item {
id: root
property alias db: database // this works for some reason
Database {
id: database
Component.onCompleted: {
connect("sqlite3", "dbname=database.db")
deleteTables();
createTables();
}
}
ContactImageProvider {
id: contactImageProvider
database: db // this works for some reason
database: root.database // This would be undefined
database: database // no loop detected but still null
}
}
So I am very very confused how I am supposed to be able to pass dependencies down though objects without the most confusing naming scheme ever. This could be anything BTW. Maybe I need a color in some control but want to pass it to another control as well. The whole scope name resolution documentation is very unclear. I can't make heads or tails what I'm supposed to be able to do.
Item {
id: root
property Database database: Database {
id: database // if no id then this doesnt work
Component.onCompleted: {
connect("sqlite3", "dbname=database.db")
deleteTables();
createTables();
}
}
property alias db: database
ContactImageProvider {
id: contactImageProvider
database: db // this doesn't work. WHY? idk
database: database // works if id is on
}
}
Also aliasing doesn't seem to work if I make it a property.

To any poor soul after me, it seems that this has to do with the differences between how property names and id names are resolved. What I ended up with was this.
Item {
id: root
property Database database: Database {
// notice no id
Component.onCompleted: {
connect("sqlite3", "dbname=database.db")
deleteTables();
createTables();
}
}
ContactImageProvider {
id: contactImageProvider
database: root.database // I can explicitly get a property
}
}

Related

How to get the value of a Firestore security rules array-contains constraint for LIST type query?

I am making a query like db.collection('accounts').where('access.users', 'array-contains', [ user1.ref ])
user1.ref is a DocumentReference to a document in the users collection that enables this user to read this account and its child collections, like an ACL
In the Firebase security rules emulator, if I debug the property resource.data.access.users I can see that is type constraint_value. And I can see the value for the array I passed:
constraint_value {
simple_constraints {
comparator: LIST_CONTAINS
value {
path_value {
segments {
simple: "databases"
}
segments {
simple: "(default)"
}
segments {
simple: "documents"
}
segments {
simple: "users"
}
segments {
simple: "account-1"
}
}
}
}
}
But how can I get the actual LIST value (in this case a LIST of of user references) of this constraint_value in the security rules so that I can check permissions using a LIST method like hasOnly() or hasAll()
There is no value property or value() method and in fact constraint_value isn't any type of interface in the docs ....

Qt quick how to connect signal and slots from different file

I'm new to QML.
I want to connect a signal from a .qml file to a slot in another .qml.
The problem is that I haven't instantiate the object in that file because I want to use a StackView in the main.qml to see on the screen the objects in that .qml files.
in main.qml I have:
StackView {
id: mStackId
anchors {
top: topSectionId.bottom
bottom: parent.bottom
left: parent.left
right: parent.right
}
initialItem: homePage
}
Component {
id: homePage
Home {id: homePageObj}
}
Component {
id: housePage
House {id: housePageObj}
}
Component {
id: createRoomPage
CreateRoom {id: createRoomPageObj}
}
where Home, House and CreateRoom are the .qml files I want to make to connect.
I've tried:
Component.onCompleted:
createRoomPage.newRoom.connect(homePage.createNewRoom)
where newRoom is the signal and createNewRoom is the slot, but Qt Creator gives me the error:
TypeError: Cannot call method 'connect' of undefined
I've also tried to use createRoomPageObj instead of createRoomPage and homePageObj instead of homePage but I get the error:
ReferenceError: createRoomPageObj is not defined
I've read I could use Connections, but documentation is not too clear to me.
This is not a matter of wrong Connections usage, the createRoomPage simply is not instantiated. You need a backend which can do the logic and to which you can connect from the views.
In the simplest form, you can make a QtObject
QtObject {
id: backend
signal newRoom(name)
}
StackView {
...
}
You can use backend.newRoom("Kitchen") from the CreateRoomPage.
EDIT after question about persistence
You can also create the QObject as a singleton:
pragma Singleton
import QtQuick 2.4
QtObject {
signal newRoom(name)
}
In this case the filename will determine the name to use in the rest of the program. So if naming above piece of code Backend.qml, you can use as follows:
Connections {
target: Backend
function onNewRoom(name) { .... }
}

How can I directly the name of a value inside a QML enum?

Our automated tests have a method which relies on unique strings for UI elements. These strings are maintained inside an enum:
object.qml/
QtObject {
enum Panel1 {
panel1Button,
}
enum Panel2{
panel2Button,
}
}
The problem is that when referring to these enum values, there is some ambiguity:
var objectname: object.Panel1.panel1button
and
var objectname2: object.Panel1.panel2button
will both return '0'
Is there a way I can return the name of the enum value? QML does not seem to natively support methods like JSON.Stringify or .name()

Realm Swift updating objects not working

I'm using realm swift v1.0.0 and I create an object and I want to update its value permanently.
Following the official guide and some other answers on stackoverflow I've done:
let myobject = MyObjectClass()
myobject.property1 = "test"
try! myrealm.write {
myrealm.add(myobject)
}
And myrealm is a global variable:
let myrealm = try! Realm()
When I recover all the values of MyObjectClass I get back myobject with some initial values set in the init of the class (so the values that I set as initial values are stored properly), but property1 is (null) (as it is declared as an optional string).
But if I look at myobject in the function where I write the code above, its property1 is set to "test", but it's not saved permanently in the Realm db.
Can anyone help me to find out why it doesn't update the value of property1?
I've also tried to do:
try! myrealm.write {
myrealm.add(myobject, update: true)
}
EDIT:
This is my model:
public class MyObjectClass(): Object {
//[...]
public dynamic var property1: String? = nil
public dynamic var property2: MyObjectClass2?
}
Same problem happens with property2 which is another Realm Object.

QML On Item Changed Signal

MyObject
Item {
property int current: 0
}
Can this be configured to emit a signal such that the following works?
Item {
property variant myObj: MyObject {}
onMyObjChanged: doThis()
...
}
cmannet85 has answered your question: it's not possible. Perhaps you could post more code so we can suggest alternative approaches.
In terms of a solution using the information you've provided, you should expose signals that client code should connect to in order to know when the object has changed. Since you said the current property is all that matters, and it already has a change signal, you can use Connections:
Connections {
target: myObj
onCurrentChanged: doThis()
}
Or connect to the signal manually:
Component.onCompleted: {
myObj.onCurrentChanged.connect(doThis);
}
function doThis() {
// ...
}

Resources