Grid Gtk.图纸连接到Gtk.网格时为空白区域
(这是我的第一篇帖子,如果我做错了什么,请道歉…) 我正在用Vala写一个程序,人们可以用它来设计教室。 我决定在GUI中使用GTK(Vala与此集成良好), 和Cairo来绘制教室图(默认情况下,GTK随附) 我创建了一个“教室”类(Gtk.DrawingArea的子类), 当前应仅显示一个正方形:Grid Gtk.图纸连接到Gtk.网格时为空白区域,grid,gtk,cairo,vala,drawingarea,Grid,Gtk,Cairo,Vala,Drawingarea,(这是我的第一篇帖子,如果我做错了什么,请道歉…) 我正在用Vala写一个程序,人们可以用它来设计教室。 我决定在GUI中使用GTK(Vala与此集成良好), 和Cairo来绘制教室图(默认情况下,GTK随附) 我创建了一个“教室”类(Gtk.DrawingArea的子类), 当前应仅显示一个正方形: public class Classroom : DrawingArea { private delegate void DrawMethod(); public Classro
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;
}
}
我还为我的应用程序创建了一个类:
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);
}
我将我的教室
小部件放入Gtk.Grid
,这是我选择的布局小部件。
当我编译并运行代码时,会出现一个空白窗口:
但是,如果我不使用Gtk.Grid
,只使用root.add()
(我已注释掉)添加我的教室
,则教室
小部件将正确显示:
为什么我的小部件在使用Gtk.Grid添加时不显示
如何解决此问题?问题是单元格大小为0x0像素,因为网格不知道绘图区域实际需要多少空间 一个简单的解决方案是只请求一些固定的大小,请尝试以下方法:
var classroom = new Classroom();
classroom.set_size_request (40, 40);
PS:我是通过看其他关于SO的类似问题得出这个想法的,尤其是。欢迎来到SO,你的问题很好。我做了一些编辑(内联图像并删除了这里认为不必要的“谢谢”部分)。不过有一件小事:我将您的所有代码复制到
.vala
文件并尝试编译,但使用Gtk时缺少代码>和使用Cairo代码>指令。你应该考虑下一次把所有代码都放在一个代码块中。个人来说,我不喜欢<>代码>使用< /COD>指令,我只是用它的完整命名空间来限定每种类型(除了GLUB命名空间和我自己的类的命名空间)。更好的解决方案是设置小部件以展开/对齐,看见一个更好但更难的解决方案是将GtkDrawingArea子类化,并重写get_preferred_width()
和get_preferred_height()
方法以返回应该的大小。