Gtk#菜单栏似乎与GLWidget冲突
本文关键字:GLWidget 冲突 菜单栏 Gtk# | 更新日期: 2023-09-27 18:24:00
我正在尝试使用gtk#和opengl编写一个简单而基本的建模程序。为了能够同时使用gtk gui和opengl,我正在使用glwidget。最近,我遇到了一个问题,在同一个vbox中同时有一个菜单栏和一个glwidget(它们在不同的插槽中,但在同一vbox中)。这是我的代码:
//MainWindow.cs
using System;
using Gtk;
using CubeGameModeler;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using GLib;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnExit (object sender, EventArgs e)
{
Application.Quit ();
}
protected void OnSaveAs (object sender, EventArgs e)
{
SaveWindow savewindow = new SaveWindow ();
savewindow.Show ();
}
protected void OnSettings (object sender, EventArgs e)
{
SettingsWindow settingswindow = new SettingsWindow ();
settingswindow.Show ();
}
protected void OnAbout (object sender, EventArgs e)
{
AboutWindow aboutwindow = new AboutWindow ();
aboutwindow.Show ();
}
protected void OnOpen (object sender, EventArgs e)
{
OpenWindow openwindow = new OpenWindow ();
openwindow.Show ();
}
protected void OnHelp (object sender, EventArgs e)
{
HelpWindow helpwindow = new HelpWindow ();
helpwindow.Show ();
}
protected void glwidget2_RenderFrame (object sender, EventArgs e)
{
GL.Viewport (0, 0, 100, 100);
GL.ClearColor (0.5f, 0.5f, 1.0f, 1.0f);
GL.Clear (ClearBufferMask.ColorBufferBit);
GL.MatrixMode (MatrixMode.Modelview);
GL.LoadIdentity ();
}
}
这是我的MainWindow.cs类中的代码。我还不担心opengl渲染,我只是想让窗口保持打开状态超过一秒钟。我不知道这是一个错误,还是只是和我那台糟糕的笔记本电脑有关。我在Debian Wheezy i386上使用MonoDevelop。当我尝试运行程序时,它会打开一瞬间,然后关闭。当我看到应用程序输出时,它是这样说的:
Loaded assembly: /home/mprogrammer/Documents/Monodevelop_Projects/CubeGameModeler/CubeGameModeler/bin/Debug/CubeGameModeler.exe
Loaded assembly: /usr/lib/mono/gac/gtk-sharp/2.12.0.0__35e10195dab3c99f/gtk-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/glib-sharp/2.12.0.0__35e10195dab3c99f/glib-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/atk-sharp/2.12.0.0__35e10195dab3c99f/atk-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll [External]
Loaded assembly: /usr/lib/mono/gac/gdk-sharp/2.12.0.0__35e10195dab3c99f/gdk-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/Mono.Posix/4.0.0.0__0738eb9f132ed756/Mono.Posix.dll [External]
Loaded assembly: /home/mprogrammer/Documents/Monodevelop_Projects/CubeGameModeler/CubeGameModeler/bin/Debug/OpenTK.dll [External]
Loaded assembly: /usr/lib/mono/gac/System.Drawing/4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll [External]
Stacktrace:
at (wrapper managed-to-native) Gtk.Application.gtk_main () <IL 0x00022, 0xffffffff>
at Gtk.Application.Run () <IL 0x00000, 0x0001f>
at CubeGameModeler.MainClass.Main (string[]) [0x00011] in /home/mprogrammer/Documents/Monodevelop_Projects/CubeGameModeler/CubeGameModeler/Main.cs:13
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
/usr/bin/mono() [0x80e863d]
/usr/bin/mono() [0x8128bac]
/usr/bin/mono() [0x8064d1f]
[0xb76f240c]
/lib/i386-linux-gnu/i686/cmov/libpthread.so.0(__pthread_mutex_lock+0x1d) [0xb769618d]
/usr/lib/i386-linux-gnu/libX11.so.6(+0x2999d) [0xb64d599d]
[0x156]
Debug info from gdb:
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
我已经通过反复试验确定问题出在菜单栏上,因为如果我把它留在窗口上,但让它不可见,那么glwidget就没有问题,程序就会继续运行
如果需要更多信息,请告诉我。
我也遇到过同样的问题。不仅有菜单,还有按钮和输入小部件。我找到了解决它的方法。在程序启动时初始化工具包为我解决了这个问题。这是代码:
public static void Main (string[] args)
{
using(Init())
{
Application.Init ();
MainWindow win = new MainWindow ();
win.Show ();
Application.Run ();
}
}
private static OpenTK.Toolkit Init()
{
return Toolkit.Init(new ToolkitOptions
{
Backend = PlatformBackend.PreferNative
});
}