The*_*ple 1 c++ gtk button show gtkmm
我正在尝试在 gtkmm 中编写一个程序,但按钮不会显示。我已经做了我所知道的一切来让这些按钮显示,但没有任何效果。我什至在 main 和 win_home.cpp 文件中都包含了“显示全部”方法,但仍然没有任何反应。然而,程序确实会执行代码,因为 cout 语句都被打印出来。有谁知道为什么这些按钮不会显示?
主要.cpp:
#include <gtkmm.h>
#include <iostream>
#include "win_home.h"
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "com.InIT.InITPortal");
std::cout << "Creating Portal Window" << std::endl;
HomeGUI win_home;
win_home.set_default_size(600,400);
win_home.set_title("St. George InIT Home");
return app->run(win_home);
}
Run Code Online (Sandbox Code Playgroud)
win_home.cpp:
#include "win_home.h"
HomeGUI::HomeGUI()
{
//build interface/gui
this->buildInterface();
//show_all_children();
//register Handlers
//this->registerHandlers();
}
HomeGUI::~HomeGUI()
{
}
void HomeGUI::buildInterface()
{
std::cout << "Building Portal Interface" << std::endl;
m_portal_rowbox = Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 5);
add(m_portal_rowbox);
Gtk::Button m_pia_button = Gtk::Button("Printer Install Assistant");
m_portal_rowbox.pack_start(m_pia_button, false, false, 0);
m_pia_button.show();
Gtk::Button m_inventory_button = Gtk::Button("Inventory");
m_inventory_button.show();
m_portal_rowbox.pack_start(m_inventory_button, false, false, 0);
m_inventory_button.show();
//add(m_portal_rowbox);
//m_portal_rowbox.show_all();
m_portal_rowbox.show();
this->show_all_children();
std::cout << "Completed Portal Interface" << std::endl;
return;
}
void HomeGUI::registerHandlers()
{
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在 void 中,HomeGUI::buildInterface()您构建了 2 个按钮,并将它们添加到您的盒子容器中。当函数返回时,按钮将被销毁,因为它们现在超出了范围。由于它们不再存在,因此无法可见。
因此,对于第一个按钮,您将使用如下所示的内容:
Gtk::Button * m_pia_button = Gtk::manage(
new Gtk::Button("Printer Install Assistant"));
m_portal_rowbox.pack_start(&m_pia_button, false, false, 0);
m_pia_button.show();
Run Code Online (Sandbox Code Playgroud)
我希望您在窗口的整个生命周期中都需要轻松访问按钮。最简单的方法是将按钮作为类的成员。它将被构造为一个空按钮,您只需要随后设置标签即可。
class HomeGUI {
....
// A button (empty)
Gtk::Button m_pia_button;
....
};
....
void HomeGUI::buildInterface()
{
....
m_pia_button.set_label("Printer Install Assistant");
m_portal_rowbox.pack_start(m_pia_button, false, false, 0);
m_pia_button.show();
....
}
Run Code Online (Sandbox Code Playgroud)