QML TypeError: Property 'xx' of object yy is not a function - qt

I have this main.qml:
import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.5
ApplicationWindow {
...
Item {
// This Item is to provide needed properties for functionality in the original app that was elided out in this example.
// It was left in in case it's relevant to the problem.
...
Column {
...
Text {
text: qsTr("Masked text")
SlidingMask {
id: testMask
anchors.fill: parent
...
}
}
Row {
Button {
id: btnRevealText
text: qsTr("Reveal")
...
}
Button {
id: btnHideText
text: qsTr("Hide")
...
}
}
}
}
Connections {
target: btnRevealText
onPressed: testMask.reveal()
}
Connections {
target: btnHideText
onPressed: testMask.hide()
}
}
And this SlidingMask.qml that's registered in the qml.qrc:
import QtQuick 2.0
Rectangle {
...
function hide() {
...
}
function reveal() {
...
}
}
When I run the app and try to press the buttons, I get the following errors:
TypeError: Property 'hide' of object SlidingMask_QMLTYPE_7(0x19991132c50) is not a function
TypeError: Property 'reveal' of object SlidingMask_QMLTYPE_7(0x19991132c50) is not a function
However, if I try changing the Connections to alter a property of the SlidingMask instead of calling a function, it works fine.
I've also tested this component previously and didn't run into any problems then, although I wasn't using Connections in that test.
I've searched here and on Google for an answer, but nothing I've found seems relevant to my situation. How would I fix this?

Here is a simple example which works properly:
//main.qml
ApplicationWindow {
id: window
width: 640
height: 480
visible: true
Column{
ItemWithFunction{
id: sc
width: 100
height: 100
}
Button{
id: btn1
text: 'Test Connection'
}
}
Connections{
target: btn1
onPressed: sc.testFunction();
}
}
//ItemWithFunction.qml
Rectangle{
color: 'red'
function testFunction(){
console.log("SOMETHING HAPPENED")
}
}
It seems that you are not putting your functions in SlidingMask root but in one of its child components.

Related

QML How to import ItemGrabResult?

I try to use ItemGrabResult {} element. But I got error
"QQmlApplicationEngine failed to load component"
"ItemGrabResult is not a type"
import QtQuick 2.15
Item {
id: root
ItemGrabResult {
}
Rectangle {
id: rect
color: "red"
anchors.fill: parent
}
ItemGrabResult is inactive
In documentation https://doc.qt.io/qt-5/qml-qtquick-itemgrabresult.html#details
I can not find any information how to import this element in CMake (or qmake) file
As iam_peter indicated, the ItemGrabResult comes from a call to Item.grabToImage().
In the following example, you need to click on the red Rectangle and it will initiate a grabToImage() call. The callback will have an instance of the ItemGrabResult which has properties and methods for accessing the grabbed image:
import QtQuick
import QtQuick.Controls
Page {
Item {
id: frame
width: 200
height: 200
Rectangle {
id: rect
color: "red"
anchors.fill: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
frame.grabToImage( function (itemGrabResult) {
// itemGrabResult: QQuickItemGrabResult(0x254f9a0)
// itemGrabResult.image: QVariant(QImage, QImage(QSize(200, 200),format=QImage::Format_RGBA8888_Premultiplied,depth=32,devicePixelRatio=1,bytesPerLine=800,sizeInBytes=160000))
// itemGrabResult.saveToFile: function() { [native code] }
dbg.text = `
itemGrabResult: ${itemGrabResult.toString()}
itemGrabResult.image: ${itemGrabResult.image.toString()}
itemGrabResult.saveToFile: ${itemGrabResult.saveToFile.toString()}
`
} );
}
}
}
footer: TextEdit { id: dbg; wrapMode: Text.WordWrap }
}
You can Try it Online!
https://doc.qt.io/qt-6/qml-qtquick-item.html#grabToImage-method

Load SwipeView pages dynamically

