Qt C++ How to create a toolbar from another file (How to declare MainWindow in a seperate headerfile)

I try from a seperate Headerfile to create a toolbar with buttons in MainWindow. I get the error
"MainWindow was not declared in this scope".
I am trying to organise my code in different files, for having a better view of it, and to have "modules" for later (re)use.  I am not even sure which approach is correct for that.

This is what I have:

QToolBar *toolbar = new QToolBar(MainWindow);

But get the error above.

Is there an expert who can help me further with this.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Basically in the above code, you are trying to create a toolbar by passing the mainwindow instance (object or pointer) as input to the constructor of the QToolBar.
Now the error states that in the file or function where you are trying to create the QToolBar, there is no instance of main window by name MainWindow.
Option 1> Somehow this piece of code needs access to MainWindow which would be created at the start of your App, so pass it as input to your function which is creating toolbar. Also in the file that creates toolbar, you will have to include #include <QMainWindow>.
Option 2> If your App only one window then , you could even do something like below
MainWindow * win = (MainWindow *) qApp::activeWindow();
QToolBar *toolbar = new QToolBar(win );

Open in new window

However if you share the sample code files, it would be easier to suggest other alternatives.
you may consider to create the toolbar as a member of the main window class. then the creation would look like

bool MyDialog::createToolbar()
      myToolBarPtr = new QToolBar(this); // myToolBarPtr declared as QToolBar * in MyDialog
      return (myToolBarPtr  != NULL);

Open in new window

jacobjakubAuthor Commented:
Hello again.

I have been trying, but simply can not get this working.
I have started from blank, but can't get either of your examples running.
Karrtik, you ask if I can share the sample code files.
I hope that it is okay that I upload four small files to this thread.

Can you help me further using this example?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Karrtik IyerSoftware ArchitectCommented:
Hi Jacob,
The main issue with your code are mentioned below: (It was compiling without any issues but toolbar was not appearing)
1> The ToolBarCode function was never called from MainWindow.

Other than that there were code level design issues, which I have corrected, major are:
2> ToolBarCode function has been renamed to an appropriate function called CreateToolBar that returns bool (true/false).
3> Also this new function is now a public member function of your class Things, it is better to rename this class as ToolBarManager. Earlier the function ToolBarCode was a global function, which defeated the purpose of your class Things. I did not rename this class since I thought you would not understand it if I renamed it.
4> Now in MainWindow, there is a member variable to store pointer of your class things.
5> This things class pointer is allocated in the constructor of MainWindow.
6>  Then after the set up Ui call in the constructor of MainWindow, now I have called the function CreateToolBar
7> In the destructor of MainWindow I have destroyed the pointer of things class which was constructed in step 5.
8> Also your class things constructor now takes an argument to get the MainWindow with which the tool bar should be associated as an parameter
9> This mainwindow parameter is stored as class member variable in class Things. (_mainWnd)
10> I have done minor modifications to code loading resources since I did not have the PNG files that you had used, hence I downloaded some JPEG images and used them.
I have uploaded the modified code and wherever I have made changes have been prefixed with //Karrtik.
Hope this helps. I have attached a screenshot of your program and how it looks.
P.S: Please rename the attached text file to ToolBarSample.tar.gz file and then you can extract it and open the QT project directly to run it.
Thanks and regards,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karrtik IyerSoftware ArchitectCommented:
Also in future, when you open QT creator, on left side bar, first you shall see an icon QT welcome icon.
if you click that , you shall see a button called examples, once you click examples, on top right side there is an option to search, enter text toolbar in it, you shall get relevant samples, and you can click the relevant one and open that project and see its code + execute it.
This shall give you a good introduction on how code is arranged in a normal QT application.
Also there is a button called Tutorials, in that step by step tutorials are given for various topics.
Hope this is useful.
jacobjakubAuthor Commented:
Hello Karrtik

Thanks for your answer. You have done a great job
I will look at it ASAP.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.