GTK4 C - how to pass arguments

,

Hello, I have googled a lot and do not found anything helpfull.

Please could anybohy help how to improve my program to write into labels content of given argumenst instead of “arg1” and “arg2”.

Also witch checking if argument was given.

Thank you.

#include <gtk/gtk.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;


static void appActivate (GApplication *app, gpointer user_data) {
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));


    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);


    gtk_window_set_child (GTK_WINDOW (win), vBox);

    GtkWidget *labText;



    labText = gtk_label_new("arg1");
    gtk_box_append (GTK_BOX (vBox), labText);

    labText = gtk_label_new("arg2");
    gtk_box_append (GTK_BOX (vBox), labText);


    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_widget_show (win);
}


int main(int argc, char **argv) {
    GtkApplication *app;
    app = gtk_application_new ("testing.app", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (appActivate), NULL);
    return g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return 0;
}

Since you’re using GApplication, you’ll probably want to look at GApplicationCommandLine.

Really **argv is just a list of strings, but there are lots of helpers in GApplicationCommandLine to make working with them easier.

Thank you for reply.
So I have found example: gapplication-example-cmdline.c
and a litle bit change it.

But there is problem, that example uses gaplication and my example gtk_aplication.

Plaese how could I combine this two types of application?

now it answers me with this errors:

./argument arg1 arg2
arguments: 
0: ./argument
1: arg1
2: arg2

(argument:7625): GLib-GObject-WARNING **: 07:18:34.070: invalid cast from 'GApplication' to 'GtkApplication'

(argument:7625): Gtk-CRITICAL **: 07:18:34.070: gtk_application_window_new: assertion 'GTK_IS_APPLICATION (application)' failed
Neoprávněný přístup do paměti (SIGSEGV) (core dumped [obraz paměti uložen])

and actual code:

#include <gio/gio.h>
#include <gtk/gtk.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

static int
command_line (GApplication            *application,
              GApplicationCommandLine *cmdline)
{
  gchar **argv;
  gint argc;
  gint i;

  argv = g_application_command_line_get_arguments (cmdline, &argc);

    cout << "arguments: " << endl;

    string s0 = "parameter not entered";
    string s1 = "parameter not entered";

  for (i = 0; i < argc; i++) {
    cout << i << ": " << argv[i] << endl;
    if (i==0) {
        s0 = argv[i];
    }
    if (i==1) {
        s1 = argv[i];
    }
  }


  g_strfreev (argv);


  GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (application));


    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);


    gtk_window_set_child (GTK_WINDOW (win), vBox);

    GtkWidget *labText;



    labText = gtk_label_new(s0.c_str());
    gtk_box_append (GTK_BOX (vBox), labText);

    labText = gtk_label_new(s1.c_str());
    gtk_box_append (GTK_BOX (vBox), labText);


    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_widget_show (win);

  return 0;
}

int
main (int argc, char **argv)
{
  GApplication *app;
  app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
  int status;
  g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
  //g_application_set_inactivity_timeout (app, 10000);

  status = g_application_run (app, argc, argv);

  g_object_unref (app);

  return status;
}


GtkApplication is a subclass of GApplication, so you have to create a GtkApplication.

thanks! that works now.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.