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

jacobjakub
jacobjakub used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect

Commented:
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.
Thanks,
Karrtik
Top Expert 2016

Commented:
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


Sara

Author

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?
Thanks.
Jacob
mainwindow.cpp
mainwindow.h
things.cpp
things.h
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Software Architect
Commented:
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,
Karrtik
ToolBar.pngToolBarSample.txt
Karrtik IyerSoftware Architect

Commented:
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
QT_ToolBar_Examples.png

Author

Commented:
Hello Karrtik

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

Jacob

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial