FLTK vs Qt vs wxWidgets: When to Choose FLTK

Getting Started with FLTK — Installation and First WindowFLTK (Fast Light Toolkit) is a lightweight, cross-platform C++ GUI library designed for speed, small binary size, and simplicity. This guide walks through installing FLTK on Windows, macOS, and Linux, building a minimal “Hello, World” window, and explaining core concepts: widgets, layouts, event handling, and simple drawing.


Why choose FLTK?

  • Small and fast: FLTK produces lightweight binaries with fast startup.
  • Portable: Runs on Windows, macOS, Linux, and many UNIX variants.
  • C++-friendly: Simple C++ API without heavy meta-systems.
  • Permissive license: LGPL with modifications (allows static linking).

Prerequisites

  • A C++ compiler: GCC/Clang on Linux/macOS, MSVC or MinGW on Windows.
  • CMake (recommended) or a build system (make, Visual Studio).
  • Basic familiarity with compiling C++ programs.

Installing FLTK

Linux (Debian/Ubuntu)

  1. Install packages (Debian/Ubuntu):
    
    sudo apt update sudo apt install libfltk1.3-dev build-essential cmake 
  2. Alternatively, build from source (useful for latest version):
    
    git clone https://github.com/fltk/fltk.git mkdir fltk/build && cd fltk/build cmake ..  make -j$(nproc) sudo make install 

macOS (Homebrew)

  1. Install with Homebrew:
    
    brew install fltk 
  2. Or build from source (using CMake):
    
    git clone https://github.com/fltk/fltk.git mkdir fltk/build && cd fltk/build cmake -DCMAKE_BUILD_TYPE=Release .. make -j$(sysctl -n hw.ncpu) sudo make install 

Windows (MSYS2 / MinGW)

  1. Using MSYS2 / pacman:
    • Open MSYS2 MinGW shell (mingw64) and run:
      
      pacman -Syu pacman -S mingw-w64-x86_64-toolchain mingw-w64-x86_64-cmake mingw-w64-x86_64-fltk 
  2. Or build with Visual Studio:
    • Install CMake and Visual Studio (with C++ tools).
    • From a developer command prompt:
      
      git clone https://github.com/fltk/fltk.git mkdir fltkuild cd fltkuild cmake -G "Visual Studio 17 2022" -A x64 .. cmake --build . --config Release 

Your first FLTK program

Create a file named main.cpp with the following code:

#include <FL/Fl.H> #include <FL/Fl_Window.H> #include <FL/Fl_Box.H> int main() {     Fl_Window *window = new Fl_Window(340,180, "Hello FLTK");     Fl_Box *box = new Fl_Box(20,40,300,100, "Hello, World!");     box->box(FL_UP_BOX);     box->labelsize(24);     box->labelfont(FL_BOLD+FL_ITALIC);     window->end();     window->show();     return Fl::run(); } 

Explanation:

  • Fl_Window creates a top-level window (width, height, title).
  • Fl_Box is a simple widget for displaying text or graphics.
  • window->end() signals adding widgets is complete.
  • window->show() displays the window.
  • Fl::run() starts the FLTK event loop.

Building the example

Using g++ (Linux/macOS with pkg-config)

g++ main.cpp -o hello `fltk-config --cxxflags --ldflags` 

Or explicitly:

g++ main.cpp -o hello -I/usr/local/include -L/usr/local/lib -lfltk 

With CMake

Create CMakeLists.txt:

cmake_minimum_required(VERSION 3.5) project(hello_fltk) find_package(FLTK REQUIRED) include_directories(${FLTK_INCLUDE_DIR}) add_executable(hello main.cpp) target_link_libraries(hello ${FLTK_LIBRARIES}) 

Then:

mkdir build && cd build cmake .. cmake --build . 

Windows (MSVC)

If you built FLTK with Visual Studio, create a project, add main.cpp, and link fltk.lib (and dependencies). If using MSYS2/MinGW, use g++ similarly to Linux but with the appropriate fltk-config or library paths.


Widgets, Layout, and Callbacks

  • Widgets are subclasses of Fl_Widget. Common widgets: Fl_Button, Fl_Input, Fl_Menu_Bar, Fl_Group.
  • Layout is manual: you set widget position and size. There are helper containers (Fl_Pack, Fl_Scroll, Fl_Group).
  • Callbacks handle events. Example button callback:
#include <FL/Fl_Button.H> void cb_button(Fl_Widget* w, void* data) {     Fl_Button* b = (Fl_Button*)w;     b->label("Clicked!"); } Fl_Button* btn = new Fl_Button(10,10,100,30,"Click"); btn->callback(cb_button); 

Callbacks receive the widget pointer and a user-data pointer.


Simple drawing with Fl_Box or Fl_Widget

Override draw() in a custom widget:

#include <FL/Fl_Widget.H> class MyWidget : public Fl_Widget { public:     MyWidget(int X,int Y,int W,int H) : Fl_Widget(X,Y,W,H) {}     void draw() override {         fl_color(FL_BLUE);         fl_rectf(x(), y(), w(), h());         fl_color(FL_WHITE);         fl_draw("Custom draw", x()+10, y()+20);     } }; 

Add to a window like any widget: new MyWidget(10,10,200,100);


Event handling basics

  • handle(int event) lets widgets intercept low-level events (FL_PUSH, FL_RELEASE, FL_KEYDOWN, etc.).
  • Use Fl::add_timeout for timers, Fl::repeat_timeout to reschedule.

Example timer:

void tick(void*){   // do something periodically   Fl::repeat_timeout(1.0, tick); } Fl::add_timeout(1.0, tick); 

Tips and best practices

  • Keep the UI simple; FLTK shines for compact interfaces and tools.
  • Use Fl_Group/Fl_Pack to manage collections of widgets; experiment with resizable() to enable dynamic layout.
  • For complex apps, separate UI code from logic and use callbacks to call controller functions.
  • Use fltk-config (on Unix) to get correct compiler/linker flags.

Debugging and common issues

  • Linker errors: ensure you link against libfltk and use correct include paths. Use fltk-config --cxxflags --ldflags.
  • On macOS, you may need to link Cocoa frameworks if building manually. Using Homebrew-installed FLTK and cmake usually handles this.
  • If fonts or icons look odd, confirm FLTK was built with fontconfig and Xft support (on Linux).

Further resources

  • FLTK official docs and API reference in the source repo.
  • Examples folder in the FLTK source tree — great to study.
  • Community forums and mailing lists for platform-specific quirks.

FLTK is ideal when you want a small, fast GUI for tools and utilities. The example above gets you a visible window in a few lines of code and provides the foundation to explore widgets, custom drawing, and event-driven programming.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *