views:

504

answers:

10

Hello,

I currently have a C++ backend that I need to connect with a GUI, and since I've never built a GUI before, I was confused on where to start.

I'm comfortable writing code in C++ and Java, so I'd prefer my GUI to be in one of those languages. Also, the GUI has to be reasonably OS independent over Windows and Linux (and hopefully, hence Macs).

Now I understand that if I use Java to do it, I'll need some wrappers to do it - but I've also heard (strictly second hand) that writing a GUI in C++ is a pain.

I don't want to rewrite too much of my backend code in Java (who does??) and I was hoping for input on:

  • Does either language offer serious advantages/disadvantages compared to the other?
  • How serious is the wrapping issue, and how much rewriting would come in if I used Java.
  • Are there any specific resources I should look at that people think would be relevant?

Thanks and Cheers All :)

+28  A: 

Have a look at Qt.

In my experience communicating between two different language runtimes is always challenging. If you have a non-trivial application to build the following often pose challenges:-

  • Error Handling.
  • Memory Management.
  • Multithreading and Synchronization Semantics.

Apart from increasing one level of indirection due to wrappers, it requires a lot of thinking like circumstances where you need to pass data structures across GUI and backend etc.

For example:- Consider passing a Java String from GUI to backend C++. Essentially, we have to extract the characters from a Java String object and make them available to the C++ developer without leaking the memory which holds them. This is an example of a basic problem (there are other aspects too like the encoding in which the characters are to be returned).

Abhay
+2  A: 

I can't say much about coupling Java and C++, but I suggest you have a look at Qt. It's a C++ library for a lot of things, like file and network access, but it's most famous for GUI development. It also has a nice IDE were you can build your GUI with drag-and-drop. I would also say that Qt is as OS-agnostic as it gets for GUI libraries.

mooware
+1  A: 

Writing a GUI in C++ is no more of a pain than doing it in Java.

There are numerous cross-platform GUI libraries. GTK, gtk--, FoX, WX, etc. I would not recommend Qt since it's not really C++ (uses an extended version of the language requiring a special preprocessor before compilation). Plus it costs a fortune if you don't want to give away your product.

BTW, that's not how the word "hence" is used.

Noah Roberts
*Many* commercial applications can use Qt via the LGPL at no cost.
Jerry Coffin
+1 for not recommending Qt. I would also mention Ultimate++, gtkmm and FLTK among portable C++ libraries.
Nemanja Trifunovic
I'll check out GTK - thanks :) As for the word 'Hence', I used it in the context that my GUI working in Linux would translate to Mac OS X. I didn't really state the OSX part clearly, I'll admit, but I've seen hence used in published text similarly. But then again, interpretations make a language...
sparkFinder
If the choice is between Java and C++, the argument that "Qt isn't really C++" doesn't count since Java neither is "really C++". Also the (free) LGPL version of Qt is very well applicable to most commercial products. Not to speak of the Qt designer which beats most open source GUI designers ...
MartinStettner
I'm a huge fan of GTK+, but I think that Qt is worth recommendation in this case.
el.pescado
A: 
  1. Wrapping is not rewriting, it's just an adapter to make both languages meet. It is straight-forward
  2. As you don't seem to be fixed on a language, I would choose a .NET Gui (With C++ CLR) you'll have a GUI that is machine independent and you can easily communicate with your existing code.

For beginners WinForms is maybe easier, but try to use WPF, it's the most modern variant for GUI development in the .NET world.

Personnally I would use C#/.NET for the GUI and use a C++ CLR wrapper DLL. But it is not the only solution.

Under Linux, the best .NET implementation is MONO. For all WinForms apps I have developped (they are not the craziest ones), they ran without change. With C++/Qt you will have to recompile for each target OS.

