tags:

views:

1233

answers:

10

This question is extremely basic, and I feel stupid for asking it. How do I create a GUI in C++? All of my C++ programs so far have been CLI, and the only other language I have experience with is PHP, which doesn't support it, which is why I don't know how it works.

By the way, I apologize if GUI is the wrong word here. I'm not sure if it's correct.

Edit: Basically, my question is how. Where do I start? How do I even create the GUI? I've never had any formal programming education, so I have absolutely no idea.

+5  A: 

QT or GTK are the two most common approaches. Explain what you want better and get a better answer.

scragar
Gtk is *not* a C++ library (though a variant called Gtk++ exists).Qt is what I prefer.
Dirk Eddelbuettel
There are wrappers such as GTKmm. So scragar's proposal is okay. However, I don't like the personal tone of this answer.
vog
@ vog - I wrote that answer in a hurry, I was intending to come back to it when I had more time, but I never got around to it.It's kind of been completely overshot by other answers now, so there is no point in me editing it, but I didn't want to sound offensive or anything, it was purely written that way for speed.
scragar
If you don't want to sound offensive, you should delete at least the second sentence, even if your answer isn't on top.
vog
+21  A: 
vog
Win API is not a library....
KTC
Thanks! I fixed that.
vog
Go with Qt, follow the tutorials, become a GUI wizard!
Skilldrick
Personally, I think programming to the Win32 API is beautiful. I likes it much more than using Qt, and that in and of itself is a benefit. Maybe that's just me, though.
mrduclaw
My personal experience is exactly the opposite. The Win32 API caused more hassle for me than any other API.
vog
+5  A: 

Since I've already been where you are right now, I think I can "answer" you.

The fact is there is no easy way to make a GUI. GUI's are highly dependent on platform and OS specific code, that's why you should start reading your target platform/OS documentation on window management APIs. The good thing is: there are plenty of libraries that address these limitations and abstract archtecture differences into a single multi-platform API. Those suggested before, GTK and QT, are some of these libraries.

But even these are a little TOO complicated, since lots of new concepts, data types, namespaces and classes are introduced, all at once. For this reason, they use to come bundled with some GUI WYSIWYG editor. They pretty much make programming software with GUIs possible.

To sum it up, there are also non free "environments" for GUI development such as Visual Studio from Microsoft. For those with Delphi experience backgrounds, Visual Studio may be more familiar. There are also free alternatives to the full Visual Studio environment supplied from Microsoft: Visual Studio Express, which is more than enough for starting on GUI development.

Spidey
dependent, not dependable.
patros
Visual C++ comes in a "free" express version. See http://stackoverflow.com/questions/1186017/how-do-i-build-a-gui-in-c/1186225#1186225
Justicle
Both fixed. Thank you.
Spidey
+4  A: 

I found a website with a "simple" tutorial: http://www.winprog.org/tutorial/start.html

ZippyV
This introduces the Win32 API, a very low level approach to GUI programming that ties you to the Windows platform. I would not recommend that.
vog
It's a valid option. I agree that it might not be the best place to start, but it doesn't deserve a down vote.
Emil H
I know this is not the ideal solution but it does help to learn how the gui system in Windows works. I've read this tutorial a couple of years ago and now I know what the message loop is and how it works. When working with winforms in .net this knowledge might come in handy.
ZippyV
If you are going to write desktop applications there is no substitute for knowing how the OS really renders and interacts with the user. If you just use a library without understanding the fundamentals then you'll never really understand what is going on.Learning a little bit of low level win32 gui programming will be time well spent.
Jim In Texas
@Jim In Texas: I agree, but this "bit of low level" should be learned _after_ the basics, so I still find that recommendation inappropriate for beginners.
vog
+4  A: 

Essentially an operating system's windowing system exposes some api calls that you can performm to do jobs like create a window, or put a button on the window. Basically you get a suite of header files and you can call functions in those imported libraries, just like you'd do with stdlib and printf.

Each operating system comes with it's own gui toolkit, suite of header files, and api calls, and their own way of doing things. There are also cross platform toolkits like gtk, qt, and wx widgets that help you build programs that work anywhere. They achieve this by having the same api calls on each platform, but a different implementation for those api functions that call down to the native OS api calls.

One thing they'll all have in common, which will be different from a CLI program, is something called an event loop. The basic idea there is somewhat complicated, and difficult to compress, but in essense it means that not a hell of a lot is going in in your main class/main function, except:

check the event queue if there's any new events
if there is, dispatch those events to appropriate handlers
when you're done, yield control back to the operating system (usually with some kind of special "sleep" or "select" or "yield" function call)
then the yield function will return when the operating system is done, and you have another go around the loop.

There are plenty of resources about event based programming. If you have any experience with javascript, it's the same basic idea, except that you, the scripter have no access or control over the event loop itself, or what events there are, your only job is to write and register handlers.

edit: Oh also, I forgot to mention that gui programming is incredibly complicated and difficult, in general. If you have the option, it's actually much easier to just integrate an embedded webserver into your program and have an html/web based interface. The one exception that i've encountered is Apple's cocoa+xcode+interface builder + tutorials that make it easily the most approachable environment for people new to gui programming that I've seen.

Breton
+7  A: 

Given the comment of "say Windows XP as an example", then your options are:

  • Interact directly with the operating system via its API, which for Microsoft Windows is surprise surprise call Windows API. The definitive reference for the WinAPI is Microsoft's MSDN website. A popular online beginner tutorial for that is theForger's Win32 API Programming Tutorial. The classic book for that is Charles Petzold's Programming Windows, 5th Edition.

  • Use a platform (both in terms of OS and compiler) specific library such as MFC, which wraps the WinAPI into C++ class. The reference for that is again MSDN. A classic book for that is Jeff Prosise's Programming Windows with MFC, 2nd Edition. If you are using say CodeGear C++ Builder, then the option here is VCL.

  • Use a cross platform library such as GTK+ (C++ wrapper: gtkmm), Qt, wxWidgets, or FLTK that wrap the specific OS's API. The advantages with these are that in general, your program could been compiled for different OS without having to change the source codes. As have already been mentioned, they each have its own strengths and weaknesses. One consideration when selecting which one to use is its license. For the examples given, GTK+ & gtkmm is license under LGPL, Qt is under various licenses including proprietary option, wxWidgets is under its own wxWindows Licence (with a rename to wxWidgets Licence), and FLTK is under LGPL with exception. For reference, tutorial, and or books, refer to each one's website for details.

KTC
MFC is evil. I'd prefer to use WIN API if I had no other choice.
the_drow
+1  A: 

Its easy to create a .NET Windows GUI in C++.

See the following tutorial from MSDN. You can download everything you need (Visual C++ Express) for free.

Of course you tie yourself to .NET, but if you're just playing around or only need a Windows app you'll be fine (most people still have Windows...for now).

Justicle
A: 

Qt is the best.

ufukgun
+5  A: 
Kirill V. Lyadvinsky
A: 

I use FLTK because QT are not free. I don't choose wxWidgets because my first test with a simple hello world program produce an executable of 24Mb, fltk 0.8MB...

pernecker