質問

I'm new to FLTK and C++. I need some help on how to get events to work. In other languages you create a button and define a function that would handle the event from button1 and button2 in different functions when the user clicks on it. Like adding an event listener to button1 and mapping it to button1_click().

This is some code from a multithreaded environment. I'm wondering how I can listen for the clicks from button1 and button2 and handle them correctly.

Also, with this design, I'm planning on having a separate thread update data on the GUI every 200 milliseconds in a loop. If I call lock and unlock in this loop, is there a possiblity this could throw an exception?

Thanks!!

#pragma once

#include <Fl.H>
#include <Fl_Window.H>
#include <Fl_Button.H>
#include <Windows.h>

class MGui
{
    public:
    Fl_Window*  window;
    Fl_Button*  button1;
    Fl_Button*  button2;

    static MGui &i() 
    {
        static MGui sMGui;
        return sMGui;
    }
    static void init() 
    {
        i();
        DWORD dwThreadId;
        CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) run, (LPVOID) 0, NULL, &dwThreadId); 
    }
    static DWORD_PTR WINAPI run(void *theParam)
    {
        i().window = new Fl_Window(100, 100, 340, 180, "Window");
        i().button1 = new Fl_Button(10, 10, 50, 24, "Button1");
        i().button2 = new Fl_Button(10, 44, 50, 24, "Button2");
        i().window->end();
        i().window->show();
        Fl::run();
        return 0;
    }
};
役に立ちましたか?

解決

То perform some action with a button you need to pass a call back function:

void cancel_callback(Fl_Widget* obj, void* data)
{
..
//do something
..
}
...
cancel = new Fl_Button(x, y, x1, y1, "Cancel");
cancel->callback(cancel_callback,(void*)this);

Its unsafe to update UI from threads. It should be done only from main thread. Use Fl::awake (Fl_Awake_Handler cb, void *message=0) in your thFn to execute some callback function within main thread.

void updateUI(void *userdata) 
{
...
// update UI
...
}

void* thFn(void* param)
{
...
Fl::awake(updateUI, userdata);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top