与gtkmm派生小部件的空白窗口

时间:2012-12-14 11:34:49

标签: c++ glade gtkmm

我目前正在用c ++编写一个小桌面应用程序,这是我第一次使用gtkmm。

我按照here的教程,但我得到的只是一个空白窗口。它在将MainWindow重构为自己的类之前有效......

请注意:我已经省略了包含,使用和信号处理程序的实现。

这是我的主要功能:

int main(int argc, char **argv)
{
    RefPtr<Application> app = Application::create(argc, argv, "org.mw88.adbtool");
    //Main kit(argc, argv);

    RefPtr<Builder> builder = Builder::create();
    try
    {
        builder->add_from_file("Gui.glade");

        MainWindow *window = NULL;
        builder->get_widget_derived("MainWindow", window);
        app->run(*window);
        //kit.run(*window);
    }
    catch (const FileError& ex)
    {
        cerr << "FileError: " << ex.what() << endl;        
        return EXIT_FAILURE;
    }
    catch (const MarkupError& ex)
    {
        cerr << "MarkupError: " << ex.what() << endl;        
        return EXIT_FAILURE;
    }
    catch (const BuilderError& ex)
    {
        cerr << "BuilderError: " << ex.what() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

My MainWindow-Header:

class MainWindow : public Window
{
public:
    MainWindow(BaseObjectType* cobject, const RefPtr<Builder>& refBuilder);
    virtual ~MainWindow();
    void show(RefPtr<Application> &app);

private:
    RefPtr<Builder>     builder;
    ToolButton          *addButton;
    ToolButton          *editButton;
    ToolButton          *deleteButton;
    ToolButton          *connectButton;
    TreeView            *hostList;
    RefPtr<ListStore>   listStore; 
    Columns             cols;

    //Signal handlers:
    virtual void onAddClicked();
    virtual void onEditClicked();
    virtual void onDeleteClicked();
    virtual void onConnectClicked();  
};

这是MainWindow类的构造函数:

MainWindow::MainWindow(BaseObjectType* cobject, const RefPtr<Builder>& refBuilder)
{
    this->builder = refBuilder;

    addButton = NULL;
    editButton = NULL;
    deleteButton = NULL;
    connectButton = NULL;
    hostList = NULL;

    builder->get_widget("hostList", hostList);

    builder->get_widget("addButton", addButton);
    addButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onAddClicked));

    builder->get_widget("editButton", editButton);
    editButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onEditClicked));

    builder->get_widget("deleteButton", deleteButton);
    deleteButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onDeleteClicked));

    builder->get_widget("connectButton", connectButton);
    connectButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onConnectClicked));

    hostList->set_model(listStore = ListStore::create(cols));
    hostList->append_column("Host", cols.host);
    hostList->append_column("Port", cols.port);

    for (int i = 0; i < 20; i++)
    {
        TreeModel::Row row = *(listStore->append());
        row[cols.host] = "Testhost";
        row[cols.port] = i;
    }

    show_all_children();
}

似乎所有窗口小部件都是从构建器正确返回的,但app-&gt;运行只显示一个空窗口。可能是什么原因?

1 个答案:

答案 0 :(得分:3)

您的构造函数不完整,您需要使用cObject:

初始化基类
MainWindow::MainWindow(BaseObjectType* cobject, const RefPtr<Builder>& refBuilder)
  : Gtk::Window(cObject)  // add this
{
    this->builder = refBuilder;
    ...