Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Accessing same variable from multiple windows

  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Post Accessing same variable from multiple windows

    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 ?

  2. #2
    Join Date
    Oct 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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.

  3. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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.
    Qt Code:
    1. QTextEdit *txt = new QTextEdit(this);
    2. QDialog *myDialog = new QDialog(this);
    3. connect(txt,SIGNAL(textChanged()),myDialog,SLOT(setText(/*create this slot*/)));
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. /*or*/connect(this,SIGNAL(sendText(QString/*implement it*/)),myDialog,SLOT(setText(QString/*create this slot*/)));
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by toufic.dbouk; 4th October 2013 at 21:49.

  4. #4
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Accessing same variable from multiple windows

    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 ?

  5. #5
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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.

  6. #6
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Accessing same variable from multiple windows

    so, you mean there is NO way to declare a 'global' variable?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing same variable from multiple windows

    Quote Originally Posted by harvey_slash View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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.

  9. #9
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Accessing same variable from multiple windows

    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?

  10. #10
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    try something like :
    not sure if that's the best way to do it

    Qt Code:
    1. A.h
    2. extern int mNum;
    3.  
    4. A.cpp
    5. #include "A.h"
    6. int mNum = 0; // initialize
    7.  
    8. B.cpp
    9. #include "A.h"
    10. // use mNum
    11.  
    12. C.cpp
    13. #include "A.h"
    14. // use mNum
    15.  
    16. D.cpp
    17. #include "A.h"
    18. //use mNum
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Accessing same variable from multiple windows

    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?

  12. #12
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    doesnt have to ,
    just declare in the header file and define it in the cpp file

  13. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (5th October 2013)

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing same variable from multiple windows

    Quote Originally Posted by harvey_slash View Post
    Were should I declare the variable?
    The same place you'd put it in any other c++ application -- in any implementation (cpp) file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #14
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Accessing same variable from multiple windows

    but this doesn't work .

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing same variable from multiple windows

    Quote Originally Posted by harvey_slash View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #16
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Accessing same variable from multiple windows

    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 "" ;

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing same variable from multiple windows

    It works with QString. If it doesn't work for you then you did something wrong.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #18
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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 11:42.

  20. #19
    Join Date
    May 2016
    Posts
    6
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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?

  21. #20
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing same variable from multiple windows

    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,
    _

Similar Threads

  1. multiple definitoin of variable
    By saman_artorious in forum Qt Programming
    Replies: 6
    Last Post: 22nd April 2013, 20:47
  2. Multiple definition of a variable?
    By T0bi4s in forum Newbie
    Replies: 5
    Last Post: 14th January 2010, 23:13
  3. Multiple File Data accessing
    By hasnatzaidi in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 17:34
  4. Replies: 6
    Last Post: 25th December 2008, 06:58
  5. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 11:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.