PDA

View Full Version : Accessing same variable from multiple windows



harvey_slash
4th October 2013, 18:03
I have the main window, and I have a dialog named Dialog.
I want to type in the main window, and the text should appear in the dialog.
where should I declare the variable so that both, Dialog.cpp & MainWindow.cpp can access it at the same time ?

deusinvictus
4th October 2013, 19:26
I'm not completely sure I understand the use case, but I'll try and answer as I understand it. If this is supposed to be updating in real time, I would think your best bet would be to create a signal/slot between the main window and the dialog. Whenever the text in the edit box on the Main Window changes, it passes the new string to the dialog which can update it's own copy of the string. If it's just a one way communication this should work fine. If you need two way (such as editing in the main window updates both, editing in the dialog updates both) you would need to be a bit more careful not to create a loop with the signals.

toufic.dbouk
4th October 2013, 20:38
hello, yea as deusinvictus said , use signal and slots, every time you type in the main window suppose in a text edit, catch the emitted signal in the dialog and create a slot in the dialog to do the work.

QTextEdit *txt = new QTextEdit(this);
QDialog *myDialog = new QDialog(this);
connect(txt,SIGNAL(textChanged()),myDialog,SLOT(se tText(/*create this slot*/)));
if you dont want to use the textChanged SIGNAL , you can implement your Signal and emit it whenever you want holding the QString you need.
maybe emitting your own signal holding a QString as a parameter would be better and more convenient with the slot you want.

/*or*/connect(this,SIGNAL(sendText(QString/*implement it*/)),myDialog,SLOT(setText(QString/*create this slot*/)));

where should I declare the variable so that both, Dialog.cpp & MainWindow.cpp can access it at the same time ?
just a note why do you need to access the same variable from different classes ie. making a variable accessible from 2 windows ?
Hope this helps.

harvey_slash
5th October 2013, 11:19
Okay. suppose I have 3 dialogs. A B C
A has one button.
B has one button.
C has a lineEdit called lineEd.

buttons of A and B work as toggle switches. (suppose int flag = 1, pressing the button will make the flag 0(and also ,if flag =0, flag will become 1))
so , I guess I need to use flag in such a way that it can be accessed by both dialogs.

How should I declare it ?

toufic.dbouk
5th October 2013, 11:36
if you want to go with the signal and slot mechanism, when the button is toggled emit a signal holding the value of the flag , catch that signal in the other dialog and there you go ,you have the value ( 1 or 0 ) hence you can you do whatever you want according to the value.

you can use some getters and setters methods from one dialog to another to get the value of the flag too.

maybe data sharing and reference counting can help too or any other approach.

harvey_slash
5th October 2013, 12:01
so, you mean there is NO way to declare a 'global' variable?

wysota
5th October 2013, 12:05
so, you mean there is NO way to declare a 'global' variable?

You can declare a global variable just like in any C++ program but the point is it is bad practice and you should use better approaches such as using signals and slots.

toufic.dbouk
5th October 2013, 12:13
I wouldnt go my self for a global variable , just use signals and slots.
but why are you avoiding signal and slots ? and insisting instead on a global variable ?
Listen to wysota , i always do and get the job done correctly.
good luck.

harvey_slash
5th October 2013, 12:28
I just want to try that out.
I know that signal slots are are better.
Were should I declare the variable?
I have:

dialog.h
dialog.cpp
MainWindow.h
MainWindow.cpp

And
Dialog and mainWindow ui s.

where to declare the variable so that they can be accessed from mainWindow and dialog?

toufic.dbouk
5th October 2013, 12:48
try something like :
not sure if that's the best way to do it


A.h
extern int mNum;

A.cpp
#include "A.h"
int mNum = 0; // initialize

B.cpp
#include "A.h"
// use mNum

C.cpp
#include "A.h"
// use mNum

D.cpp
#include "A.h"
//use mNum

harvey_slash
5th October 2013, 13:18
I had tried this.
It's showing "already defined"

A is a class having only me variable,right?
it's not any dialog or window,right?

toufic.dbouk
5th October 2013, 13:39
doesnt have to ,
just declare in the header file and define it in the cpp file

wysota
5th October 2013, 17:55
Were should I declare the variable?
The same place you'd put it in any other c++ application -- in any implementation (cpp) file.

harvey_slash
9th October 2013, 07:20
but this doesn't work .

wysota
9th October 2013, 07:41
but this doesn't work .

Apparently you didn't do it correctly. Qt doesn't change the way one uses global variables in C++ applications.

harvey_slash
9th October 2013, 07:52
okay here is the deal
it works with integers .
suppose I make I=54 in mainwindow and print I in another window , it shows 54.
but it doesn't work with QStrings.
when I acesss qstring q from dialog 'dialog' , it show "" ;

wysota
9th October 2013, 10:22
It works with QString. If it doesn't work for you then you did something wrong.

toufic.dbouk
9th October 2013, 10:38
Hello Wysota,
Just a note, since there are two threads for the same user about the same topic, even having the same title , why dont you move them into one thread ?
that's the other thread Accessing the same variable from multiple windows.
It would be better instead of going through to the two threads every now and then.
Thanks in advance Sir.
Best Regards.

MartijnKor
2nd May 2016, 22:32
Hey guys!

I have sort of the same question as harvey_slash. Whenever you press the button "add" in my MainWindow it will open a dialog in which you can fill in some line-edits. Each of the line-edits will be assigned to a specific QString variable. And all of those QStrings will be put into a class of my own making. Then, whenever I post the "save" button whithin the dialog I would like it to put that object with the QStrings into a QVector that I have created in my MainWindow. I am struggling unfortunately with sending that object to my MainWindow. Can any of you please help me out? :)

anda_skoa
3rd May 2016, 08:41
If you have a modal dialog, just add a getter function and call it when exec() returns.
If you have a non-modal dialog, emit the value with a custom signal.

Cheers,
_

MartijnKor
4th May 2016, 23:20
Thanks for your response!

So, if I understand correctly, you can only send one value at a time. It isn't possible for me to send an entire object holding more variables using the signal slot mechanism?

Thanks!

anda_skoa
5th May 2016, 10:26
So, if I understand correctly, you can only send one value at a time.

No.
A signal can have multiple arguments and the argument types can be any custom class.



It isn't possible for me to send an entire object holding more variables using the signal slot mechanism?

Of course it is. That's what my previous comment said.

Cheers,
_