简体   繁体   中英

GTK::main::run segfaults

I'm trying to write a GTK program. I managed to get my window to spawn with a button in it, but now when I try to pack a box and add 2 buttons to the box I segfault. What confuses me is that it doesn't segfault when I create anything, but insead when i run GTK::Main::run.

int main(int argc, char *argv[])
{
    Glib::RefPtr<Gtk::Application> app =
    Gtk::Application::create(argc, argv,
    "org.gtkmm.examples.base");

    MainWindow mainWindow;
    cout << "trying to run window"<< endl;
    Gtk::Main::run(mainWindow);
    cout << "done running window"<< endl;
    return 0;
 }

and

MainWindow::MainWindow()
:quit_button("Quit"),
write_button("Write"),
window_box()
{
set_border_width(10);
quit_button.signal_clicked().connect(sigc::mem_fun(*this,
    &MainWindow::quit_button_clicked));
write_button.signal_clicked().connect(sigc::mem_fun(*this,
    &MainWindow::write_button_clicked));
window_box.start_pack(quit_button);
window_box.start_pack(write_button);
add(window_box);
write_button.show();
quit_button.show();
window_box.show();
}

MainWindow::~MainWindow()
{

}

void MainWindow::write_button_clicked()
{
    std::cout << "Hello World" << std::endl;
}
void MainWindow::quit_button_clicked()
{
    exit(0);
}

are my main method and my constructor for my MainWindow class. I've tried not packing anything or packing less things and I still segfault. I'm brand new to GTK so I know I must be missing something simple.

Edit: Main Window Declaration

#include <gtkmm.h>
#include <iostream>

using namespace std;

class MainWindow : public Gtk::Window
{
public:
    MainWindow();
    ~MainWindow();

protected:
//Signal Handlers
 void   write_button_clicked();
 void   quit_button_clicked();


//Widgets
 Gtk::Button quit_button;
 Gtk::Button write_button;
 Gtk::VBox window_box;asd

};
#endif // GTKMM_EXAMPLE_HELLOWORLD_H

Gtk::Main has been deprecated and replaced by Gtk::Application , it handles the event loop now.

It works if instead of Gtk::Main::run(mainWindow); you use app->run(mainWindow); like ergosys said.

For proper cleanup, you should probably call app->quit() instead of libc's quit() , too (or just close the window, which will terminate the main loop too)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM