simonkrauter / NiGui

Cross-platform desktop GUI toolkit written in Nim
MIT License
724 stars 49 forks source link

Removing and adding controls to a container won't display them #123

Closed Wh1teDuke closed 1 year ago

Wh1teDuke commented 3 years ago

Sorry to bother you again, but I run into a different issue:

import nigui

proc test =
  app.init()

  var window = newWindow()
  var container = newLayoutContainer(Layout_Vertical)

  window.add(container)
  container.add(newLabel("Label"))

  discard startRepeatingTimer(1000, proc(event: TimerEvent) =
    var controls = container.childControls()
    for c in controls: container.remove(c)
    controls.add(newLabel("Label"))
    for c in controls: container.add(c)
  )

  window.show()
  app.run()

test()

Once the timer kicks in, no label will appear on screen, don't know if I'm doing something wrong. This is what the terminal displays: (test:3376): Gtk-CRITICAL **: gtk_widget_create_pango_layout: assertion 'GTK_IS_WIDGET (widget)' failed (test:3376): Pango-CRITICAL **: pango_layout_get_extents_internal: assertion 'layout != NULL' failed

Sznymo commented 1 year ago

I join the question

simonkrauter commented 1 year ago

I found the explanation here: https://stackoverflow.com/questions/31276404/gtk2-error-gtk-is-container-gtk-is-widget-failed

In short: Calling gtk_container_remove will decrease the refcount and in our case destroy the widget because the refcount reached 0. The solution is to increase the refcount by calling g_object_ref.