首页 \ 问答 \ 如何从另一个QML访问和控制ListModel的内容(How to access and control the content of a ListModel from another QML)

如何从另一个QML访问和控制ListModel的内容(How to access and control the content of a ListModel from another QML)

我在这里问过类似的问题。 但是这个解决方案只适用于一页QML文件。 我构建了一个包含许多页面的QML应用程序,因此我制作了一个包含添加/删除操作的收藏夹。 在收藏标签页中有一个列表模型。 在我看来,为了将任何页面添加到收藏夹列表中,需要从示例页面调用收藏夹列表模型,因此当用户单击该动作时,该页面应作为列表项目添加到收藏夹页面。 我有两个例子。 第一个例子是一页QML,它添加/删除列表项。 但第二个示例不会将示例页面添加/移除到收藏夹列表页面。 如何使用QML或Java Script访问,保存和恢复列表模型的内容? 谢谢。

第一个例子:

在这里输入图像描述

main.qml

import QtQuick 2.6
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.0
import Qt.labs.settings 1.0
ApplicationWindow{
    id:main
    width: 640
    height: 480
    visible:true
    title: "Menu"
    property string datastore: ""
    property int countt2: 0
    Settings{
        property alias datastore: main.datastore
        property alias mycount: main.countt2
    }
    menuBar:MenuBar {
        Menu{
        title: main.title
            Action {
                id:action2
                text: qsTr("On/Off")
                onTriggered:{
                    countt2++
                    console.log("triggered works.Count/2: "+ countt2%2)
                    if(countt2%2==1){
                        console.log("it must be added")
                        dataModel.append({ "title": "Application Tools" })
                    }
                    else if(countt2%2==0){
                        console.log("list must be removed. count/2: "+countt2%2)
                        return dataModel.remove(dataModel.index)
                    }
                }
            }
        }
    }
    Component.onCompleted: {
        if(datastore){
            dataModel.clear()
            var datamodel=JSON.parse(datastore)
            for (var i=0; i<datamodel.length; ++i) dataModel.append(datamodel[i])
        }
        console.log("onCompleted works right now.")
    }
    onClosing: {
        var datamodel = []
        for (var i=0;i<dataModel.count; ++i) datamodel.push(dataModel.get(i))
        datastore=JSON.stringify(datamodel)
        console.log("datastore: "+datastore)
    }
        ListView {
            id:malist
            width: parent.width
            height: parent.height
            focus: true
            interactive: true
            clip: true
            model:FavModel{
            id:dataModel
            }
            delegate: ItemDelegate {
                width: parent.width
                text: model.title
            }
        }
}

FavModel.qml:

import QtQuick 2.0
ListModel {
    id:dataModel
    }

第二个例子:

在这里输入图像描述

在这里输入图像描述

main.qml:

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.0
import QtQuick.Layouts 1.3
import Fluid.Controls 1.0
import QtQuick.Window 2.3
ApplicationWindow{
    id:main
    width: 640
    height: 480
    visible:true
    title: "Example App"
    initialPage:TabbedPage {
        title: main.title
        Tab{
            title:"APPS"
        ListView {
            id:malist
            width: parent.width
            height: parent.height
            focus: true
            interactive: true
            clip: true
            model:ListModel {
            id:appModel
            ListElement { title: qsTr("Page1"); source: "qrc:/SampePage.qml" }
            }
            delegate: ListItem {
                text: model.title
                onClicked: pageStack.push(model.source)
            }
        }
        }
        Favourites{}
    }
}

Favourites.qml:

import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import Fluid.Controls 1.0
Tab{
    title:"FAVORITES"
ListView {
    id:favorites
    width: parent.width
    height: parent.height
    focus: true
    interactive: true
    clip: true
    model:FavModel {
    id:favModel
    }
    delegate: ListItem {
        text: model.title
        onClicked: pageStack.push(model.source)
    }
}
}

FavModel.qml:

import QtQuick 2.0

ListModel {
    id:dataModel
    }

SamplePage.qml:

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import Fluid.Controls 1.0
import Qt.labs.settings 1.0
Page{
    title:qsTr("Page1")
    appBar.maxActionCount: 2
    id:sampleapp
    property string datastore: ""
    property int countt2: 0
    Settings{
        id:mysetting4
        property alias datastore: sampleapp.datastore
        property alias mycount: sampleapp.countt2
    }
    FavModel{
        id:dataModel
    }
    Component.onCompleted: {
        console.log("onCompleted works right now.")
        if(datastore){
            dataModel.clear()
            var datamodel = JSON.parse(datastore)
            for (var i=0; i<datamodel.length; ++i) dataModel.append(datamodel[i])
        }
    }
    onCanGoBackChanged: {
        var datamodel=[]
        for (var i=0; i<dataModel.count; ++i) datamodel.push(dataModel.get(i))
        datamodel = JSON.stringify(datamodel)
        console.log("datastore: "+datastore)
    }
    actions: [
        Action {
          id:favourite2
            onTriggered:{
                countt2++
                console.log("Count/2: "+ countt2%2)
                if(countt2%2==1){
                    console.log("List must be added")
                    dataModel.append({ "title": "Application Tools", "source": "qrc:/SampePage.qml" })
                }
                else if(countt2%2==0){
                    console.log("List must be removed.")
                    return dataModel.remove(dataModel.index)
                }
            }
            icon.name: "toggle/star"
            toolTip: qsTr("Add/Remove")
        }
    ]
}

*在第二个示例中,我无法仅使用Qt Quick Controls 2.0示例,因为使用列表菜单非常简单,所以必须使用流体qml库。


I had asked a similar question here. But this solution worked only for one-paged QML file. I build a QML app that contains many pages, so I made a favorite tab that contains Add/Remove action. There is a list model in favorite tab page. In my opinion, in order to add any page to the favorite list, it is necessary to call the favorite list model from the sample page, so when a user clicked the action, the page should be added to the favorite page as a list item. I have two examples. First example is one-paged QML and it adds/removes list item. But second example doesn't adds/removes the sample page to favorite list page. How can I access, save and restore the content of the list model using QML or Java Script? Thanks.

First example:

enter image description here

main.qml

import QtQuick 2.6
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.0
import Qt.labs.settings 1.0
ApplicationWindow{
    id:main
    width: 640
    height: 480
    visible:true
    title: "Menu"
    property string datastore: ""
    property int countt2: 0
    Settings{
        property alias datastore: main.datastore
        property alias mycount: main.countt2
    }
    menuBar:MenuBar {
        Menu{
        title: main.title
            Action {
                id:action2
                text: qsTr("On/Off")
                onTriggered:{
                    countt2++
                    console.log("triggered works.Count/2: "+ countt2%2)
                    if(countt2%2==1){
                        console.log("it must be added")
                        dataModel.append({ "title": "Application Tools" })
                    }
                    else if(countt2%2==0){
                        console.log("list must be removed. count/2: "+countt2%2)
                        return dataModel.remove(dataModel.index)
                    }
                }
            }
        }
    }
    Component.onCompleted: {
        if(datastore){
            dataModel.clear()
            var datamodel=JSON.parse(datastore)
            for (var i=0; i<datamodel.length; ++i) dataModel.append(datamodel[i])
        }
        console.log("onCompleted works right now.")
    }
    onClosing: {
        var datamodel = []
        for (var i=0;i<dataModel.count; ++i) datamodel.push(dataModel.get(i))
        datastore=JSON.stringify(datamodel)
        console.log("datastore: "+datastore)
    }
        ListView {
            id:malist
            width: parent.width
            height: parent.height
            focus: true
            interactive: true
            clip: true
            model:FavModel{
            id:dataModel
            }
            delegate: ItemDelegate {
                width: parent.width
                text: model.title
            }
        }
}

