Best practice for passing widget data between callbacks in FLTK?Body

26 views
Skip to first unread message

Jackk Sonn

unread,
Apr 30, 2026, 5:52:51 PM (5 days ago) Apr 30
to fltk.general
Hi all, 
I'm trying to create a simple GUI where typing text into an Fl_Input and clicking an Fl_Button updates an Fl_Output field.I am struggling to pass the input widget pointer to the button's callback function to retrieve the value. I know callback(callback_func, data) allows passing void*, but I am not sure of the cleanest way to pass multiple widgets (the input to read from, and the output to write to) into one callback without using global variables.What is the recommended way to handle this in modern FLTK 1.3/1.4? Should I use a struct to pack the widgets, or is there a better design pattern? Thanks!

Albrecht Schlosser

unread,
Apr 30, 2026, 6:10:20 PM (5 days ago) Apr 30
to fltkg...@googlegroups.com
On 4/30/26 23:36 Jackk Sonn wrote:
> 'm trying to create a simple GUI where typing text into an Fl_Input
> and clicking an Fl_Button updates an Fl_Output field.I am struggling
> to pass the input widget pointer to the button's callback function to
> retrieve the value. I know callback(callback_func, data) allows
> passing void*, but I am not sure of the cleanest way to pass multiple
> widgets (the input to read from, and the output to write to) into one
> callback without using global variables.What is the recommended way to
> handle this in modern FLTK 1.3/1.4?

This is not exactly what you're asking for, but here's what you would
usually do: you don't need to use a button. Instead you attach the
callback directly to the input widget. Depending on the when() condition
you use each character or the full input (after losing the focus) could
be mirrored to the output widget. Possible code (untested):

void my_callback(Fl_Widget *w, void *v) {
  Fl_Input *in = (Fl_Input *)w;
  Fl_Output *o = (Fl_Output *)v;
  o->value(in->value());
}

auto input = new Fl_Input( ... );
auto output = new Fl_Output( ... );
input->callback(my_callback, (void *)output);

As you can see in the code of the callback, you can use both arguments.
The first is the widget that caused the callback, the second is the
argument of the callback assignment.

>  Should I use a struct to pack the widgets, or is there a better
> design pattern?

If you really want to use a button, then a struct with both pointers
would be a good choice (the "classic C++" decision).

Otherwise you may want to explore the more modern way of a lambda
expression, but if you need help with that, someone else may chime in...

> Thanks!

Welcome. And welcome to the world of FLTK!

Jackk Sonn

unread,
May 1, 2026, 3:08:03 AM (4 days ago) May 1
to fltk.general
Thanks very much let me try that out, I will update the results I get 

Ian MacArthur

unread,
May 1, 2026, 3:45:32 AM (4 days ago) May 1
to fltk.general
On Thursday, 30 April 2026 at 22:52:51 UTC+1 Jakk wrote:
Hi all, 
I'm trying to create a simple GUI where typing text into an Fl_Input and clicking an Fl_Button updates an Fl_Output field.I am struggling to pass the input widget pointer to the button's callback function to retrieve the value. I know callback(callback_func, data) allows passing void*, but I am not sure of the cleanest way to pass multiple widgets (the input to read from, and the output to write to) into one callback without using global variables.What is the recommended way to handle this in modern FLTK 1.3/1.4? Should I use a struct to pack the widgets, or is there a better design pattern? Thanks!

Aside from Albrecht's notes, if you _really_ want to use a button to trigger the updates (or otherwise pass references to callbacks) then there are a number of ways to go.

Honestly, if it was just a small, simple, GUI, I would probably just make the widgets all have "global" scope so the callback can reference them directly.
Not the "classy" solution, but simple and effective.

For more complex cases I probably would pack the items I wanted to reference into a struct (or a class), and pass that to the callback. Again, this is a simple approach, there are _much_ fancier ways you can do this, but the struct approach is effective and easy to understand!
Remember that in C++ a struct is just like a class with default public visibility, so you can put various methods into your struct to act on the data, access it etc., to help out the callback; so the callback needn't necessarily "know" all the details of the data, so long as the struct provides the appropriate helper methods to do the job.
 

Matthias Melcher

unread,
May 1, 2026, 4:20:00 AM (4 days ago) May 1
to fltk.general

FLTK 1.5 will support C++11 captures in callbacks, so you can give any number of arguments. 

As somewhat of a helper for 1.4 which does not support captures, there are macros that allow you to give up to four typed arguments to a callback. Take a look at FL_FUNKTION_CALLBACK in its variations. See link below:

https://www.fltk.org/doc-1.4/common.html#common_callbacks

Jackk Sonn

unread,
May 2, 2026, 9:41:52 PM (2 days ago) May 2
to fltk.general
Thanks guys I really do appreciate 

Reply all
Reply to author
Forward
0 new messages