12

I am a C# programmer who started using ubuntu about 2 years ago. I'm wanting to learn GUI programming in either C or C++. I don't really like mono, it tends to crash on my system. I have a basic understanding of C++. I have never worked in C, but it looks cool. Which toolkit should I learn/use? Give Pro/Cons of each. Thanks!

Kredns
  • 36,461
  • 52
  • 152
  • 203

7 Answers7

38

I could be accused of bias since I do help contribute to gtkmm, but I was a user first, so... In any case, I would highly recommend gtkmm if you're comfortable with C++. Memory management is much easier with gtkmm than with GTK+ because reference-counted objects are managed automatically with smart pointers. You can also instantiate objects as auto variables (e.g. on the stack) and have their lifetime determined by their scope. So in practice, it's much easier to avoid memory leaks with gtkmm than with GTK+.

Another huge advantage of gtkmm over GTK+ (in my opinion) is the use of a type-safe signals framework. In GTK+, you constantly need to pass things as void pointers and then cast them around to the type you think they should be. In gtkmm, you dont need to do this, and can take advantage of the compiler enforcing type-safety on your signal handlers.

Another big advantage over C/GTK+ is the ease of deriving new classes. In GTK+, you need to write a lot of boilerplate code and basically re-implement things that you get for free in C++ as part of the language (e.g. inheritance, constructors, destructors, etc). This is more tedious and error-prone.

greyfade mentioned that gtkmm is incomplete, and he's right to a certain extent -- gtkmm does not cover absolutely everything in the GTK+ API (though it gets awfully close). But in practice this is not a problem because you can always use the C/GTK+ API directly from your gtkmm code. This C compatibility is a huge advantage of C++ over something like C# or python bindings where you would have no alternatives if the binding didn't cover part of the API.

The only real reasons to choose GTK+ over gtkmm (IMO) are that gtkmm has a little additional overhead since it is a wrapper on top of the C library (but this is generally just a single function call, which is going to have negligible impact), or if you hate or can't use C++.

jonner
  • 6,331
  • 6
  • 30
  • 28
18

If you're a C# programmer, why don't you take a look at Vala?

I use pygtk for most of my Linux GUI applications, but Python was simply too slow for the project I'm working on right now, so I was trying to pick one of GTK+ and GTKmm. Then I met Vala.

It's a pretty new language, and therefore documentation is pretty limited at the moment, but I think it has the best of both worlds: C# syntax with C speed.

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
13

Since C++ is more familiar to you, you may find GTKmm to be a better fit, since you can use idioms like RAII. Unfortunately, GTKmm is a little incomplete and is missing a few of the lesser-used parts of GTK.

GTK+ on its own, however, essentially exposes an object model similar to what you find in C++, but with only C functions. Things like construction and destruction in C++ are done explicitly in the C API and instances of widgets are handled via pointers exclusively.

Try both and see which fits your project better.

greyfade
  • 24,948
  • 7
  • 64
  • 80
7

Like many have said, Gtkmm does provide you with good memory management, reference counted objects, etc. It does fall down in one department, though. Documentation. The whole of the Gtkmm project suffers from the "undocumentation" phenomena, where the posted (and reposted on 3rd party sites) documentation is simply a javadoc scan of the header files.

Just wanted you to know what you'd be getting into. For instance, the Scrolled Window is one of the better documented classes in Gtkmm.

  • 1
    To be precise, docs for the `mm` libraries (`gtkmm`, `glibmm`) are written into the headers, but what is written there is typically generated by converting the docs from the C functions in the GTK+/GLib/etc. `.c` file at the point of wrapping (building). However, this can be overridden and custom docs written, for instance when the C docs contain a lot of C-isms that do not convert well. Of course, if you see anything that is missing or badly converted, then patches to either the `mm` headers or the routine that converts from the C documentation would be welcome on Bugzilla or the mailing list – underscore_d Jul 31 '17 at 16:08
5

I would suggest to learn vala with gedit.

http://www.valaide.org/doku.php : vala

https://launchpad.net/valable : Eclipse

http://code.google.com/p/vtg/ : Gedit

http://abderrahim.arablug.org/blog/ : anjuta

plan9assembler
  • 2,862
  • 1
  • 24
  • 13
3

I think the best way to go would be first learn gtkmm! After you are done with the basics of gtkmm, GTK+ should be fairly straightforward to learn(provided you know C and are comfortable with pointers).

In case you don't know C, you can learn it quickly by reading The C Programming Language by Dennis Ritchie

I recommend you to learn gtkmm first because it is specially designed for C++, which is somewhat similar to C# since both are Object Oriented, so gtkmm will be relatively easy to learn first than GTK+.

After gtkmm, you can move on to GTK+

Most of the open source companies use GTK+ rather than gtkmm, so GTK+ is worthwile to learn!

kpsfoo
  • 392
  • 4
  • 18
  • 1
    When taking the plunge to teach myself GUI programming, `gtkmm` was the 1st GUI library that I looked at where I didn't immediately recoil in baffled horror (GTK+ included). The fact that I was teaching myself C++11 at the time, and `gtkmm` makes a concerted effort to use a modern language version (again, unlike GTK+) added a lot to the appeal. And yeah, like you said, once I'd used `gtkmm` to get a handle on how GUI models work - and due to a necessity of reporting a few little bugs in GTK+ ;-) - the latter made infinitely more sense when I looked at it again. Now, I almost appreciate it! ;-) – underscore_d Oct 30 '16 at 16:50
3

Have you looked at Qt?
It's nice C++ design, cross platform and LGPL

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • Yeah, I kinda like it but I don't (if that makes sense). – Kredns Mar 15 '09 at 19:09
  • 8
    Yeah I know what you mean. I mean not really –  Mar 15 '09 at 19:20
  • But Honestly what I mean is I'm running ubuntu not kubuntu. So I'm using GNOME and not KDE. I tried Qt it doesn't exactly look right. – Kredns Mar 15 '09 at 19:22
  • 1
    Qt != KDE Things that "do not look rihgt" are theme-related. Try changing the Qt theme to get something eye-candy. – Anonymous Mar 15 '09 at 19:35
  • @Anonymous: I would rather it work right out of the box than have to change it. I'm lazy. – Kredns Mar 15 '09 at 19:39
  • If you really want to write gnomish apps, GTK is your only choice. There _is_ QGtkStyle acting as a bridge, but it's not the same. And in that case, GTKmm is much more sane. I don't wish for anyone to land in GObject hell. –  Mar 15 '09 at 21:00
  • The OP didn't say they wanted to wite gnome apps, they said gui apps. Gnome/GTK isn't the only choice – Martin Beckett Mar 15 '09 at 21:22
  • 1
    I disagree on the 'nice c++ design'. It abuses macros and gets in the way with other libs (boost::signals for example). Just as a side comment. – David Rodríguez - dribeas Mar 15 '09 at 23:25
  • You mean 'LGPL' meaning library or lesser GPL so you can link it with your propriety code and still keep it closed-source. (Same license as GTK) – Roalt Mar 16 '09 at 06:50
  • Well nice C++ compared to MFC! Sorry about the typo, I think all possible aconyms ending in L are now the name of some licence! – Martin Beckett Mar 16 '09 at 17:23
  • 1
    It was the non-standard extensions to C++ that Qt used that pushed me toward gtkmm. I'd rather have something that doesn't need to use those to "just work". – Kevin Anderson Aug 10 '11 at 21:28
  • 1
    @DavidRodríguez-dribeas You can simply `CONFIG += no_keywords` and the interferences with other libraries, like boost::signals or the gnome signals are gone... – Jacob Krieg May 02 '15 at 00:49