Link to home
Start Free TrialLog in
Avatar of Jacob
JacobFlag for Denmark

asked on

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.
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

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
Avatar of sarabande
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
Avatar of Jacob

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
User generated image
Avatar of Jacob

ASKER

Hello Karrtik

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

Jacob