How to Add a Toolbar to a Window in a Qt Widget Application in C++


C++


In this article, we show how to add a toolbar to a window in a Qt widget application in C++.

So when we are dealing with windows in the Qt software, we are referring to main windows. We create a window of the QMainWindow application, which allows us to work on a main window. This window can have a menu which contains several different menu items, such as File, Edit, Help, etc. This window may also have a toolbar, which is a common feature of many software applications. A toolbar gives easy access to aspects of the software usually through icons.

Below is an example of a toolbar created with the Qt software.

Main window with a toolbar in a Qt widget in C++

So you can see the toolbar underneath the Menu of the software. This toolbar contains 3 icons: copy, paste, and cut.

By having a toolbar there, the user has easier access to these features than what it would be if the toolbar wasn't there. The user would have to do more to access these features.

So how do we add this toolbar to a main window in a Qt widget application?

So the first thing we have to do is create a main window by creating a program of the QMainWindow base class.

Then we need to create the File menu items. A toolbar is simply file menu items that are made more easily accessible.

Once we have these menu items, what is normally done next is to link the icons to the menu items. For copy functionality, this would be two sheets of paper. For paste functionality, this would be a clipboard. For cut functionality, this would be scissors.

So now all you have to do is go to the main window and right click on the top underneath the menu and click 'Add Toolbar' which should be the first selection of the drop-down list.

Now you can add any menu item elements to this toolbar by dragging it with the mouse and its icon will appear on the toolbar.

And this is how you can add a toolbar to a main window in a Qt widget in C++.


Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...