views:

88

answers:

2

Suppose I have some function, say in C++

int f() {
    // do some stuff
}

How do I connect it to a button in some application? It does not need to be a web application per se, it can be a normal desktop application (not a big difference, I guess).

What all do I need to do to get this function to be called when I press a button in some application?


I guess the question is too vague to be answerable as such.

I am reading the Head First Design Patterns book, and it has a lot of code samples for some dummy apps. Of course all that in is Java. But I am more familiar with C++. So, while reading, I was just wondering how I could possibly get my code connected to some app. What is the general procedure?

For concreteness, how do I connect it to a button on a webpage?

+3  A: 

Yes, there's actually quite a large difference between a desktop application and a web application in this respect -- at least usually, though there are exceptions (e.g., Microsoft's Silverlight framework lets you do some things the same in a web app as you can in a desktop app).

How you connect things up on a desktop app varies widely. Raw Windows applications frequently include a giant switch statement based on the value of a "message". MFC uses what it calls Message Maps. Qt uses what it calls signals and slots.

It's probably worth mentioning, however, that in a typical case you don't really deal much with this in code. Most people let an IDE handle most of the details of connecting this up. It'll (typically) produce a skeleton function about like you have above (possibly with even more filled in to do things like calling parent class functions, etc.) so all you normally have to deal with directly is the //do some stuff part.

Edit (in response to edit of question): a web page is probably the last thing you want to deal with right now. There's simply a huge range of variation in how you handle button events and such on web pages. In particular, you frequently get a combination of actions that happen inside the browser (e.g., in Javascript) and things that happen on the server (e.g., using AJAX). You can invoke functions on either the client, the server, or both, in response to a particular button click, etc., so this is actually quite a complex scenario in general.

While there's certainly variation between windowing systems and frameworks in desktop applications, at least the variation is over a rather smaller range.

Jerry Coffin
@Jerry Coffin: thanks! I was expecting something very simple when I originally posted the question. A lot left to explore!
Lazer
+1  A: 

Basically, what you're noticing is that C++ doesn't come with a standard UI. Java does. (It in fact has a number of standard UI's such as Swing, AWT, SWT). This makes it hard to say how you'd connect a button.

Now, the problem is a lot easier if you choose a UI library to use in combination with C++. Qt is a popular choice, because it is easy to use, portable and effective.

With Qt, the answer has two parts: You declare int f() in the public slots: part of your dialog class, and in your dialog's constructor you connect the button's clicked signal to the int f() slot.

MSalters