gpt4 book ai didi

c++ - 带有 gtkmm 派生小部件的空白窗口

转载 作者:行者123 更新时间:2023-11-28 00:51:28 25 4
gpt4 key购买 nike

我目前正在用 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;
}

我的主窗口标题:

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();
}

似乎所有 Widget 都从构建器正确返回,但 app->run 只显示一个空窗口。这可能是什么原因?

最佳答案

您的构造函数不完整,您需要使用 cObject 初始化基类:

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

关于c++ - 带有 gtkmm 派生小部件的空白窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13877924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com