I have created the following MWE (Qt 5.13.0):
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
ApplicationWindow
{
property int itemsNo: 3;
id: window
visible: true
width: 480
height: 480
SwipeView
{
anchors.fill: parent;
id: theSwipeView;
Loader
{
sourceComponent: theSingleComp;
Component
{
id: theSingleComp;
Page
{
Text
{
text: "The single one";
}
}
}
}
Repeater
{
model: itemsNo;
Loader
{
sourceComponent: theMultiComp;
Component
{
id: theMultiComp;
Page
{
Text
{
text: "The multi one " +
(theSwipeView.currentIndex - 1);
}
}
}
}
}
}
}
In my program, I have an unique component (theSingleComp) and multiple components behind him (theMultiComp). As for now, I need to implement the following functionality:
In case the model used for theMultiComp has only 1 item, display only this item and not the theSingleComp. In case the are more theMultiComp items, display it like now. It seems to me that there is no possibility for this to work if I keep the items defined statically. But on the other hand, I don't know how to do this dynamically, since there is a case in which one of the components should not be displayed at all. I tried an approach like this:
sourceComponent: (itemsNo > 1) ? theSingleComp : null;
But then the page for this null component is still created.
Your problem is that Loader is an Item and SwipeView creates a page for it even if it doesn't have a source component.
To solve this problem you can use Repeater instead with a model of 1 (or 0 to disable it). Repeater is also an Item but it has some special code under the hood to be ignored by containers.
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
ApplicationWindow
{
id: window
property int itemsNo: 0
visible: true
width: 480
height: 480
SwipeView {
id: theSwipeView
anchors.fill: parent
Repeater {
model: window.itemsNo > 1 ? 1 : 0
Page {
Text {
text: "The single one"
}
}
}
Repeater {
model: window.itemsNo
Page {
Text {
text: "The multi one " + model.index
}
}
}
}
}
(I've simplified your code to remove the explicit Components and the Loaders)
I have come up with the following solution but I am not happy with it. It's very hacky and the user can see how the page index changes.
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
ApplicationWindow
{
property int itemsNo: 2;
id: window
visible: true
width: 480
height: 480
SwipeView
{
anchors.fill: parent;
id: theSwipeView;
Component.onCompleted:
{
if (itemsNo > 1)
insertItem(0, theSingleComp);
set0IndexTimer.start();
}
Timer
{
id: set0IndexTimer;
interval: 1;
running: false;
repeat: false;
onTriggered: theSwipeView.setCurrentIndex(0);
}
onCurrentIndexChanged: console.log("page: ", currentIndex);
Repeater
{
model: itemsNo;
Loader
{
sourceComponent: theMultiComp;
Component
{
id: theMultiComp;
Page
{
Text
{
text: "The multi one " + theSwipeView.currentIndex;
}
}
}
}
}
}
Item
{
id: theSingleComp;
Page
{
Text
{
text: "The single one";
}
}
}
}
I am still seeking some other examples.

How to implement single connection between objects in QML?

In my Qt app I have many windows, and sometimes they need a "Back" button. This button is placed on ToolBar component in the header of the ApplicationWindow .
What I want to achieve, is that this Back button, would have only single connection to other objects , i.e. the connection to the last object that called connect method. Right now with every connect I am getting a new connection and when the signal is emitted, it is called multiple times. Unfortunately Qt doesn'thave disconnectAll method, if it would , that would have solve my problem , I would just call disconnectAll before and then connect and that would implement single connection.
So , how are you doing this functionality in Qt , with a simple method?
Here is a minimal reproducible example, click on the tabs many times, then press 'Back' button and you will see lots of console.log messages. And what I need is this message to correspond to the last object that is connected to the Back button.
import QtQuick 2.11
import QtQuick.Controls 2.4
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Tabs")
signal back_btn_clicked()
SwipeView {
id: swipeView
anchors.fill: parent
currentIndex: tabBar.currentIndex
Page1Form {
id: page1
function page1_callback() {
console.log("page 1 back button triggered")
}
function install_button() {
enable_back_button(page1_callback)
}
}
Page2Form {
id: page2
function page2_callback() {
console.log("page 2 back button triggered")
}
function install_button() {
enable_back_button(page2_callback)
}
}
function install_back_button(idx) {
if (idx===0) {
page1.install_button()
}
if (idx===1) {
page2.install_button()
}
}
}
Button {
id: btn_back
visible: false
text: "Back Button"
onClicked: back_btn_clicked()
}
footer: TabBar {
id: tabBar
currentIndex: swipeView.currentIndex
TabButton {
text: qsTr("Page 1")
onClicked: swipeView.install_back_button(0)
}
TabButton {
text: qsTr("Page 2")
onClicked: swipeView.install_back_button(1)
}
}
function enable_back_button(func_name) {
btn_back.visible=true
back_btn_clicked.connect(func_name)
}
}
PageForm.ui is defined like this
import QtQuick 2.11
import QtQuick.Controls 2.4
Page {
width: 600
height: 400
header: Label {
text: qsTr("Page 1")
font.pixelSize: Qt.application.font.pixelSize * 2
padding: 10
}
Label {
text: qsTr("You are on Page 1.")
anchors.centerIn: parent
}
}
The simplest hack, I think, would be to store the callback in a property, then in enable_back_button(), reference that property in your disconnect() function, and update the property accordingly with the new callback passed as a function argument. (The rationale for this argument being that the disconnect() function must take in an argument: the slot to disconnect. So we'll need to keep track of it some way or another.)
ApplicationWindow {
visible: true
// ... omitted for brevity
property var prevCallback: null
// ... ofb
function enable_back_button(func_name) {
btn_back.visible=true
if (prevCallback)
back_btn_clicked.disconnect(prevCallback) // disconnect previous callback
back_btn_clicked.connect(func_name) // connect new callback
prevCallback = func_name // update property with new callback
}
}
And this could work on multiple connections as well, by simply changing the storage into an array, then iterating through that.

Qt.createComponent url of the library components

Below is a function from TimelinePresenter.qml which is a custom component I created.
function createMenu() {
var menuComp = Qt.createComponent("Menu.qml");
if( menuComp.status != Component.Ready )
{
if( menuComp.status == Component.Error )
console.debug("Error: " + menuComp.errorString());
return;
}
}
It gives the error:
Error: qrc:/qml/timeline/Menu.qml:-1 No such file or directory
TimelinePresenter.qml is a resource file specified in the .qrc file and its path is qml/timeline as shown in error message so qml engine is trying to find the Menu.qml there expectedly. How can I specify the path to create qt's Menu component?
Edit:
my resources.qrc file
<RCC>
<qresource prefix="/">
<file>qml/main_window.qml</file>
<file>qml/timeline/TimelineViewItem.qml</file>
<file>qml/timeline/HorizontalLine.qml</file>
<file>qml/timeline/TimelineView.qml</file>
<file>qml/timeline/VerticalLine.qml</file>
<file>qml/timeline/timeline-item/timeline_item.h</file>
<file>qml/timeline/TimelinePresenter.qml</file>
<file>qml/timeline/timeline-item/analog_timeline_item.h</file>
<file>qml/timeline/timeline-item/digital_timeline_item.h</file>
<file>qml/timeline/timeline_presenter_backend.h</file>
<file>qml/ControllableListPresenter.qml</file>
<file>qml/controllable_list_backend.h</file>
<file>qml/controllable-popup/AddControlUnitPopup.qml</file>
<file>qml/styled/CenteredPopup.qml</file>
<file>qml/styled/StyledTextField.qml</file>
</qresource>
</RCC>
You are confusing the creation of a component with the creation of an object that belongs to a component.
The Menu component already exists and is provided by Qt, what you must do is create the object using the Qt.createQmlObject() method.
Example:
var menuObj = Qt.createQmlObject('import QtQuick.Controls 2.0 ; Menu {
MenuItem { text: "Cut" }
MenuItem { text: "Copy" }
MenuItem { text: "Paste" } }', parentItem, "dynamicSnippet1");
Complete Example:
import QtQuick 2.7
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
id: parentItem
Component.onCompleted: {
var menu = Qt.createQmlObject('import QtQuick.Controls 2.0 ; Menu {
MenuItem { text: "Cut" }
MenuItem { text: "Copy" }
MenuItem { text: "Paste" }
}', parentItem,"dynamicSnippet1");
// test: open menu
menu.open()
}
}
In the case you have described in your comments, I would suggest to only create one Menu and only popup() it at the place where you have clicked, setting it in a specific context.
I prepared a small example to illustrate how the Menu could be used:
import QtQuick 2.7
import QtQuick.Window 2.0
import QtQuick.Controls 2.3 // Necessary for the "Action" I used. Create the Menu otherwise if you are bound to older versions.
import QtQml 2.0
ApplicationWindow {
id: window
visible: true
width: 600
height: 600
Repeater {
model: ListModel {
ListElement { color: 'black'; x: 400; y: 50 }
ListElement { color: 'black'; x: 100; y: 190 }
ListElement { color: 'black'; x: 70; y: 80 }
ListElement { color: 'black'; x: 30; y: 0 }
ListElement { color: 'black'; x: 340; y: 500 }
ListElement { color: 'black'; x: 210; y: 10 }
}
delegate: MouseArea {
x: model.x
y: model.y
width: 50
height: 50
property QtObject modelItem: model
onClicked: menu.openMenu(x + mouse.x, y + mouse.y, modelItem)
Rectangle {
color: model.color
anchors.fill: parent
}
}
}
Menu {
id: menu
Action { text: "green" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "blue" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "pink" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "yellow" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "orchid" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "orange" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "teal" ; onTriggered: { menu.currentContext.color = text } }
Action { text: "steelblue"; onTriggered: { menu.currentContext.color = text } }
property QtObject currentContext
function openMenu(x, y, context) {
currentContext = context
popup(x, y)
}
}
}
Though I think this answer might solve your problem, I know that it is not really the answer to the question you stated initially.
For the Component-part: I think you misunderstood what a Component is - it is not an Item. It is a prestage in the creation of QtObjects and more something like a prototype or configured factory.
So your function - if it would work - would end at the creation of a invisible thing, from which you could create objects, by calling createObject().
Creating Components is the right thing to do, if you want to create an object at a later time and you might want to create similar objects multiple times, either by JavaScript or by other QML-types that expect Components as some input (e.g. delegates).
To create Components you have multiple possibilities, e.g.:
Qt.createComponent(url)
Component { SomeItem {} }
The first expects you to know the url, which in your case, you do not. To circumvent that, the easiest solution is, to create a new File, like MyMenu.qml
that only contains the Menu {} - then you can create a Component from this.
The second does not expects you to know the url, but it is not dynamically created.
Component {
id: myCmp
Menu {
}
}
onSomeSignal: myCmp.createObject({ prop1: val1 }, this)
Here the Component is automatically created when the object in the file is instantiated. This makes that (one time) initially a bit slower, since more code has to be processed, but you don't have to do it later.
Creating objects like eyllanesc shows with Qt.createQmlObject("Write a new QML-File here") might be also used to create a Component if the top-level element is a Component. If you don't have a Component as top-level, it will also first create a component that is once used to create a QtObject and then is discarded. It is the slowest but most flexible way to dynamically create objects.