FavModel.qml:

import QtQuick 2.0
ListModel {
    id:dataModel
    }

Second Example:

enter image description here

enter image description here

main.qml:

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.0
import QtQuick.Layouts 1.3
import Fluid.Controls 1.0
import QtQuick.Window 2.3
ApplicationWindow{
    id:main
    width: 640
    height: 480
    visible:true
    title: "Example App"
    initialPage:TabbedPage {
        title: main.title
        Tab{
            title:"APPS"
        ListView {
            id:malist
            width: parent.width
            height: parent.height
            focus: true
            interactive: true
            clip: true
            model:ListModel {
            id:appModel
            ListElement { title: qsTr("Page1"); source: "qrc:/SampePage.qml" }
            }
            delegate: ListItem {
                text: model.title
                onClicked: pageStack.push(model.source)
            }
        }
        }
        Favourites{}
    }
}

Favourites.qml:

import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import Fluid.Controls 1.0
Tab{
    title:"FAVORITES"
ListView {
    id:favorites
    width: parent.width
    height: parent.height
    focus: true
    interactive: true
    clip: true
    model:FavModel {
    id:favModel
    }
    delegate: ListItem {
        text: model.title
        onClicked: pageStack.push(model.source)
    }
}
}

FavModel.qml:

import QtQuick 2.0

ListModel {
    id:dataModel
    }

SamplePage.qml:

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import Fluid.Controls 1.0
import Qt.labs.settings 1.0
Page{
    title:qsTr("Page1")
    appBar.maxActionCount: 2
    id:sampleapp
    property string datastore: ""
    property int countt2: 0
    Settings{
        id:mysetting4
        property alias datastore: sampleapp.datastore
        property alias mycount: sampleapp.countt2
    }
    FavModel{
        id:dataModel
    }
    Component.onCompleted: {
        console.log("onCompleted works right now.")
        if(datastore){
            dataModel.clear()
            var datamodel = JSON.parse(datastore)
            for (var i=0; i<datamodel.length; ++i) dataModel.append(datamodel[i])
        }
    }
    onCanGoBackChanged: {
        var datamodel=[]
        for (var i=0; i<dataModel.count; ++i) datamodel.push(dataModel.get(i))
        datamodel = JSON.stringify(datamodel)
        console.log("datastore: "+datastore)
    }
    actions: [
        Action {
          id:favourite2
            onTriggered:{
                countt2++
                console.log("Count/2: "+ countt2%2)
                if(countt2%2==1){
                    console.log("List must be added")
                    dataModel.append({ "title": "Application Tools", "source": "qrc:/SampePage.qml" })
                }
                else if(countt2%2==0){
                    console.log("List must be removed.")
                    return dataModel.remove(dataModel.index)
                }
            }
            icon.name: "toggle/star"
            toolTip: qsTr("Add/Remove")
        }
    ]
}

*I couldn't make Qt Quick Controls 2.0 only example in second example, I had to use fluid qml library because it is simple to make list menu.


原文:https://stackoverflow.com/questions/48891034
更新时间:2024-02-03 18:02

最满意答案

在某些内核版本中,可以通过更改sys_call_table来添加或修改系统调用。 但是因为这个表不打算在运行时更改,所以它没有保护。 更换桌子将导致比赛条件。 即使没有竞争条件,也存在与在使用或堆叠时移除模块有关的问题。 由于从模块更改sys_call_table出现问题,因此不再在新内核中导出符号。 换句话说,如果在尝试加载模块时得到"unresolved symbol sys_call_table" ,则表示模块中存在错误,内核不再接受此类错误模块。


In some kernel versions it is possible to add or modify a systemcall by changing the sys_call_table. But because this table is not intended to be changed at runtime, it has no protection. Changing the table will lead to race conditions. Even without the race conditions, there are problems related to removing the modules while they are in use or stacked. Because of the problems with changing sys_call_table from modules, the symbol is no longer exported in new kernels. In other words if you get "unresolved symbol sys_call_table" when trying to load a module, it means there is a bug in the module, and the kernel does no longer accept such buggy modules.

