Create Windows Application Using Dev C%2b%2b

Posted on by

This tutorial is an introduction to Qt programming and will teach you how to create an application with Qt and C++. Full project and source code are provided.

I have been messing around with making a windows application in Dev-C I wanted to make it in a single source file, rather than a project to see if it worked. It did, other than the fact that I got the windows app, AND a DOS prompt behind it. Is there anyway to remove the DOS prompt? I will include the code, so that you can see what I mean. What is Dev-C? Dev-C, developed by Bloodshed Software, is a fully featured graphical IDE (Integrated Development Environment), which is able to create Windows or console-based C/C programs using the MinGW compiler system. I am using visual studio 2017 and windows 10 operating system. I am trying to create windows desktop application but I have no idea how to create I followed this page of Microsoft https://docs. C/CX is Microsoft’s language extension for C that allows it to target the Windows Runtime that was released with Windows 8 and continues to evolve with Windows 10. The Windows Runtime Library is a template library that provides a low-level way to author and use Windows Runtime components. Create application for windows, mac and other oses (i prefer to create my application without using any other software and i want to know how you can create GUI application, 0-100 with c coding) In a sense, your goals are somewhat mutually exclusive, and like many things in life, you have to make hard choices.

Qt

Qt is a cross-platform framework and a collection of tools for creating applications which can run on desktop, embedded and mobile operating systems.

Develop C Windows, Linux, Mobile applications and games using Azure cloud services in a powerful, rich development environment using Visual Studio. Try Visual Studio Visual Studio Code.

If you need to create an application with a GUI in C++ you should definitely consider Qt for it.

A simple example application

This tutorial will guide you through the creation of a basic window with a menu bar, a menu and a status bar. The final result while look like this:

Nothing too fancy, but these are the basic elements of most desktop applications and you can use this code as starting template for your projects.

When working with Qt, especially when developing cross-platform software, it’s recommended to use Qt Creator as IDE. The main reason is that Qt Creator is the official Qt IDE and it offers full integration with the framework and all the other development tools. Using other IDEs is possible, but I don’t recommend it and for this tutorial I will use Qt Creator.

Creating a new Qt project

The first step is to create a new project in Qt Creator from one of the available templates:

For a graphical application you want to select the Qt Widgets Application template.

The second step is selecting a name for your project and a location where to save it. For my project I decided to use “BasicApplication” for the name, but obviously you can use anything you want.

The third step is selecting the kit(s) for building your application.

Qt Creator kits are groups of settings used for building and running projects for different platforms.

I am developing on Linux so I am going for the kit Desktop Qt 5.7.0 GCC 64bit, but you might see different kits if developing on/for different platforms.

The fourth step is defining the details of the initial main window class that will be generated by the template.

You can leave everything as it is, but make sure to uncheck the Generate form field. The form file is used by the integrated visual interface editor Qt Designer. We won’t need it because we will be coding the GUI from scratch.

In the final step of the project creation you will get a summary of what defined in the previous steps.

Here it’s also possible to set a version control system to use for the project, but feel free to not set any for now.

Clicking on the button Finish will create the project and generate 3 C++ files (main.cpp, MainWindow.h and MainWindow.cpp) and a project file used by Qt Creator and qmake (the Qt building tool) to build the project.

The main

Create Windows Application Using Dev C 2b 2b Program

The main function initialises the application, then creates and opens the main window:

The last line of code launches the Qt application and waits for it to return an exit code before terminating the program.

The main window class

The main window is defined in the header file MainWindow.h and it inherits from QMainWindow:

It’s important to include the Q_OBJECT macro in all the classes that are inheriting from QObject.

In this simple example the constructor is the only public function:

Finally some private slots are declared:

Slots are handlers used to do something when an event happens. They will be explained in more detail later.

The main window implementation

All the MainWindow methods, including the slots, are implemented in MainWindow.cpp.

Most of the code will be placed in the constructor because it’s there where the elements of the window are created.

Window settings

We can start with some basic settings:

The function setWindowTitle sets the title of the window, whereas setMinimumSize sets the minimum size allowed when resizing it.

Adding a menu bar

Most applications have a menu bar with menus. Creating one in Qt is pretty simple:

Once the menu bar is created you need to set it for the main window calling the setMenuBar function.

After creating and setting the menu bar it’s time to create a first menu:

The “&” before “File” will enable the keyboard shortcut ALT+F to open the menu.

Now that we have a menu we can populate it with entries, which are QAction objects:

As you can see creating an entry and adding it to a menu is pretty straightforward.

What’s more interesting is connecting menu entries to functions so that something happens when they are clicked. This is achieved using the Qt function connect which in its standard format takes 4 parameters:

  1. sender – the element generating an event
  2. signal – the event generated by the sender
  3. receiver – the element receiving the event
  4. slot – the handler to execute when an event is received

In the case of the first menu entry we have the following scenario

  1. sender : action (the menu entry)
  2. signal : &QAction::triggered (a menu entry has been clicked)
  3. receiver : this (the event is handled by the MainWindow)
  4. slot : &MainWindow::OnFileNew (when the menu entry is clicked this function is called)

When building a menu it’s also possible to add a horizontal line to to separate logic blocks. It only takes one line of code:

The final entry of the File menu is going to be one to exit from the application:

This entry is connected to the function close defined in QMainWindow which, as expected, will close the window when the menu entry is clicked.

Adding a status bar

A status bar is useful to show messages to the user. Creating one is pretty simple:

As for the menu bar, it’s important to remember to set the active status bar calling the setStatusBar function.

Implementing the slots

In this simple example all the slot functions do is setting a message to show in the status bar like in the following code:

Obviously this is just an example and a real application will do something more useful with them.

Adding content to the window

Once the basic elements of the window have been created it’s time to add some content.

This is done by calling the function setCentralWidget and passing it a QWidget which contains the graphic elements the application needs to show and handle.

Create windows application using dev c 2b 2b program

I decided to not add any central widget in my code to keep this example as simple as possible, but obviously you’ll need to do that in your application.

Source code

The full source code of this tutorial is available on GitHub and released under the Unlicense license.

References

All the Qt widgets discussed in this tutorial are documented in detail in the Qt Widgets C++ Classes section of the Qt documentation.

To learn more about Qt Creator check out the online manual.

Conclusion

This was a simple tutorial to teach you how to create an application with Qt and C++. If you have any question about the code explained here feel free to leave a comment.

If you found it helpful please feel free to share it on social media using the share buttons below.

Subscribe

Don’t forget to subscribe to the blog newsletter to get notified of future posts.

You can also get updates following me on Google+, LinkedIn and Twitter.


create application for windows, mac and other oses (i prefer to create my application without using any other software and i want to know how you can create GUI application, 0-100 with c++ coding)

In a sense, your goals are somewhat mutually exclusive, and like many things in life, you have to make hard choices. Here's what I mean....
There isn't a standard C++ way of creating apps with GUIs. GUIs aren't part of the C or C++ standards. So to create GUIs with C or C++ you absolutely need platform specific libraries or frameworks.
Having said that, all operating systems have platform specific low level APIs (Application Programming Interfaces) for creating GUI apps. The keywords above are 'platform specific'. You could sink a lot of time into learning the low level Windows Api (Furry Guy posted some code above), but it would be useless to you for creating Mac, Android, Linux, or any other OS app.
The answer to that would be the cross language frameworks which provide a high level abstraction layer which you code against, and then the framework generates code for each specific operating system. In that way you'll be using the C++ language, but its use would be somewhat in conflict with your other goal of using a minimal of framework or library code - hense the 'hard choices' I mentioned at first.

Create Windows Application Using Dev C 2b 2b Tutorial

The hard choice I've personally made is to mostly limit myself to Windows and write low level Api code which runs close to 'bare metal', i.e., its not being filtered down through piles of higher level 'abstraction layers' of framework code. But it definitely won't run on Mac or Linux.