Create Objects of inner Component of Object of outer Component

Is it possible to do something like this:
import QtQuick 2.7
import QtQuick.Window 2.2
Window{
id: root_
visible: true
width: 300
height: 300
Component {
id:compouter
Column{
anchors.fill: parent
Component {
id: compinner
Rectangle {
width:parent.width
height:parent.height/2
}
}
}
}
Component.onCompleted: {
var c = compouter.createObject(this)
//var d = c.compinner.createObject(c, {"color": "green"})
//var e = c.compinner.createObject(c, {"color": "red"})
}
}
That is, I want to create multiple Objects inside the outer object (after the outer object was created). However this isn't possible, as I get the error:
TypeError: Cannot call method 'createObject' of undefined
Is there any workaround for this? Is it maybe only possible to instantiate all inner objects during the instantiation of the outer object?
The ids are out of the scope for your function call.
You can either add a function to your outer component, that creates your objects:
Component {
id:compouter
Column{
anchors.fill: parent
function createCompinner(arg) { compinner.createObject(this, arg) }
Component {
id: compinner
Rectangle {
width:parent.width
height:parent.height/2
}
}
}
}
or you expose the inner Component as a property:
Component {
id:compouter
Column{
property alias compinner: compinnerComponent
anchors.fill: parent
Component {
id: compinnerComponent
Rectangle {
width:parent.width
height:parent.height/2
}
}
}
}
and access it like that.
Ok, I found a workaround through sending a signal to the outter object:
import QtQuick 2.7
import QtQuick.Window 2.2
Window{
id: root_
visible: true
width: 300
height: 300
Component {
id:compouter
Column{
anchors.fill: parent
signal createRect(var color)
onCreateRect: {
compinner.createObject(this, {"color": color})
}
Component {
id: compinner
Rectangle {
width:parent.width
height:parent.height/2
}
}
}
}
Component.onCompleted: {
var c = compouter.createObject(this)
c.createRect("green")
c.createRect("red")
}
It's working, but maybe there is some more generic approach to this (see the accepted answer). As the poster suggested, calling a function would be semantically more clean than sending a signal

Resources