我拼命尝试在树莓派上使用 gtkmm3 创建一个简单的 GUI,并且为此苦苦挣扎了太久。但是,当我运行程序时,我似乎遇到了分段错误,我不知道为什么..如果我取出网格上的最后一行,它就可以工作。我做错了什么还是更好的方法?
这是头文件gui_ex.h:
#ifndef GTKMM_EX_GUI_H
#define GTKMM_EX_GUI_H
#include <gtkmm.h>
class Gui_Ex : public Gtk::Window
{
public:
Gui_Ex();
virtual ~Gui_Ex();
protected:
// Signal handlers:
void on_connect_click();
void on_quit();
//child widgets
Glib::RefPtr<Gtk::Adjustment> m_adjustment;
Gtk::Box m_VBox;
Gtk::Label m_label, m_label2, m_label3;
Gtk::Grid m_grid;
Gtk::SpinButton m_spin;
Gtk::Button m_button3, m_button4;//, m_connect;
};
#endif // GTKMM_EX_GUI_H
gui_ex.cpp:
#include "gui_ex.h"
Gui_Ex::Gui_Ex()
:
m_adjustment( Gtk::Adjustment::create(1.0, 1.0, 5.0, 1.0, 5.0, 0.0) ),
m_VBox(Gtk::ORIENTATION_VERTICAL),
m_label2("label2"),
m_label3("label3"),
m_button3("Connect"),
m_button4("Quit"),
m_spin(m_adjustment),
m_label("Choose the number of clients")
{
set_title("Grid");
set_border_width(16);
add(m_VBox);
m_VBox.pack_start(m_grid);
m_grid.attach(m_label, 0,0,2,1); //column, row, width (# col span), height (# row span). starts at 0
m_grid.attach(m_spin, 2,0,1,1);
m_grid.attach(m_label2, 0,1,2,1);
m_grid.attach(m_label3, 2,1,1,1);
m_grid.attach(m_button3, 0,2,2,1);
m_grid.attach(m_button4, 2,2,1,1);
m_spin.set_wrap();
m_spin.set_numeric(true);
m_button3.set_sensitive(true);
m_button4.set_sensitive(false);
m_button3.signal_clicked().connect(sigc::mem_fun(*this, &Gui_Ex::on_connect_click));
m_button4.signal_clicked().connect(sigc::mem_fun(*this, &Gui_Ex::on_quit));
show_all_children();
}
Gui_Ex::~Gui_Ex()
{
}
void Gui_Ex::on_quit(){
bool running = false;
m_spin.set_sensitive(true);
m_button3.set_sensitive(true);
}
void Gui_Ex::on_connect_click()
{
int BACKLOG = m_spin.get_value_as_int();
m_spin.set_sensitive(false);
m_button4.set_sensitive(true);
}
main_gui_ex.cpp:
#include "gui_ex.h"
#include <gtkmm/application.h>
int main (int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
Gui_Ex gui_ex;
return app ->run(gui_ex);
}