Object as global variable
-
Hi everyone,
I'm a beginner on Qt, ... and I have a beginner's question.
I'm trying to develop an application made up of two main windows (I know it's not right, but I want windows that are independent of each other because they are equally important for the application). Each of the two windows lets me enter information that I save in a text file, and I want to be able to switch from one window to the other and back to the first, without losing what I've just entered in either window.
These windows are initialised in main.cpp .
Once displayed, I switch from one to the other using a combined show() and hide() button, but the problem is that I generate a new blank window each time and my input is lost.So I thought of tracking each of the window objects from their first generation by making them global objects declared in a header file so that I could call them every time.
'main.cpp' code :'varglobales.h' code :
But when I compile I get an error ' ' which stops the compilation...
What have I done wrong?
Thanks for your feedback
-
@MortyMars said in Object as global variable:
What have I done wrong?
You declared a pointer but did not define it anywhere. You basically only told the compiler that there is a pointer to SidStarWindow but nothing more. You have to define it in one of your cpp files.
And btw: global objects are not really good design and should be avoided. -
@Christian-Ehrlicher said in Object as global variable:
You declared a pointer but did not define it anywhere. You basically only told the compiler that there is a pointer to SidStarWindow but nothing more. You have to define it in one of your cpp files.
And btw: global objects are not really good design and should be avoided.Hi Christian,
Thanks for your quick and efficient feedback: it was indeed a beginner's question (in C++ more than in Qt...).
With this simple initialisation, it works the way I want it to!
I understand that global objects are not desirable, but what would be the elegant alternative in my case?
Thanks again!
-
-
@MortyMars hi and welcome to devnet,
The usual answer would be: proper architecture.
If you use global variable for ease of access then you are doing something very wrong.
-
@SGaist said in Object as global variable:
@MortyMars hi and welcome to devnet,
The usual answer would be: proper architecture.
If you use global variable for ease of access then you are doing something very wrong
Thank you for your welcome ;-)
-
@ChrisW67 said in Object as global variable:
@MortyMars SIDs, STARs, and approaches ... welcome to my world :)
I'm trying to develop a tool to help edit approach files for X-Plane 12, because it's not impossible to do by hand, but it's pretty tedious...
-
@SGaist said in Object as global variable:
@MortyMars hi and welcome to devnet,
The usual answer would be: proper architecture.
If you use global variable for ease of access then you are doing something very wrong.
Hi SGaist,
After a few fruitless searches on the net, I have to admit that I'd be interested in a bit more detail about an alternative solution to global variables and the right architecture to put in place as you mention....
Thank you for any leads you can give me ;-)
-
Well, the first thing to do is to identify why you think you need global variables.
What is your application architecture ?
What are you using them for ?From there we can iterate to improve your architecture.
-
@SGaist said in Object as global variable:
Well, the first thing to do is to identify why you think you need global variables.
What is your application architecture ?
What are you using them for ?From there we can iterate to improve your architecture
Thanks for your feedback.
My application is based around two windows that can be called up from each other. Each of these windows needs to be unique and remain for the entire life of the application, as data is entered in them and needs to be preserved.
I use the show() and hide() methods to hide and recall them, but the show() method (probably badly used) creates new objects of the window class each time I call it up and doesn't allow me to recall the two original windows.
Global variables seemed to me to be the solution for creating a lasting link and pointing to these two famous windows for sure.
If there's an alternative way of creating this durable link, then my problem is solved...
-
@MortyMars ok so first, take a look at QStackedWidget which allows you to easily switch between multiple widgets.
I don't know about the data you need to share but it makes me think of the model view architecture. Your widgets are the views and you share a model between the two. Give your widgets as setModel method, store the model pointer in each of and voila, your global variable need has been eliminated.
-
@SGaist said in Object as global variable:
ok so first, take a look at QStackedWidget which allows you to easily switch between multiple widgets.
I did not suggest this because: initially the OP talked about
show()
&hide()
, so only one window/widget at a time. For whichQStackedWidget
would be perfect. But in his initial code notice heshow()
s them both. He then saysand I want to be able to switch from one window to the other and back to the first
It took it to mean he wants to see both at the same time. But we can't tell.
@MortyMars Simple question: do you want both windows visible at same time ---
QStackedWidget
no good --- or do you only want one at a time --- in which case it's fine/recommended, but then don't start outshow()
ing both of them. So which is it? -
-
@MortyMars said in Object as global variable:
In fact I have two different windows, mainly to display data masks that don't fit in a single window.
The two windows are the same size and overlap perfectly.I can't see how this works or why one window won't....
The two windows are the same size and overlap perfectly.
And I don't need to have both displayed at the same time (since they don't fit on the screen anyway).Then you could go with a
QStackedWidget
as recommended by @SGaist .
You just simply replace / change the widget from one window instead of showing two windows which have a "static" widget.(since they don't fit on the screen anyway).
I mean, this is no excuse ;-)
Since you are the developer, you can make them fit ;-)
There are plenty techniques to do so (layouts, scroll areas... etc). -
@Pl45m4 said in Object as global variable:
Then you could go with a
QStackedWidget
as recommended by @SGaist .I see that @SGaist's QStackedWidget solution is unanimously approved.
So I'm going to take a closer look :-)(since they don't fit on the screen anyway).
I mean, this is no excuse ;-)
Since you are the developer, you can make them fit ;-)My only excuse is that I'm just a modest occasional developer ;-)
But the way my application is organised makes ergonomic sense.Thanks for your help :-)