es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿Cómo hago que un GtkComboBox(Text) se expanda horizontalmente (hexpand) dentro de un GtkToolItem?

Estoy intentando incrustar un GtkComboBoxText en una GtkToolbar (similar a un campo de URL) a través de un GtkToolItem. Es la parte más importante de la barra de herramientas, por lo que me gustaría que ocupara todo el espacio disponible.

He intentado establecer hexpand como true en varios niveles de la cadena de contenedores, desde la toolbar hasta el GtkComboBoxText (ejemplo a continuación) y también en el GtkEntry y GtkCellAreaBox incrustados en cada contenedor intermedio al que pude acceder.

Al resaltar con el inspector, parece que la toolbar está bien con la toma de todo el espacio horizontal, pero no el toolitem, sin importar lo que contenga. Pero eso es suposición.

(Mi código está en Haskell, gi-gtk-3.0.37, enlazado dinámicamente a gtk3.22.30, pero no supongo que eso cambie algo)

main :: IO ()
main = do
  Gtk.init Nothing
  win <- new Gtk.Window [ #title := "win" ]
  on win #destroy Gtk.mainQuit
  vbox <- new Gtk.Box [ #orientation := Gtk.OrientationVertical ]
  #add win vbox

hbox <- new Gtk.Box [ #orientation := Gtk.OrientationHorizontal ]
  #add vbox hbox
  f1 <- new Gtk.Label [ #label := "filler 1" ]
  #add hbox f1
  cb1 <- new Gtk.ComboBoxText [ #hexpand := True ]
  #add hbox cb1
  f2 <- new Gtk.Label [ #label := "filler 2" ]
  #add hbox f2

tb <- new Gtk.Toolbar []
  #add vbox tb
  f3 <- new Gtk.ToolButton [ #label := "filler 3" ]
  #add tb f3
  tbi <- new Gtk.ToolItem [ #hexpand := True ]
  #add tb tbi
  cb2 <- new Gtk.ComboBoxText [ #hexpand := True ]
  #add tbi cb2
  f4 <- new Gtk.ToolButton [ #label := "filler 4" ]
  #add tb f4

#showAll win
Gtk.main

Mi objetivo es obtener resultados similares entre el hbox (arriba) y la toolbar (abajo).

enter image description here

¿Es posible este tipo de comportamiento? ¿Estoy usando GtkToolbar de forma incorrecta?

Tags:  ,

Answer

  1. Avatar for davy.ai

    Para hacer que el GtkComboBoxText ocupe tanto espacio como sea posible dentro de la GtkToolbar, establezca la propiedad expand del GtkToolItem en True y la propiedad fill en True. Esto hará que el GtkComboBoxText se expanda para ocupar el espacio disponible dentro de la GtkToolbar. Aquí está el código modificado:

    main :: IO ()
    main = do
      Gtk.init Nothing
      win <- new Gtk.Window [ #title := "win" ]
      on win #destroy Gtk.mainQuit
      vbox <- new Gtk.Box [ #orientation := Gtk.OrientationVertical ]
      #add win vbox
    
      hbox <- new Gtk.Box [ #orientation := Gtk.OrientationHorizontal ]
      #add vbox hbox
      f1 <- new Gtk.Label [ #label := "filler 1" ]
      #add hbox f1
      cb1 <- new Gtk.ComboBoxText [ #hexpand := True ]
      #add hbox cb1
      f2 <- new Gtk.Label [ #label := "filler 2" ]
      #add hbox f2
    
      tb <- new Gtk.Toolbar []
      #add vbox tb
      f3 <- new Gtk.ToolButton [ #label := "filler 3" ]
      #add tb f3
      tbi <- new Gtk.ToolItem [ #expand := True, #fill := True ]
      #add tb tbi
      cb2 <- new Gtk.ComboBoxText [ #hexpand := True ]
      #add tbi cb2
      f4 <- new Gtk.ToolButton [ #label := "filler 4" ]
      #add tb f4
    
      #showAll win
      Gtk.main
    

    Esto debería producir el comportamiento deseado, con el GtkComboBoxText ocupando tanto espacio como sea posible dentro de la GtkToolbar.

Comments are closed.