gtkmm 中的程序不会显示按钮

Program in gtkmm will not show buttons

本文关键字:显示 按钮 程序 gtkmm      更新时间:2023-10-16

我正在尝试用gtkmm编写程序,但按钮不会显示。 我已经做了我所知道的一切来显示这些按钮,但没有任何效果。 我什至在主文件和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);
}

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

在无效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();

我希望您需要在窗口的整个生命周期内轻松访问按钮。最简单的方法是将按钮作为类的成员。它将被构造为一个空按钮,之后您只需要设置标签。

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