How to add/remove pages to the favorite list page?



  • Hello. I have list menu and there are many apps. So I have Favorite tab page in order to insert some pages to it. When the user is in a page and clicks on the favorite star icon, the page should be added to the favorite list page. I have two examples here.
    First example is working, when clicking on favorite action it adds the page dynamically. When again clicking the action it removes the page.
    Second example is not working. It has a page in ApplicationWindow, when entering the page and clicking the favorite action it doesn't work. I used the same method with the first app. Could anybody help me about this?
    Here is the short video of two examples.
    First Example:
    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
    import Qt.labs.settings 1.0
    ApplicationWindow{
        id:main
        width: 640
        height: 480
        visible:true
        title: "Favorites"
        property string datastore: ""
        property int countt2: 0
    
        Settings{
            property alias datastore: main.datastore
            property alias mycount: main.countt2
        }
    
        Component.onCompleted: {
            if(datastore){
                dataModel.clear()
                var datamodel=JSON.parse(datastore)
                for (var i=0; i<datamodel.length; ++i) dataModel.append(datamodel[i])
            }
        }
        onClosing: {
            var datamodel = []
            for (var i=0;i<dataModel.count; ++i) datamodel.push(dataModel.get(i))
            datastore=JSON.stringify(datamodel)
        }
        initialPage:TabbedPage {
            title: main.title
            actions: [
                Action {
                    id:action2
                    text: qsTr("Settings")
                    icon.name: "toggle/star"
                    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)
                        }
                    }
                }
             ]
        }
            ListView {
                id:malist
                width: parent.width
                height: parent.height
                focus: true
                interactive: true
                clip: true
                model:FavModel{
                id:dataModel
                }
                delegate: ListItem {
                    text: model.title
                }
            }
    }
    

    FavModel.qml:

    import QtQuick 2.0
    
    ListModel{
            id:dataModel
    }
    

    Second Example:
    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:/Angle_convert2.qml" }
                }
                delegate: ListItem {
                    text: model.title
                    onClicked: pageStack.push(model.source)
                }
            }
            }
            Favourites{}
        }
    }
    

    Favorites.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
    }
    

    Angle_convert2.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:angconv
        property string datastore: ""
        property int countt2: 0
        Component.onCompleted: {
            console.log("onComponent 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)
        }
        Settings{
            id:mysetting4
            property alias datastore: angconv.datastore
            property alias mycount: angconv.countt2
        }
        FavModel{
            id:dataModel
        }
        actions: [
            Action {
              id:favourite2
                onTriggered:{
                    countt2++
                    console.log("triggered works.Count/2: "+ countt2%2)
                    if(countt2%2==1){
                        console.log("List 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)
                    }
                }
                text: qsTr("Favourite")
                icon.name: "toggle/star"
                toolTip: qsTr("Favourite")
            }
        ]
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.