|
我在用gtkmm/libglademm编程的时候遇到了一个奇怪问题,可能是libglademm的一个BUG,请大家帮忙看看解决办法。
问题如下:
我要让程序在按下“关于”按钮时显示“关于”对话框。
我先定义Gtk::AboutDialog *gp_about_dialog; ,再用Gnome::Glade::Xml的get_widget方法来取得AboutDialog。m_refGlade->get_widget("gp_about_dialog", gp_about_dialog);
程序编译没有问题,但是在运行时却出错了。出错信息如下:
- (gpppoe:7858): libglademm-CRITICAL **: widget `gp_about_dialog' not found in glade file `gpppoe.glade'
- ** (gpppoe:7858): CRITICAL **: Gnome::Glade::Xml::get_widget(): dynamic_cast<> failed.
复制代码
但是glade文件中明明是有gp_about_dialog的呀,而且在取得其它的窗口时没有问题,只有在取得Gtk::AboutDialog时出错。
是我程序写错了,还是libglademm本身的问题?
我在网上搜索了一下,发现有人也碰到过这个问题。
http://osdir.com/ml/gnome.gnomemm/2005-08/msg00005.html
那人写道:
After a bit of research and experimentation, it would seem that the
problem lies in the way libglademm handles the <child
internal-child="vbox"> tags, inside widgets derrived from GtkDialog.
我发现Gtk::AboutDialog的确有一个内部的vbox类。真的是这里出的问题吗?有何解决方法?
附上源代码:
MainWindow.hpp
- #ifndef _MAINWINDOW_HPP_
- #define _MAINWINDOW_HPP_
- #include <gtkmm.h>
- #include <libglademm.h>
- //#include <libglademm/xml.h> //Load widgets from glade file
- #include <libglademm/variablesmap.h> //connect widgets and variables
- #include <sigc++/functors/slot.h> //signal slots
- class MainWindow: public Gtk::Window
- {
- public:
- MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade);
- virtual ~MainWindow();
- static MainWindow* create();
-
- Gtk::Button *gp_button_connect;
- protected:
- Glib::RefPtr<Gnome::Glade::Xml> m_refGlade;
- Gtk::Button *gp_button_about;
- Gtk::Button *gp_button_quit;
- Gtk::AboutDialog *gp_about_dialog;
- //Signal handlers:
- virtual void on_button_quit();
- void on_gp_button_about_clicked();
- };
- #endif // _MAINWINDOW_HPP_
复制代码
MainWindow.cpp
- #include <iostream>
- #include "MainWindow.hpp"
- #include <glibmm.h>
- #include <giomm.h>
- #define GLADE_FILE "gpppoe.glade"
- #define GLADE_NAME "gp_main_window"
- MainWindow::MainWindow (BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade)
- : Gtk::Window(cobject),
- m_refGlade(refGlade),
- gp_button_about(0),
- gp_about_dialog(0)
- {
- //Get the Glade-instantiated Button, and connect a signal handler:
- m_refGlade->get_widget("gp_button_about", gp_button_about);
- gp_button_about->signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_gp_button_about_clicked));
-
- m_refGlade->get_widget("gp_button_quit", gp_button_quit);
- gp_button_quit->signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_quit));
-
- m_refGlade->get_widget("gp_button_connect", gp_button_connect);
- //Signal handlers:
-
- }
- MainWindow::~MainWindow()
- {
-
- }
- MainWindow* MainWindow::create() {
- MainWindow *w = NULL;
- Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create(GLADE_FILE, GLADE_NAME);
- refXml->get_widget_derived(GLADE_NAME, w);
- return w;
- }
- void MainWindow::on_button_quit () {
- hide();
- }
- void MainWindow::on_gp_button_about_clicked() {
- gp_about_dialog = 0;
- m_refGlade->get_widget("gp_about_dialog", gp_about_dialog);
- if (gp_about_dialog)
- {
- //std::cout << "gp_about_dialog" << std::endl; //for test
- gp_about_dialog->show();
- }
- }
复制代码 |
|