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.
Thanks.
C++
Last Comment
Jacob
8/22/2022 - Mon
Karrtik Iyer
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
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.
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.
Thanks,
karrtik
Jacob
ASKER
Hello Karrtik
Thanks for your answer. You have done a great job
I will look at it ASAP.
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
Open in new window
However if you share the sample code files, it would be easier to suggest other alternatives.Thanks,
Karrtik