I have the following example, where I create a simple window with a scrollbar and a scroll event controller that connects the onScroll
member function to handle the events:
<code>#include <iostream>
#include <gtkmm.h>
class ScrollingWindow : public Gtk::Window {
Gtk::Scrollbar scrollbar;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Glib::RefPtr<Gtk::EventControllerScroll> eventControllerScroll;
public:
ScrollingWindow() {
set_default_size(600, 100);
adjustment = Gtk::Adjustment::create(0, 0, 100, 1);
eventControllerScroll = Gtk::EventControllerScroll::create();
eventControllerScroll->signal_scroll().connect(sigc::mem_fun(*this, &ScrollingWindow::onScroll), true);
scrollbar.set_adjustment(adjustment);
scrollbar.add_controller(eventControllerScroll);
set_child(scrollbar);
}
bool onScroll(double dx, double dy) {
std::cout << "test" << std::endl;
return true;
}
};
int main(int argc, char **argv) {
auto app = Gtk::Application::create("hu.azsn.animation");
return app->make_window_and_run<ScrollingWindow>(argc, argv);
}
</code>
<code>#include <iostream>
#include <gtkmm.h>
class ScrollingWindow : public Gtk::Window {
Gtk::Scrollbar scrollbar;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Glib::RefPtr<Gtk::EventControllerScroll> eventControllerScroll;
public:
ScrollingWindow() {
set_default_size(600, 100);
adjustment = Gtk::Adjustment::create(0, 0, 100, 1);
eventControllerScroll = Gtk::EventControllerScroll::create();
eventControllerScroll->signal_scroll().connect(sigc::mem_fun(*this, &ScrollingWindow::onScroll), true);
scrollbar.set_adjustment(adjustment);
scrollbar.add_controller(eventControllerScroll);
set_child(scrollbar);
}
bool onScroll(double dx, double dy) {
std::cout << "test" << std::endl;
return true;
}
};
int main(int argc, char **argv) {
auto app = Gtk::Application::create("hu.azsn.animation");
return app->make_window_and_run<ScrollingWindow>(argc, argv);
}
</code>
#include <iostream>
#include <gtkmm.h>
class ScrollingWindow : public Gtk::Window {
Gtk::Scrollbar scrollbar;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Glib::RefPtr<Gtk::EventControllerScroll> eventControllerScroll;
public:
ScrollingWindow() {
set_default_size(600, 100);
adjustment = Gtk::Adjustment::create(0, 0, 100, 1);
eventControllerScroll = Gtk::EventControllerScroll::create();
eventControllerScroll->signal_scroll().connect(sigc::mem_fun(*this, &ScrollingWindow::onScroll), true);
scrollbar.set_adjustment(adjustment);
scrollbar.add_controller(eventControllerScroll);
set_child(scrollbar);
}
bool onScroll(double dx, double dy) {
std::cout << "test" << std::endl;
return true;
}
};
int main(int argc, char **argv) {
auto app = Gtk::Application::create("hu.azsn.animation");
return app->make_window_and_run<ScrollingWindow>(argc, argv);
}
Unfortunately, I don’t get any output in the terminal when I scroll the scrollbar. What do I miss?
Class reference that I checked:
Gtk::EventControllerScroll::signal_scroll. It doesn’t specify what the second bool argument should be, I tried both.
<code>#include <iostream>
#include <gtkmm.h>
class ScrollingWindow : public Gtk::Window {
Gtk::Scrollbar scrollbar;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Glib::RefPtr<Gtk::EventControllerScroll> eventControllerScroll;
public:
ScrollingWindow() {
set_default_size(600, 100);
adjustment = Gtk::Adjustment::create(0, 0, 100, 1);
eventControllerScroll = Gtk::EventControllerScroll::create();
// Connect the scroll signal to the handler function
eventControllerScroll->signal_scroll().connect(sigc::mem_fun(*this, &ScrollingWindow::onScroll), false);
scrollbar.set_adjustment(adjustment);
// Add the controller to the window instead of the scrollbar
add_controller(eventControllerScroll);
set_child(scrollbar);
}
bool onScroll(double dx, double dy) {
std::cout << "Scroll event detected. dx: " << dx << ", dy: " << dy << std::endl;
return true;
}
};
int main(int argc, char **argv) {
auto app = Gtk::Application::create("hu.azsn.animation");
return app->make_window_and_run<ScrollingWindow>(argc, argv);
}
</code>
<code>#include <iostream>
#include <gtkmm.h>
class ScrollingWindow : public Gtk::Window {
Gtk::Scrollbar scrollbar;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Glib::RefPtr<Gtk::EventControllerScroll> eventControllerScroll;
public:
ScrollingWindow() {
set_default_size(600, 100);
adjustment = Gtk::Adjustment::create(0, 0, 100, 1);
eventControllerScroll = Gtk::EventControllerScroll::create();
// Connect the scroll signal to the handler function
eventControllerScroll->signal_scroll().connect(sigc::mem_fun(*this, &ScrollingWindow::onScroll), false);
scrollbar.set_adjustment(adjustment);
// Add the controller to the window instead of the scrollbar
add_controller(eventControllerScroll);
set_child(scrollbar);
}
bool onScroll(double dx, double dy) {
std::cout << "Scroll event detected. dx: " << dx << ", dy: " << dy << std::endl;
return true;
}
};
int main(int argc, char **argv) {
auto app = Gtk::Application::create("hu.azsn.animation");
return app->make_window_and_run<ScrollingWindow>(argc, argv);
}
</code>
#include <iostream>
#include <gtkmm.h>
class ScrollingWindow : public Gtk::Window {
Gtk::Scrollbar scrollbar;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Glib::RefPtr<Gtk::EventControllerScroll> eventControllerScroll;
public:
ScrollingWindow() {
set_default_size(600, 100);
adjustment = Gtk::Adjustment::create(0, 0, 100, 1);
eventControllerScroll = Gtk::EventControllerScroll::create();
// Connect the scroll signal to the handler function
eventControllerScroll->signal_scroll().connect(sigc::mem_fun(*this, &ScrollingWindow::onScroll), false);
scrollbar.set_adjustment(adjustment);
// Add the controller to the window instead of the scrollbar
add_controller(eventControllerScroll);
set_child(scrollbar);
}
bool onScroll(double dx, double dy) {
std::cout << "Scroll event detected. dx: " << dx << ", dy: " << dy << std::endl;
return true;
}
};
int main(int argc, char **argv) {
auto app = Gtk::Application::create("hu.azsn.animation");
return app->make_window_and_run<ScrollingWindow>(argc, argv);
}
New contributor
Mohit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.