0

I am trying to connect to the GTKMM configure_event signal. Other signals such as property_is_active and delete_event work fine.

in the code sample below, it compiles and runs, but when I move or resize the window with the mouse, the "cout" does not display on the console.

I am baffled at what could be wrong. The code follows, as the GTKMM docs say, the same pattern as other 'events', such as button press, which I have done before: enable the event's MASK, then connect it's signal to my handler. Based on some things "google" returned, I have tried both the add_event(...) shown here, also set_event(...), and included a "show()" either before the add/set call, in an effort to satisfy some hints in old tutorials (possibly from GTK2). There are other posts on various forums suggesting people have gotten past this point when (mostly languages other than C++.

(current Debian Linux, GTK 3)

Any help would be greatly appreciated.

#include <fstream>
#include <istream>
#include <ostream>
#include <iostream>
#include <gdkmm.h>
#include <gtkmm.h>

using namespace std;

class AdjunctWindow : public Gtk::Window {

public:

AdjunctWindow();
~AdjunctWindow();

bool on_configure_changed(GdkEventConfigure* configure_event);
};

AdjunctWindow::AdjunctWindow() {
  add_events(Gdk::STRUCTURE_MASK);
  signal_configure_event().connect( sigc::mem_fun(*this,
    &AdjunctWindow::on_configure_changed));
}

AdjunctWindow::~AdjunctWindow(){
}

bool AdjunctWindow::on_configure_changed(GdkEventConfigure* configure_event) {
 cout << "configure changed\n";
 return false;
}

int main(int argc, char** argv) {
  Gtk::Main kit(argc, argv);
  Gtk::Main::run(*(new AdjunctWindow())); 
}
Glenn
  • 1
  • 1

1 Answers1

2

connect() takes a second argument to set if your signal handler should be called before or after the default signal handler. The default is true which means that your signal handler would be called after the default. In this case you want it to be called before and should add the false argument.

See https://developer.gnome.org/glibmm/2.48/classGlib_1_1SignalProxy.html for further information.

The adjusted version of your code that has the signal handler called is below.

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

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

  bool on_configure_changed(GdkEventConfigure* configure_event);
};

AdjunctWindow::AdjunctWindow() {
  add_events(Gdk::STRUCTURE_MASK);
  signal_configure_event().connect(
    sigc::mem_fun(*this, &AdjunctWindow::on_configure_changed), false);
}

AdjunctWindow::~AdjunctWindow(){
}

bool AdjunctWindow::on_configure_changed(GdkEventConfigure* configure_event) {
 std::cout << "configure changed\n";
 return false;
}

int main(int argc, char** argv) {
  Gtk::Main kit(argc, argv);
  Gtk::Main::run(*(new AdjunctWindow())); 
}

Just as a note it is best not to use using namespace std; as it can cause name clashes between name spaces. Have a read of Why is "using namespace std" considered bad practice? which explains in more detail.

Community
  • 1
  • 1
  • Right. The default handler appears to block subsequent ones, hence the need to connect before it. This saved me a while more of head-scratching, so hopefully the +1 will bump it a bit further up on search results for the next programmer! Adding `Gdk::STRUCTURE_MASK` is redundant, though: as [the documentation](https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-configure-event) states, "_GDK will enable this mask automatically for all new windows._". – underscore_d Jul 17 '18 at 17:36
  • That was it why my program using `signal_damage_event()` was not compiling, so simple! – Erich Kuester Mar 25 '19 at 13:44