Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Issues modifying the entries of a ComboBox #606

Closed
Boxylmer opened this issue Jan 5, 2022 · 18 comments
Closed

Issues modifying the entries of a ComboBox #606

Boxylmer opened this issue Jan 5, 2022 · 18 comments

Comments

@Boxylmer
Copy link

Boxylmer commented Jan 5, 2022

I currently have a simple combobox connected to a GtkListStore with a default entry. I want to add a few entries (and later remove / update them) and finally, access the currently chosen value when a button is pressed. I've made a mockup in Glade, but am struggling to actually see the results even though I can access the added values with indexing.

using Gtk
gladefile = GtkBuilder(filename=joinpath(@__DIR__, "basicapp.glade"))
showall(win)
combo_charge_port = gladefile["combo_charge_port"]
list_charge_ports = gladefile["list_charge_ports"]
btn_refresh_ports = gladefile["btn_refresh_ports"]
function btn_refresh_ports_clicked(widget)
    push!(list_charge_ports, ("test", 2))
end
signal_connect(btn_refresh_ports_clicked, btn_refresh_ports, "clicked")
julia> list_charge_ports
GtkListStoreLeaf()
julia> list_charge_ports[2]
("test", 2)
julia> list_charge_ports[1:2]
ERROR: MethodError: no method matching getindex(::GtkListStoreLeaf, ::UnitRange{Int64})

The docs point me to the list and tree widgets but I'm having a very difficult time generalizing what's going on here to modifying an existing widget from glade. Using the standard accessor notation from the built gladefile, it seems I only have access to the GtkListStoreLeaf and not the GtkListStore

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

Can you make your example working? You can do this by pasting the xml into a string and the GtkBuilder constructor can also take this string. Then one can take your code and paste it to the REPL.

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

And don't be confused about the GtkListStoreLeaf type. This is the the concrete widget. GtkListStore is just an abstract type. If I create the store in the REPL it will also be a GtkListStoreLeaf type.

@Boxylmer
Copy link
Author

Boxylmer commented Jan 5, 2022

Absolutely! Sorry about that, I'm trying to get the XML in a julia string and getting additional odd issues, likely a product of me being new to this, I can post what I have really quickly but I don't believe it will work just yet. I've cut the project down to an MWE as much as possible.

using Gtk

glade_xml = """
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkListStore" id="list_charge_ports">
    <columns>
      <!-- column-name stringcol -->
      <column type="gchararray"/>
      <!-- column-name idcol -->
      <column type="gint"/>
    </columns>
    <data>
      <row>
        <col id="0" translatable="yes">test_from_glade</col>
        <col id="1">1</col>
      </row>
    </data>
  </object>
  <object class="GtkWindow" id="main_window">
    <property name="can-focus">False</property>
    <child>
      <!-- n-columns=3 n-rows=1 -->
      <object class="GtkGrid">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="row-homogeneous">True</property>
        <property name="column-homogeneous">True</property>
        <child>
          <object class="GtkLabel">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="margin-left">5</property>
            <property name="margin-right">5</property>
            <property name="margin-start">5</property>
            <property name="margin-end">5</property>
            <property name="margin-top">5</property>
            <property name="margin-bottom">5</property>
            <property name="label" translatable="yes">Charge COM Port</property>
            <attributes>
              <attribute name="font-desc" value="Sans Bold 10"/>
            </attributes>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkComboBox" id="combo_charge_port">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="model">list_charge_ports</property>
            <property name="id-column">1</property>
          </object>
          <packing>
            <property name="left-attach">1</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="btn_refresh_ports">
            <property name="label" translatable="yes">Refresh</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="left-attach">2</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
"""
gladefile = GtkBuilder(buffer=glade_xml)
win = gladefile["main_window"]
showall(win)
combo_charge_port = gladefile["combo_charge_port"]
list_charge_ports = gladefile["list_charge_ports"]
btn_refresh_ports = gladefile["btn_refresh_ports"]
function btn_refresh_ports_clicked(widget)
    push!(list_charge_ports, ("test", 2))
end
signal_connect(btn_refresh_ports_clicked, btn_refresh_ports, "clicked")

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

So here it seems to add the entries but they are not shown. Are you sure you can link a list store with multiple entries with a combobox?

@Boxylmer
Copy link
Author

Boxylmer commented Jan 5, 2022

This is indeed the problem. I've tried using list stores with single columns in them (strings) but the behavior was unchanged. I assumed initially that I was facing a gotcha of some sort!

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

Are you on OS X as well? I can't get it work with a single column either. But this seems not to be a Julia problem. Glade also does not show the text entries, but it should.

@Boxylmer
Copy link
Author

Boxylmer commented Jan 5, 2022

I'm on Windows! And yup, I just checked and the preview-snapshop in Glade also doesn't even show the default entry that was inserted from Glade itself (which also shows up on the Leaf object in Julia).

I don't know if you noticed this, but the number of items in the dropdown increases when you push additional objects to the GtkListStoreLeaf, so it's recognizing and updating from changes.

So all in all, it's accessible, but Gtk refuses to display it for some reason. I still think I'm not doing something correctly in terms of linking the combobox to the list, but this is all quite new to me.

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

I think the GtkComboBox is the wrong widget. I usually use its Text variant: GtkComboBoxText.
Right now it is not clear to me how the GtkComboBox should actually know hoot render the List items.

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

I think you miss the cell renderer

@Boxylmer
Copy link
Author

Boxylmer commented Jan 5, 2022

Oh! Is this a different widget I should be finding in Glade? I'm not seeing an exact analog to the text version

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

I always use the text based, which seems to be one, where you have a list store with just one column of strings.

@Boxylmer
Copy link
Author

Boxylmer commented Jan 5, 2022

I'll find the widget and try this out, as well as potentially getting the renderer to work. If so, I'll report back here and hopefully be able to make a contribution to the docs. Thank you so much for the help!

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

I would recommend that you use

  1. GtkComboBoxText and hold in Julia an extra Vector / Dict where you can store additional column informations
  2. GtkComboBox without glade. Just grab a good tutorial from the web and you can do the same in Julia. The TreeView examples in our documentation show howto do that.

I always use approach 1. for combobox and approach 2. for treeviews

@tknopp
Copy link
Collaborator

tknopp commented Jan 5, 2022

Bildschirmfoto 2022-01-05 um 22 20 39

third column, third row (this is an old version of glade)

@Boxylmer
Copy link
Author

Boxylmer commented Jan 6, 2022

Looks like it works! I wasn't able to do much with the standard combobox, but the text one works well. The last thing is being able to clear the options (or access the underlying Dict/Vector that holds said options. Right now I can only get the widget itself with combo_charge_port = gladefile["combo_charge_port"], but where in the docs can I find other methods to access the data inside of it?

@Boxylmer
Copy link
Author

Boxylmer commented Jan 6, 2022

Found it. For anyone needing to modify these entries: The methods to do this are defined in lists.jl starting here. The method to remove things is generally empty!(::SomeLeafObject)

@Boxylmer Boxylmer closed this as completed Jan 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants