Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:27:53+00:00 2026-05-26T15:27:53+00:00

In virtually every example code of GTK+ I’ve seen so far, the widgets of

  • 0

In virtually every example code of GTK+ I’ve seen so far,
the widgets of the GUI are all defined inside the main function.
At first I adopted this, but then found it highly inconvenient when
e.g. manipulating multiple widgets from a single callback function.
Of course I could just use the ‘data’ gpointer for that, but wouldn’t I
have to wrap every widget I want to manipulate in some sort of struct first
to pass it as the ‘data’ argument?

Anyway, to not be bound by this, I just started defining all Widgets outside the main
function, so I can easily access them across all function. Are there any drawbacks to this style?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:27:54+00:00Added an answer on May 26, 2026 at 3:27 pm

    The drawbacks are the same as the drawbacks for using any global variable. This page has a good overview of situations when you should not (and when you should) use global variables. If you look near the bottom, you will see under “Really bad reasons to use global variables”:

    I don’t want to pass it around all the time.

    I’m afraid this is kind of what your objection sounds like. However, as the page I linked to also mentions, if you are just writing short or one-off programs, then the ease of using global variables probably outweighs the drawbacks.

    The usual way in medium-sized programs is to create a struct and populate it in main(), and pass it to the callbacks:

    typedef struct {
        GtkWidget *window, *button, *textfield;
    } Widgets;
    
    int main(int argc, char **argv) {
        gtk_init(&argc, &argv);
        Widgets *w = g_slice_new0(Widgets);
        w->window = gtk_window_new(... etc...
        ...
        g_signal_connect(w->button, "clicked", G_CALLBACK(on_clicked), w);
        ...etc...
        gtk_main();
        g_slice_free(Widgets, w);
        return 0;
    }
    

    In large programs, a better way is to create your own classes representing main windows, preferences dialogs, etc., and pass those classes to the various callbacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Virtually every code example out there reads a TXT file line-by-line and stores it
In virtually every project I've ever worked on, there will be one or two
We are trying to get the Model-View-Presenter pattern used on (virtually) all new dev
I've managed to get through my C++ game programming career so far virtually never
My GUI project in Qt has a lot of configuration pages classes which all
I'm a newbie in events. This example shows that an event is invoked every
For every example of registering .NET COM objects in the web, I see the
I am having virtually the same problem as this: C# Update combobox bound to
Would it be possible to write a class that is virtually indistinguishable from an
This is javascript, but a virtually identical regex is failing in PHP too, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.