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 ?
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 ?
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.
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.
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.Qt Code:
connect(txt,SIGNAL(textChanged()),myDialog,SLOT(setText(/*create this slot*/)));To copy to clipboard, switch view to plain text mode
maybe emitting your own signal holding a QString as a parameter would be better and more convenient with the slot you want.
Qt Code:
To copy to clipboard, switch view to plain text modejust a note why do you need to access the same variable from different classes ie. making a variable accessible from 2 windows ?where should I declare the variable so that both, Dialog.cpp & MainWindow.cpp can access it at the same time ?
Hope this helps.
Last edited by toufic.dbouk; 4th October 2013 at 20:49.
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 ?
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.
so, you mean there is NO way to declare a 'global' variable?
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.
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?
try something like :
not sure if that's the best way to do it
Qt Code:
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 mNumTo copy to clipboard, switch view to plain text mode
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?
doesnt have to ,
just declare in the header file and define it in the cpp file
harvey_slash (5th October 2013)
but this doesn't work .
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 "" ;
It works with QString. If it doesn't work for you then you did something wrong.
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.
Last edited by toufic.dbouk; 9th October 2013 at 10:42.
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?
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,
_
Bookmarks