C++ Custom GUI Button Question - c++

I am designing a graphical application for which I've decided to write my own menu. I would like this menu to be platform independent. For the time being, my menu will mostly consist of a number of buttons. My issue involves the handling of events when a button is clicked. My dilemma is with a button "knowing" about the context in which it exists. It seems to me that if there is some larger piece of code that creates buttons and handles mouse events, the need for some type of switch statement might arise. The switch statement would have to invoke the appropriate action based on whatever uniquely defined the button that was clicked.
I would like to avoid this switch statement. My first idea was to have each button maintain a function pointer that it uses to blindly initiate the correct action when it is clicked. This would eliminate any button-specific code. Yet, it bugs me that a button should contain any context-specific information (such as a function pointer). I am fairly inexperienced and I am wondering if this is considered bad design. Regardless, how can I design my menu in a manner which eliminates the need for some type of switch statement and is considered good OOP design? I would like to hear what your preferred solutions are.
Thanks in advance!

You take a look at libraries :
SPTK (Simply Powerful Toolkit) is a cross-platform toolkit that provides a set of C++ classes for fast and easy application development. It provides GUI components that use FLTK, and features database support with seamless connection to GUI components.
eGUI
http://www.codeplex.com/egui
http://torjo.com/egui/
http://msdn.microsoft.com/en-us/magazine/cc534994.aspx
NovaTK is an object-oriented, cross-platform GUI toolkit. One of the focuses of NovaTK is to facilitate rapid development of cross-platform applications requiring fewer lines of code. The event system is based upon a powerful callback mechanism that makes application design simpler, easier to read, and logical.
Ecere Cross Platform GUI Applications
Develop applications once, deploy them on all platforms alongside a lightweight runtime environment.
Introduction to wxWidgets
ClassLib, A C++ class library
libgm - A mini GUI library for Windows

You can take a look at the
Command Pattern.
You can associate a command to a menu item, the command would contain the code to be executed.

Related

How to add custom class processing functionality in Qt

I'm a little new to Qt. I have been programming in C++ for quite a while.
I want to create an application in Qt/C++ just because it's very easy to create the GUIs. I wanted to know that is it possible that I can write a C++ class in native C++ and use it in my Qt application.
I want to write the GUI in Qt and do all processing using my native C++ code rather than Qt/C++. Is it possible to compile a DLL in native C++ and then load and use it in QtC++ application?
I really want to write a GUI application using C++.
There is no such thing as "native C++" as opposed to "Qt/C++", so the question makes little sense. All of core Qt code is compiled native C++, just as any C++ application that uses Qt is.
As JBentley has duly noted:
[You may be] confused because of the code generating tools Qt uses, like moc [or uic]. Those don't mean that C++ compiles natively while Qt doesn't. All those tools do is provide a convenient way to produce a lot of boiler code that the framework relies on, so that the programmer can pretend they don't exist and use things like Qt's slots and signals. The final code which you compile and link is ordinary C++, with calls into the Qt library (also C++) which you've used.
If what you're asking is "am I forced to use Qt classes in all parts of my application", then no - nobody's forcing you to do that. If you already have parts of your application written using boost, or standard C++ library classes, it's fine.
You have to understand that Qt has modular design and provides a lot of non-GUI functionality. You can use the non-gui modules in the non-gui part of your code. It's a fairly clean and general purpose application development framework. It's perfectly fine to use it for various internet server applications, for example.
The following Qt modules are of note for non-gui development:
Core: Core non-graphical classes: containers, event loop, timers, threads, state machines, internationalization, XML, json, file I/O - all of it portable.
Network: Classes to make network programming easier and more portable. Includes secure sockets, HTTP requests, etc.
SQL: Classes for database integration using SQL.
You are able to use any C++ code you would like. I have have actually had questions with the same thought process in my day to day work. First you need to grok the idea that Qt uses normal C++ and special tools to create a robust set of libraries.
Later on come back and you will want to learn more about the items below:
Signals and Slots
This is how events are wired together.
One objects sends a signal and all connected slots get called with the given parameters.
Signals and slots are an example of the Observer Pattern.
Designer Forms
The form designer lets you lay out controls using simple drag and drop operations.
The .ui file gets transformed into C++ code as part of the build process.
Layouts
You can do a lot of things with just the horizontal, vertical and grid layouts
Spacers
You can push things up, down, left, or right using different types of spacers
They look like a spring on the form designer.
Resource Files
Lets you embed graphics and other content directly into the application.
Models, Views and Delegates
Hard to avoid if your have a non trivial UI
There are other things too, but knowledge in these areas will let you make some decent desktop applications. My favorite reference is older, but still a goodie: "C++ GUI Programming with Qt4" http://www.amazon.com/Programming-Edition-Prentice-Software-Development/dp/0132354160
Yes QT4 is "old" and QT5 has been out for some time... but the book provides a solid foundation that you will be able to add information to.

