Gtk.DrawingArea 附加到 Gtk.Grid 时空白

Gtk.DrawingArea blank when attached to Gtk.Grid

(这是我的第一个post,如果我做错了请见谅...)

我正在用 Vala 编写一个程序,可以用它设计一个 class 房间。 我决定将 GTK 用于 GUI(Vala 与此集成得很好), 和 Cairo 绘制 class 房间图(GTK 默认自带)。

我创建了一个 'classroom' class(Gtk.DrawingArea 的子class), 目前应该只显示一个正方形:

public class Classroom : DrawingArea
{
    private delegate void DrawMethod();

    public Classroom()
    {
        this.draw.connect((widget, context) => {
            return draw_class(widget, context, context.stroke);
            });
    }

    bool draw_class(Widget widget, Context context, DrawMethod draw_method)
    {
        context.set_source_rgb(0, 0, 0);
        context.set_line_width(8);
        context.set_line_join (LineJoin.ROUND);

        context.save();

        context.new_path();
        context.move_to(10, 10);
        context.line_to(30, 10);
        context.line_to(30, 30);
        context.line_to(10, 30);
        context.line_to(10, 10);
        context.close_path();

        draw_method();  // Actually draw the lines in the buffer to the widget

        context.restore();

        return true;
    }
}

我还为我的申请创建了一个class:

public class SeatingPlanApp : Gtk.Application
{
    protected override void activate ()
    {
        var root = new Gtk.ApplicationWindow(this);
        root.title = "Seating Plan";
        root.set_border_width(12);
        root.destroy.connect(Gtk.main_quit);

        var grid = new Gtk.Grid();
        root.add(grid);

        /* Make our classroom area */
        var classroom = new Classroom();
        grid.attach(classroom, 0, 0, 1, 1);
        //root.add(classroom);

        root.show_all();
    }

    public SeatingPlanApp()
    {
        Object(application_id : "com.github.albert-tomanek.SeatingPlan");
    }
}

这是我的主要功能:

int main (string[] args)
{
    return new SeatingPlanApp().run(args);
}

我将 classroom 小部件放入 Gtk.Grid,我选择的布局小部件。 当我编译我的代码并 运行 它时,我得到一个空白 window:

但是,如果我不使用 Gtk.Grid,而只是使用 root.add() 添加我的 classroom(我已将其注释掉),则会显示 classroom 小部件正确:

为什么使用 Gtk.Grid 添加时我的小部件没有显示?

我该怎么做才能解决这个问题?

问题在于单元格的大小为 0x0 像素,因为网格不知道 space 您的绘图区域实际需要多少。

一个简单的解决方案是只请求一些固定大小,试试这个:

var classroom = new Classroom();
classroom.set_size_request (40, 40);

PS:我通过查看关于 SO 的其他类似问题得到了这个想法,特别是 this one