相关问答

更多
  • 在某些内核版本中,可以通过更改sys_call_table来添加或修改系统调用。 但是因为这个表不打算在运行时更改,所以它没有保护。 更换桌子将导致比赛条件。 即使没有竞争条件,也存在与在使用或堆叠时移除模块有关的问题。 由于从模块更改sys_call_table出现问题,因此不再在新内核中导出符号。 换句话说,如果在尝试加载模块时得到"unresolved symbol sys_call_table" ,则表示模块中存在错误,内核不再接受此类错误模块。 In some kernel versions it ...
  • globals()返回当前模块的字典,因此可以像添加任何其他字典一样向它添加项目。 尝试: for letter in ['a', ..., 'z']: globals()[letter] = letter 或者消除对globals()的重复调用: global_dict = globals() for letter in ['a', ..., 'z']: global_dict[letter] = letter 甚至: globals().update((l,l) for l in [ ...
  • 如果你有一个符号链接 ,例如 $ ls -l /vmlinuz lrwxrwxrwx 1 root root 30 2009-08-03 08:59 /vmlinuz -> boot/vmlinuz-2.6.28-15-generic 然后readlink系统调用将获得符号链接目标( boot/vmlinuz-2.6.28-15-generic ),就像readlink命令一样: $ readlink /vmlinuz boot/vmlinuz-2.6.28-15-generic If you have ...
  • 对于未来的读者: 一般的答案是,您可以在系统的ABI文档中找到系统调用更改的寄存器。 对于我的系统(飞思卡尔MPC5200B),我发现答案是IBM应用笔记'开发PowerPC嵌入式应用程序二进制接口(EABI)兼容程序'。 所以我将标记为volatile的寄存器(即R3..R12,F0..F13和标志寄存器)添加到clobbers列表中。 For future readers: The general answer is that you can find the registers altered by ...
  • 这不是使用内联汇编的正确方法。 如上所述,这两个语句是分开的,并且没有理由编译器必须保留两者之间的任何寄存器值。 您需要将两个汇编指令放在同一个内联汇编块中,并使用适当的输入和输出约束,或者您可以执行以下操作,以使编译器更高效: register unsigned int temp __asm__("r1") = 42; __asm__ volatile("swi 1" : : "r"(temp) : "memory"); (请注意,我在clobber列表中添加了内存;我不确定您正在进行哪个系统调用,但如 ...
  • 对于64位系统,Linux系统调用ABI与i * 86完全不同,除非有一层兼容性。 这可能会有所帮助: http : //callumscode.com/blog/3 我还在eglibc中找到了syscall源代码,它看起来确实不同: http ://www.eglibc.org/cgi-bin/viewvc.cgi/trunk/libc/sysdeps/unix/sysv/linux/x86_64/syscall.S ?视图=标记 所以看起来int $0x80对x86_64 Linux内核不起作用,你需要 ...
  • 使用原始字符串。 使用普通字符串, \1由Python解释,这意味着将带有ASCII代码1的字符放入字符串中,而不是传递给shell。 command = r"sed -E 's/(#define PACKET_DELAY_TIME_A).*/\1 " + pckt_delay_A[1] + r"/' variables\ orig.h > variables.h" 或者,您可以使用其re模块编写在Python中执行此操作的代码,而不是调用sed 。 Use a raw string. With a no ...
  • myCustomer指令现在位于Controller控制器上 一点都不。 它与控制器无关。 它恰好在范围内期望客户变量,并且控制器恰好在其范围内设置这样的变量。 但是您可以在任何视图中随意使用该指令。 在两个示例中,您可以使用控制器和指令完全相同的方式。 无论它们是在同一文件中定义还是在不同文件中定义都不会改变任何内容:只要文件都由html页面的