Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating a TableView #54

Closed
tibaes opened this issue Mar 19, 2018 · 6 comments
Closed

Updating a TableView #54

tibaes opened this issue Mar 19, 2018 · 6 comments

Comments

@tibaes
Copy link

tibaes commented Mar 19, 2018

Hi there, I am learning to use the TableView.
I am already able to load a QML table view with data from a julia array.
What I don't understand is how to push! an element to the julia array and then update/refresh the QML table view.
My qml and julia codes:

import QtQuick 2.3
import QtQuick.Controls 1.4
import QtQuick.Window 2.2
import QtQuick.Layouts 1.2
import org.julialang 1.0

ApplicationWindow {
    title: "Biometric Enroll"
    width: 1024
    height: 768
    visible: true

    menuBar: MenuBar {
        Menu {
            title: "File"
            MenuItem {
                text: "Login"
            }
            MenuItem {
                text: "Lock"
            }
            MenuItem {
                text: "Quit"
            }
        }
        Menu {
            title: "Enrolee"
            MenuItem {
                text: "Add a new Enrolee"
            }
            MenuItem {
                text: "Find by Name"
            }
            MenuItem {
                text: "Find by Birth"
            }
            MenuItem {
                text: "Find by Document"
            }
            MenuItem {
                text: "Find by Biographic"
            }
        }
        Menu {
            title: "Auth"
            MenuItem {
                text: "Verify Identity"
            }
            MenuItem {
                text: "Identify"
            }
            MenuItem {
                text: "Verify Relationship"
            }
        }
    }

    ColumnLayout {
        id: root
        spacing: 6
        anchors.fill: parent

        RowLayout {
            Layout.fillWidth: true
            Layout.alignment: Qt.AlignCenter

            Button {
                Layout.alignment: Qt.AlignCenter
                text: "Add column"
                onClicked: { Julia.append_enrolee() }
            }

            Button {
                Layout.alignment: Qt.AlignCenter
                text: "Remove column"
                onClicked: { Julia.pop_enrolee() }
            }
        }

        TableView {
            id: view
            Layout.fillWidth: true
            Layout.fillHeight: true
            model: enroleeModel

            TableViewColumn {
                id: nameColumn
                title: "Name"
                role: "name"
                width: 250
            }

            TableViewColumn {
                id: birthColumn
                title: "Birth"
                role: "birth"
                width: 120
            }

            TableViewColumn {
                id: parentColumn
                title: "Parent"
                role: "parent"
                width: 320
            }
        }
    }
}
using Base.Test
using QML

struct Enrolee
    name::String
    birth::String
    parent::String
end

enrolees = [Enrolee("rafael", "10/04/1990", "roger")]
enroleeModel = ListModel(enrolees)

function append_enrolee()
    println("tentando dar push!...")
    push!(enrolees, Enrolee("evandro", "10/04/1980", "roger"))
    enroleeModel = ListModel(enrolees)
end

function pop_enrolee()
    println("pressed pop year")
end

@qmlfunction append_enrolee pop_enrolee

# Load QML after setting context properties, to avoid errors on initialization
qml_file = joinpath(dirname(@__FILE__), "qml", "main.qml")
@qmlapp qml_file enroleeModel

# Run the application
exec()

println("Getting out.")
@barche
Copy link
Collaborator

barche commented Mar 19, 2018

Hi,

Currently, it is only possible to manipulate the ListModel from QML. The dynamiclist example shows how to do this, the relevant code is here:
https://github.com/barche/QML.jl/blob/master/example/qml/dynamiclist.qml#L177-L181

Changing the ListModel like this from within QML will modify the underlying Julia array.

That said, maybe it's not a bad idea to provide a Julia interface to modify the ListModel from the Julia side.

@tibaes
Copy link
Author

tibaes commented Mar 21, 2018

Thanks for your fast answer!

That said, maybe it's not a bad idea to provide a Julia interface to modify the ListModel from the Julia side.

Agreed! In my case, the model needs to be updated by database/Julia and not by the QML UX.

I am thinking about destroying the model and building a new one, however I don't know the best way to do this... do I need to destroy the entire window and construct a new tableview from the ground or there is a way to switch the model and perform some sort of "refresh" in the tableview/window?

barche added a commit that referenced this issue Mar 21, 2018
@barche
Copy link
Collaborator

barche commented Mar 21, 2018

If you can use the master version of CxxWrap.jl and QML.jl then you can use the ListModel Julia interface I just added, indexing operations, push! and delete! should work.

@tibaes
Copy link
Author

tibaes commented Mar 22, 2018

Awesome!

However, for some reason I am getting "UndefVarError(:push_back)" on push! to the ListModel.
My updated Julia code is:

using Base.Test
using QML

struct Enrolee
    name::String
    birth::String
    parent::String
end

enrolees = [Enrolee("rafael", "10/04/1990", "roger")]
enroleeModel = ListModel(enrolees)

function append_enrolee()
    println("trying to push!...")
    push!(enroleeModel, Enrolee("evandro", "10/04/1980", "roger"))
end

function pop_enrolee()
    println("pressed pop year")
end

@qmlfunction append_enrolee pop_enrolee

# Load QML after setting context properties, to avoid errors on initialization
qml_file = joinpath(dirname(@__FILE__), "qml", "main.qml")
@qmlapp qml_file enroleeModel

# Run the application
exec()

println("Getting out.")

While the qml code stays the same. I got the error on calls to append_enrolee()

@barche
Copy link
Collaborator

barche commented Mar 22, 2018

With any luck (if you're not on Windows) this should be fixed with a Pkg.build("QML").

@tibaes
Copy link
Author

tibaes commented Mar 22, 2018

ouch, I forgot the build (luckily I'm on a Mac).
Worked like a charm, thank you!

@tibaes tibaes closed this as completed Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants