-1

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>
Ali
  • 7
  • 5
  • Could [this](https://www.c-plusplus.net/forum/topic/113528/gtkmm-beispiel-programm-pt-3-multithreaded/3) help? – BobMorane Mar 11 '21 at 19:58
  • Yeah, it is helpful. but I wanna be sure, please edit my code. – Ali Mar 12 '21 at 08:58
  • I posted documentation below. Unfortunately, if you have no research to show (ex.: trial code, specific questions, etc), I can't help you. – BobMorane Mar 12 '21 at 20:34

2 Answers2

0

In python, there is something here: passing GUI button pressing to a thread controlling countdown in that GUI. perhaps similar can be done in c. for the reason I am not familiar with c, I cannot give you the precise c code. https://github.com/f4iteightiz/UWR_scoreboard

floppy_molly
  • 175
  • 1
  • 10
0

The official documentation (Gtkmm 3.24) has a nice example, with a progress bar being updated by a button. Basically, Glib::Dispatcher is what you are looking for.

For other versions of Gtkmm, see this page

BobMorane
  • 3,870
  • 3
  • 20
  • 42