I want to make a useful application with gtkmm/glade and I need to pass a simple Button-signal to another thread. But unfortunately, I don't know how to do this. I made a small piece of software for a better demonstration. which is a button and a progress bar and if you press that button the progress bar gets filled up. It would be great if you modify my code to make the "ClickInterrupt()" function work in another thread.
cheers!
cpp file:
#include <iostream>
#include <gtkmm.h>
using namespace std;
Glib::RefPtr<Gtk::ProgressBar> MyProgressBar;
void ClickInterrupt (void)
{
static float value;
value += 0.2;
if (value > 1.0) value = 0.0;
MyProgressBar->set_fraction (value);
cout << "how do I run this in another thread?" << endl;
}
int main (int argc,char* argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.mylittleapp.uwu");
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("mygui.glade");
auto MainWindow = Glib::RefPtr<Gtk::ApplicationWindow>::cast_dynamic(builder->get_object("ApplicationMain"));
auto MyButton = Glib::RefPtr<Gtk::Button>::cast_dynamic(builder -> get_object("MyButton"));
MyButton -> signal_clicked().connect(sigc::ptr_fun(&ClickInterrupt));
MyProgressBar = Glib::RefPtr<Gtk::ProgressBar>::cast_dynamic(builder->get_object("Progress"));
app->run(*(MainWindow.get()));
}
glade file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkApplicationWindow" id="ApplicationMain">
<property name="can-focus">False</property>
<property name="title" translatable="yes">N00B :)</property>
<property name="default-width">200</property>
<property name="default-height">100</property>
<property name="icon-name">face-smile-big</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkProgressBar" id="Progress">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="MyButton">
<property name="label" translatable="yes">Fill the Progressbar</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>