从Gtk.FlowBox中移除小部件

问题描述:

我试图通过销毁小部件从Gtk.FlowBox中移除一个小部件,但是还有一个灰色框留在原地。任何想法如何删除灰色框,以便相邻的小部件在删除小部件后落入适当位置。从Gtk.FlowBox中移除小部件

以下是如何窗口小部件包装: 1-两个图像(来自PIXBUF)和标签被添加到窗格在overlayImage 2-叠印图像被添加到事件盒 3-事件盒被添加到FlowBox

我曾尝试以下方法:1 摧毁事件盒 2-获取和破坏叠加图像的所有儿童,然后摧毁覆盖图像和事件盒

在这两种情况下,小部件被删除,但一个空白区域停留在点击时灰色的地方,但什么都不做 - 看图片。

如何删除此空白空间,以便下一个小部件自动落入小部件已移除的位置,并且下一个小部件落入其位置等等。

的代码可以在这里和“removeSelectedBooksFromLibrary”是被去除事件盒通过从FlowBox

此用户选择是从FlowBox添加部件 https://github.com/babluboy/bookworm/blob/master/src/bookworm.vala#L589

此代码中的方法是代码从FlowBox删除部件 https://github.com/babluboy/bookworm/blob/master/src/bookworm.vala#L512

enter image description here 预先感谢

下面是添加解决方案的一个工作示例,它将删除小部件及其父项。

public class FlowBoxIssue : Gtk.Window { 

    public static int main (string[] args) { 
     Gtk.init (ref args); 
     FlowBoxIssue window = new FlowBoxIssue(); 
     window.show_all(); 
     Gtk.main(); 
     return 0; 
    } 

    public FlowBoxIssue() { 
     this.title = "FlowBox Issue"; 
     this.window_position = Gtk.WindowPosition.CENTER; 
     this.destroy.connect (Gtk.main_quit); 
     this.set_default_size (800, 600); 

     Gtk.FlowBox library_flowbox = new Gtk.FlowBox(); 
     Gtk.Box library_mainbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 20); 

     Gtk.ScrolledWindow library_scroll = new Gtk.ScrolledWindow (null, null); 
      library_scroll.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); 
      library_scroll.add (library_flowbox); 

     Gtk.Overlay aOverlayImage1 = new Gtk.Overlay(); 
     Gtk.EventBox aEventBox1 = new Gtk.EventBox(); 
     Gtk.EventBox aEventBox2 = new Gtk.EventBox(); 
     Gtk.EventBox aEventBox3 = new Gtk.EventBox(); 
     try{ 
     Gdk.Pixbuf aBookCover1 = new Gdk.Pixbuf.from_file_at_scale("cover.png", 200, 250, false); 
     Gtk.Image aCoverImage1 = new Gtk.Image.from_pixbuf(aBookCover1); 

     aOverlayImage1.add(aCoverImage1); 
     Gtk.Label overlayTextLabel1 = new Gtk.Label("Label 1"); 
     aOverlayImage1.add_overlay(overlayTextLabel1); 
     aEventBox1.add(aOverlayImage1); 
     library_flowbox.add (aEventBox1); 
     }catch(Error e){ 

     } 

     Gtk.Overlay aOverlayImage2 = new Gtk.Overlay(); 
     try{ 
     Gdk.Pixbuf aBookCover2 = new Gdk.Pixbuf.from_file_at_scale("cover.png", 200, 250, false); 
     Gtk.Image aCoverImage2 = new Gtk.Image.from_pixbuf(aBookCover2); 

     aOverlayImage2.add(aCoverImage2); 
     Gtk.Label overlayTextLabel2 = new Gtk.Label("Label 2"); 
     aOverlayImage2.add_overlay(overlayTextLabel2); 
     aEventBox2.add(aOverlayImage2); 
     library_flowbox.add (aEventBox2); 
     }catch(Error e){ 

     } 

     Gtk.Overlay aOverlayImage3 = new Gtk.Overlay(); 
     try{ 
     Gdk.Pixbuf aBookCover3 = new Gdk.Pixbuf.from_file_at_scale("cover.png", 200, 250, false); 
     Gtk.Image aCoverImage3 = new Gtk.Image.from_pixbuf(aBookCover3); 

     aOverlayImage3.add(aCoverImage3); 
     Gtk.Label overlayTextLabel3 = new Gtk.Label("Label 3"); 
     aOverlayImage3.add_overlay(overlayTextLabel3); 
     aEventBox3.add(aOverlayImage3); 
     library_flowbox.add (aEventBox3); 
     }catch(Error e){ 

     } 

     Gtk.Button delete_button = new Gtk.Button.with_label("Delete Pix"); 
     delete_button.clicked.connect (() => { 
      //This is the line which resolved the issue - get the parent of the widget and destroy it and then destroy the widget 
      aEventBox2.get_parent().destroy(); 
       aEventBox2.destroy(); 
      }); 

     library_mainbox.pack_start(library_scroll, true, true, 0); 
     library_mainbox.pack_end(delete_button, true, false, 0); 
     this.add(library_mainbox); 
    } 

} 
+0

您忘记了链接代码(在问题本身中应该确实是[MCVE](http://*.com/help/mcve))。 – andlabs

+0

忘记提及代码的道歉。我将尝试创建一个MCVE并更新问题 –

你添加一个子窗口小部件,GtkFlowBox每次,流浆箱部件将在两者之间添加一个隐含的子部件,事件处理和造型的目的 - 作为文档GtkFlowBox状态:

尽管GtkFlowBox必须只有GtkFlowBoxChild子元素,但您可以通过gtk_container_add()向其添加任何类型的构件,并且GtkFlowBoxChild构件将自动插入到构件和构件之间。

这意味着该行:

library_grid.add(aEventBox); 

是真的等同于:

var flowbox_child = new Gtk.FlowBoxChild(); 
flowbox_child.add(aEventBox); 
library_grid.add(flowbox_child); 

如果你想从GtkFlowBox删除一个小部件,你只保留给孩子一个参考您添加了,您需要检索其父项并从GtkFlowBox删除:

aEventBox.get_parent().destroy(); 
// or 
library_grid.remove(aEventBox.get_parent()); 
+0

非常感谢您的快速和非常明确的回应。该解决方案在我的主代码和我正在创建的MVCE中工作。我已经将MVCE添加到了这个问题中,以防止有人遇到同样的问题。耻辱,我没有正确阅读文件。再次感谢 ! –