Best Practise for GUI development for vanilla C++ application

I want to know about developing of gui to vanilla C++ application. I have experience in mostly in command line application. My experience in C++ gui till now is cout and cin. I have some experience in WPF (just to mention that I do have some gui experience) I hope this will describe my level of experience with gui. Now,I want to develop an application which needs separate GUI with possibility of 3D display. Of course, one of the choice for GUI API is Qt.Also,after reading lots of stuff on internet, i think code separation would be really helpful in future.
So, here goes my question: Is it possible if I keep my logic as generic as possible (not using winapi or qt in my logic) and make only GUI part API specific (say winapi or qt). Or I will need to add some code in my logic section, say for synchronization between logic and GUI. One can say signal and slot from qt is possible but as far as i know signals and slots are qt specific. they are not standard C++.
In summery,my question is can i make my logic in standard C++ (to stay platform/ framework independent) and only design platform specific GUI? If yes, could you please suggest a link or tutorial or book. A sample code or implementation would be 'a wish come true'. Also, some insights in code separation tactics would be quite helpful.
Regards!!
EDIT::
I will elaborate my problem. I have an application which has separate GUI and Logic section. The gui uses WinAPI and as the communication mode used is windows messages, previous user has created a HWND object in Logic which communicates to GUI. So, there is a HWND in Logic and HWND in GUI. I don't find this approach satisfying. One of the reason is that Logic part will be edited by non-programmers in later stage (not much. just modification of constants or changing implementation of function without changing para or return value). So, I just want to keep logic part in standard format (as much as possible). So once again, could anyone help me in designing business logic in C++ and GUI in any API.
After reading some threads, I found that answering own question is not a bad practice. So I will share the answer I have got.
There is no standard way to build a C++ gui application without using external dependencies. The synchronization between gui and logic part is always framework specific. So, if I want to develop a C++ gui application, I cannot put logic part in standard C++. It must have some code from external framework which will communicate the logic part and gui part. Having said that, I have found my way in by following method. I am going to put my logic part inside a static lib and then I will attach this lib to gui part. It will increase coding in gui section, but it will keep the base functions in standard c++. This way (a function lib in standard C++ and machine operation in framework specific code) will work for me. I hope I am on right track. :)
You can to a large extent write completely separate logic and GUI code in Qt. However you will be able to create a much more useful GUI if you allow the logic and GUI code to interact. Qt has it's own classes for a lot of things (QString, QVector<> etc.) but you're free to ignore these for the most part and use the standard library instead if you'd prefer.
However, I do not think that it is worth trying to separate Qt from the logic code entirely because, as mentioned before, you will be able to build a much better GUI if they interact. For a simple example you could write a very simple Qt GUI with just a window and a button; press that button and some logic code is run. However with more interaction you could use signals and slots to update a progress bar on the GUI to let the user know how far the logic code had got. Also Qt is very portable, allowing you to build your program for Windows, Mac and at least some Linux distros.
Also for your 3D display requirement I recently found myself trying to do a similar thing and found this example very useful - http://qt-project.org/doc/qt-5.0/qtgui/openglwindow.html.
I think that by default, you should aim to separate business logic from presentation (GUI) code. In web development, the most commonly used pattern is MVC, and it's principles apply equally to native applications.
However, this separation might be more difficult to achieve in native applications. Mainly because there are no frameworks such as Symfony, which have already solved these architectural problems, and make it easy to keep UI and business code separate by following the established conventions. I have't used Qt but from what I know it's mainly a GUI toolkit - models, views and controllers are not as well-defined and integrated.
Depending on the nature of the needed interface, a simple OpenGL GUI might suffice. This is what I did for a simple uni project. The project needed to display a shapefile colored according to statistics in an XML file. I created a number of GUI widgets - button, label etc, and a custom map widget which encapsulates "business logic". Perhaps I should have "cleansed" the map widget, making it as generic as possible, and move all business logic to a separate library, but considering the business layer was very thin I thought the added complexity would outweigh the benefits.
Another factor is your skill-set - current, and areas which you want to improve. I was more interested in OpenGL and freeglut, then in learning Qt. If I had known Qt, I would have used that.
I have done some projects with vanilla C++ and a QT GUI. The idea of not mixing any QT code into the pure C++ stuff is always good but takes a lot of fun out of it. Usually you end up with a much more elaborate GUI than you expected and would often like to connect it better/easier to your code. Signals and Slots are a really great way to let the GUI interact with your code, but then you start mixing...
TLDR: Think really hard about why you don't want to mix your code with e.g. QT. It would not meand mixing GUI and Logic, no way, but your life could be a lot easier using QT classes like QStrings, QProcesses or QThreads...
If you manage to do all the interactions using QTs Signal & Slot mechanism, at some point you might even change you native QT GUI to a QT Quick (QML) one, which is highly customizable, has nice looks and animations and whatnot.
This is just my opinion.

How to connect C++ code to an application?

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?
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.
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.

Convert GUI C++ app to a console one

I have a GUI C++ application (Visual Studio 2008) that needs to be converted to a console one.
I don't have any experience in C programming. Mostly I use .NET. Where do I start?
Down-converting a GUI app is major surgery. The programming model is entirely different, a GUI app is event driven. Relying on a message loop to deliver events, processed in message handlers. And typically a bunch of controls that take care of the grunge work of taking input.
Given that you have to completely redesign the app to make it work as a console mode app and that you don't have experience with the language, writing this in a .NET language you have experience with is the best way to get it completed quickly.
Start with refactoring. Make sure that GUI is separated from business logic. Then add another interface to access this business logic: one that uses console, rather than GUI widgets.
Check out ncurses and readline to help you build a rich console application. You can't use them both at once, as I found out, so try ncurses if your application is more oriented toward output/display or will implement single-key interactions (hotkeys), and readline if it's more of a line-at-a-time user input situation.
Create a new project with a main
add your files
here you got a console application doing nothing. It may still create windows, or if you like, hidden windows.
Now it's up to your creativity to tie interface to existing code.
Don't forget to download and use boost::program_options to access command line parameters properly.

Touch friendly GUI in Windows Mobile

I'm porting an audio processing application written in C++ from Windows to Windows Mobile (version 5+). Basically what I need to port is the GUI. The application is quite complicated and the GUI will need to be able to offer a lot of functionality. I would like to create a touch friendly user interface that also looks good. Which basically means that standard WinMo controls are out the window.
I've looked at libraries such as Fluid and they look like something I would like to use. However, as I said I'm developing i C++. Even though it would be possible to only write the GUI part i some .NET language I rather not. My experience with .NET on Windows Mobile is that it doesn't work very well...
Can anyone either suggest a C/C++ touch friendly GUI library for Windows Mobile or some kind of "best practices" document/how-to on how to use the standard Windows Mobile controls in order to make the touch friendly and also work and look well in later versions of Windows Mobile (in particular version 6.5)?
There are two aspects to your question:
Libraries. For this I would take a look at Qt for CE/WinMo. The C++ alternative is MFC.
GUI Design. About Face and Designing Interfaces (J. Tidwell) are a couple of good books.
Also:
make sure that your UI is finger-friendly, I hate it when I have to use a stylus.
keep in mind that on touch screens you can't have tooltips (no mouse over) and you don't have a mouse pointer. WinMo uses click and long click, but the latter is not easily discoverable.
add joystick UI navigation
don't try to cram too many controls on the tiny screen, use tabs or drill-down menus
I don't know any good C++ libs but you could try SlideUI mobile controls (it is in .NET), but you wouldn’t need any specific knowledge to use it and it's available via design time and easy to use.
http://www.devslide.com/products/slideui
Disclosure: I am affiliated with devslide.