0

I am beginning with GTK development, and I have struggled.

This first application took me 4 hours, I would like to know now if I am already doing too many obvious mistakes?

Mainly I want to ask how do I transition this code to GTK3? It gives me error:

 ‘GTK_OBJECT’ was not declared in this scope

If I try to compile against GTK3 instead of GTK2, i.e.:

pkg-config --cflags --libs gtk+-3.0

#include <gtk/gtk.h>
static int click_counter = 0;
void greet ( GtkWidget * widget, gpointer data )
{
 g_print ( "My first GTK app!\n" );
 g_print ( "%s clicked %d times\n", (char*) data, ++click_counter );
}
void destroy ( GtkWidget * widget, gpointer data )
{
 gtk_main_quit ();
}
int main ( int argc, char * argv[] )
{
 GtkWidget * main_window;
 GtkWidget * counter_button;
 gtk_init( & argc, & argv);
 main_window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
 g_signal_connect (main_window, "destroy", G_CALLBACK (destroy), NULL);
 gtk_container_set_border_width ( GTK_CONTAINER ( main_window ), 30 );
 counter_button = gtk_button_new_with_label ("Click me!");
 g_signal_connect ( GTK_OBJECT(counter_button), "clicked", G_CALLBACK (greet), NULL ); // last param counter_button ?
 gtk_container_add ( GTK_CONTAINER (main_window), counter_button );
 gtk_widget_show_all ( main_window );
 gtk_main ();
 return 0;
}

I compile using a Makefile, the main part:

CXX := g++-8
CXXFLAGS := -std=c++17 -Wall -Wextra -Wc++11-compat # -Werror -Wpedantic -pedantic-errors
LDLIBS := $$( pkg-config --cflags --libs gtk+-2.0 )
PROJECT := firstGTK
BINARY := $(PROJECT)
SOURCE := $(PROJECT).cpp
$(BINARY): $(SOURCE)
 $(CXX) $(CXXFLAGS) $(SOURCE) -o $(BINARY) $(LDLIBS)
halfer
20.1k19 gold badges110 silver badges207 bronze badges
asked May 15, 2019 at 6:04

1 Answer 1

3

If you are porting GTK 2 code to GTK 3, then you should read the GTK 2 to 3 migration guide. It is to be expected that some changes to the code will be necessary when you switch.

There is a section that talks specifically about how to replace GtkObject. The answer is, in most cases, to use GObject or GtkWidget.

answered May 16, 2019 at 3:00
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.