在 julia 中初始化后,如何修改 Composed Widgets 中 GtkListStore 的内容?

how can I modify the content of GtkListStore inside a Composed Widgets after its initialisiation in julia?

我想在 Julia 程序中使用 GTK 来显示随时间变化的属性列表。我现在同时使用 GTK 和 Julia

我尝试合并 the tutorial for composed widgets with the one for liststore,但这没有用,因为当我尝试从结构外部访问 ListStore 时出现错误。

为了我的特定目的,我什至不需要推送到 listStore。如果我可以修改已经存在的行就足够了。另外我还处于开发的早期阶段,所以我仍然可以换到另一个gui-framework,所以推荐也很感激。

以下工作,只要我不尝试访问 myListStore

using Gtk

mutable struct CustomStore <: Gtk.GtkListStore
    handle::Ptr{Gtk.GObject}
    listStore
    # listStore::Gtk.GtkListStoreLeaf
    # listStore::Ptr{Gtk.GObject}

    function CustomStore()
        container = GtkBox(:v)
        infoLabel= GtkLabel("container")
        push!(container, infoLabel)

        listStore = GtkListStore(String, Int)
        push!(listStore,("aaa",123))
        push!(listStore,("bbb",234))

        print("typeof(listStore): ")
        println(typeof(listStore)) # typeof(listStore): GtkListStoreLeaf

        tv = GtkTreeView(GtkTreeModel(listStore))
        rTxt = GtkCellRendererText()
        c1 = GtkTreeViewColumn("Name", rTxt, Dict([("text",0)]))
        c2 = GtkTreeViewColumn("Value", rTxt, Dict([("text",1)]))
        push!(tv, c1, c2)
        push!(container,tv)
        push!(listStore,("ccc",345))

        print("typeof(listStore): ")
        println(typeof(listStore)) # typeof(listStore): GtkListStoreLeaf

        return Gtk.gobject_move_ref(new(container.handle), container)
    end
end

function main()
    win = GtkWindow("My First Gtk.jl Program", 800, 600)
    myStore = CustomStore()
    push!(win, myStore)
    showall(win)
      
    # Some things I tried but yielded errors:
    # myListStore::GtkTreeStore = getproperty(myStore, :ts) 
    # myListStore::GtkListStoreLeaf = getproperty(myStore, :ts) 
    # myListStore = get_gtk_property(myStore,:ts,String) 

    myListStore = getproperty(myStore, :ts)
    print("typeof(myListStore): ")
    println(typeof(myListStore)) # typeof(myListStore): Gtk.GLib.FieldRef{CustomStore}


    println("Here is what my goal is: Modifying the listStore after the custom widget has been initialized: ")
    # push!(myListStore,("zzz",999))
    # --> ERROR: LoadError: MethodError: no method matching push!(::Gtk.GLib.FieldRef{CustomStore}, ::Tuple{String, Int64})
    # Closest candidates are:
    #   push!(::Any, ::Any, ::Any) at /opt/julia-1.7.2/share/julia/base/abstractarray.jl:2952
    #   push!(::Any, ::Any, ::Any, ::Any...) at /opt/julia-1.7.2/share/julia/base/abstractarray.jl:2953
    #   push!(::GtkTreeStore, ::Tuple) at ~/.julia/packages/Gtk/B6LVT/src/lists.jl:224
    
    
    if !isinteractive()
        @async Gtk.gtk_main()
        Gtk.waitforsignal(win,:destroy)
    end    
end

main()

在您的构造函数中,您应该 return 声明它的 new() 结构:一个句柄和一个列表存储。

您有时还需要在更改列表后更新 window。隐藏和显示 window 应该这样做:

using Gtk

mutable struct CustomStore <: Gtk.GtkListStore
    handle::Ptr{GObject}
    listStore::GtkListStoreLeaf
    function CustomStore()
        container = GtkBox(:v)
        infoLabel= GtkLabel("container")
        push!(container, infoLabel)

        listStore = GtkListStore(String, Int)
        push!(listStore,("aaa",123))
        push!(listStore,("bbb",234))

        print("typeof(listStore): ")
        println(typeof(listStore)) # typeof(listStore): GtkListStoreLeaf

        tv = GtkTreeView(GtkTreeModel(listStore))
        rTxt = GtkCellRendererText()
        c1 = GtkTreeViewColumn("Name", rTxt, Dict([("text",0)]))
        c2 = GtkTreeViewColumn("Value", rTxt, Dict([("text",1)]))
        push!(tv, c1, c2)
        push!(container,tv)
        push!(listStore,("ccc",345))

        print("typeof(listStore): ")
        println(typeof(listStore)) # typeof(listStore): GtkListStoreLeaf

        return Gtk.gobject_move_ref(new(container.handle, listStore), container)
    end
end

function main()
    win = GtkWindow("My First Gtk.jl Program", 800, 600)
    myStore = CustomStore()
    push!(win, myStore)
    showall(win)

    println("Here is what my goal is: Modifying the listStore after the custom widget has been initialized: ")
    push!(myStore.listStore,("zzz",999))
    hide(win)
    show(win)

    if !isinteractive()
        @async Gtk.gtk_main()
        Gtk.waitforsignal(win,:destroy)
    end
end

main()