Avatar of Jacob
Jacob
Flag 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.
C++

Avatar of undefined
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
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
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
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Karrtik Iyer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Karrtik Iyer

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
Jacob

ASKER
Hello Karrtik

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

Jacob