jdehaan
He asked for platform independant. Windoze, Linux and Mac. I think that counts out C#/.NET.
bradgonesurfing
The Mono version of GTK might allow it.
Noah Roberts
One downvote, I knew it was a stupid idea to make a .NET suggestion to C++ fanatics :-D
jdehaan
A C# answer doesn't belong in a C++ question and your answer only targets a subset of those the OP wants.
Noah Roberts
Maybe the OP didn't seem very tied to a specific language and probably did not consider this - in my opinion valid - alternative into his decision yet. If he is not sure to use Java or C++ why not also check for a .NET solution with C++/CLR (not necessarily C#). According to me the downvotes are not justified. This is a community with a democratic voting system. Let's see what happens over time.
jdehaan
WPF is not cross-platform. Also, the recompilation part is completely irrelevant. If we remove all the chaff from your answer we get WinForms, which is a decent alternative, but I personally wouldn't pick it because Mono is still much less popular than either Java or C++.
rpg
A: 

Do not use Java for creating GUIs unless platform independence is a must. The user experience will be sluggish and interoperability to C++ will be a pain.

For creating native GUI in C++, you can either use GTKmm along with a library like Boost or QT. Additionally, these libraries are available for most platforms (GNU/Linux, Windows, OS X) so your application can be recompiled anywhere.

edit: use GLADE for quickly creating GUI and fill in the signal slots with C++ code in GTKmm.

Manas
So how would say, using gtkmm in C++ pose platform independence problems?
sparkFinder
gtkmm wouldn't pose any platform independence problems except for distribution, just that you need to recompile your application for each platform.using Java, you could just give away jar files.
Manas
Using Java, you just give away jar files along with compiled for each platform C++ backend - in this case.
el.pescado
The "sluggish" claim is false.
Andy Thomas-Cramer
@Andy: Agreed; "sluggish" is an understatement. ;-)
James McNellis
It is definitely possible to make responsive GUIs in Java. If you don't like Swing, SWT is pretty good, and there are bindings for Qt as well (haven't tried the latter personally). That said, writing GUIs in Java always leaves me with a bad taste in the mouth.
ZoFreX
this is __FULL__ of __FUD__, not all Java GUI's are "sluggish", just poorly written ones. And with a clean C++ API, SWIG would make short work of exposing a C++ API to Java.
fuzzy lollipop
I do love Java, have been actively using it for 6 years - while not trying to start a religious war, all I am saying is if most of your code is already in C++, I don't see a point in using Java just for creating GUI. Except for platform indep, Java doesn't offer any clear advantages over C++, auto garbage collection is overrated and is partly responsible for the sluggishness."I've also heard (strictly second hand) that writing a GUI in C++ is a pain." -- if you use the right tools, it is no pain at all.@fuzzy lollipop "this is FULL of FUD" -- thats what she said ;-)
Manas
Well then... the Java fans are welcome to point out ONE cross-platform Java UI that both looks good and is responsive. Good luck finding one.
rpg
@rpg - Eclipse and Azureus are significant cross platform applications written in Java. I would claim both look good or at least no-less ugly than most tools. Eclipse can be sluggish and consumes large amounts of memory but I would say that is not primarily the fault of the GUI. Both use SWT, but I think for most applications Swing is now perfectly adequate. It's look-and-feel and performance have improved massively since it first appeared in 1997. I think dismissing it out of hand, the way many are doing, constitutes FUD. However that doesn't mean I'm advocating Java for the given issue.
George Hawkins
A: 

Qt is the winner.

Conradaroma
A: 

Depending on your needs, a simple web interface might be the simplest when you have no existing frontend code. Embed a tiny web server in your application and open a browser on "http://localhost:12345" (or what port you end up using).

Thorbjørn Ravn Andersen
downvote? For suggesting a web gui? Oh well :)
Thorbjørn Ravn Andersen
Fanatics, I tell you ;-). This is also a good alternative, how many apps are web enabled today... I think they read Web and that scared the downvoters. I put you again at level 0. :-)
jdehaan
+4  A: 

You say you already know C++ and Java, and that you never did a GUI before. That means:

  • no matter if you go for a Java GUI or a C++ GUI, you will need to learn how to handle the GUI framework
  • if you chose Java, you also need to learn how to interface between the two languages

So staying in C++ saves you one thing to learn. Well, it's always a good idea to learn something, but might be a bad idea to learn two new concepts at the same time. Anyway, the learning might be the smaller burden, I guess there is a lot of actual work involed, even when you use tools like SWIG.

You might want to know if writing a GUI in Java or doing it in C++ is easier. It depends on the chosen Framework. For Java, you have AWT and Swing which are part of the default Java distribution, and then there is SWT which is used by Eclipse, for example. For C++, there are many toolkits, with Qt, GTK and wxWidgets being the most popular ones, and all three support every major platform. Most of those "C++" GUI toolkits also have a Java binding or even a Java port, so you could use them with Java as well.

So far I've used Swing, Qt and a few others which don't help in your situation (The UI thingy that came with Borland C++ Builder and WinForms on .NET). Basically, the concepts are the same for all those frameworks, and I found none of them being harder or easier than the other. The only exception maybe Java, because I never got those LayoutManagers to work, even though the other toolkits have equivalents to LayoutManagers that are easy to master. But maybe thats just me.

People also will tell you that Java GUIs are always ugly and don't fit the host system. Well, most Java GUIs really are, but IMHO thats not because of Java, but because of bad programming. It takes two lines of code to let a Swing app adapt to the look and feel of the OS, and most programmers simply don't put enough effort into their Java GUIs to copy and paste those two lines... you can imagine how much they care about the rest of their GUI design.

For your current situation, I would recommend a C++ GUI, but if you know how your future plans look like, and if you know you will doing Java GUIs for the rest of your life, then it's probably ok to start that now and take the extra effort of .

And if you chose C++ for the GUI, people will tell you all kind of things to pull you in any direction. All of the three big portable frameworks have their pros and their cons, but I don't believe there is any single best or worst one among them. I'd recommend Qt simply because I already used it - but if I'd happten to have used GTK or wxWidgets instead, I'd probably suggest that.

Brian Schimmel
A: 

You did not mention the richness of the interaction between front and back ends, which would weight the importance of the existing language in your decision.

I've worked with Qt, Swing and SWT, and typically used both C++ and Java code with all of these toolkits. The interaction between languages can add additional cost/risk. However, sometimes that cost is warranted given other benefits.

If for any reason you choose a Java front end, look at JNA and SWIG.

Andy Thomas-Cramer
A: 

Let's all be honest here. C++ just ain't on the map when it comes to portable GUIs.

Java has a consistent, portable, widely used, thoroughly documented, mature GUI toolkit. C++ has a bunch of half-assed OSS libraries that barely work, none are truly portable, plus some expensive commercial libraries that don't work on all targets they claim to, work spotty on the remaining targets, and invert control so that you are stuck in their weird framework.

Unless you need C++ for other reasons (of which there are plenty), choose Java for the GUI. The cross-over coding is trivial for someone who knows both languages, but it can get messy to manage, so you'll want to minimize the native interface as best you can. My advice here is to make a pact with your team that you will never attempt to hold pointers (or references) across the interface. It just gets messier if you do, and no debugger can save you when the lines get tangled. Instead, use integer or string keys and pass them across the native interface.

John
While you may be honest, you are misinformed. The best and most popular UI libraries are written in either C, C++ or C#. Java doesn't look good even on ONE platform, never mind cross platform.
rpg
The problem is: you are both right (John and rpg). C++ UI libraries are not very good, and Java UI does not look nice on any platform.
Nemanja Trifunovic
It's not a matter of being informed. It's a matter of being experienced. :) I have professional experience with just about every GUI toolkit ever written, portable or native. Java's is the best out there in terms of toolkit. In terms of how it LOOKS... well, you have a point. Java's GUI looks better than arbitrary web apps, and better than the lesser toolkits like Tk, FLTK, etc., but worse the native desktop toolkits (.NET, Cocoa). It's by no means perfect. Though I dislike Java in general, I have to give credit where credit is due, and admit that Java's GUI toolkit is pretty solid.
John