Results 1 to 10 of 10

Thread: Accessing the same variable from multiple windows.

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

    Default Accessing the same variable from multiple windows.

    I want to access the same variable from mainWindow as well as my dialog class called 'Dialog'
    suppose i change this variable from mainwindow, the change should be reflected whenever i use the variable from dialog.
    i cannot use signal /slots due to some reasons.

    i have already tried this.
    i made a source calls vars.h /vars.cpp and declared the variable there, but it doesn't seem to work .
    what can i do ?

  2. #2
    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 the same variable from multiple windows.

    Do you mean you need a global variable ?
    Wouldnt go for a global variable since its not a good practice.
    What are the reasons behind not using signals and slots ?

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

    Default Re: Accessing the same variable from multiple windows.

    no, I just want to use a global variable .

  4. #4
    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 the same variable from multiple windows.

    Isnt it the same as this link link.
    Last edited by toufic.dbouk; 9th October 2013 at 07:52.

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

    Default Re: Accessing the same variable from multiple windows.

    okay, here is my problem,(why I want to use global)

    I have connected my mainwindow and dialog (thanks you you)
    like this :
    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(seText(QString)));
    To copy to clipboard, switch view to plain text mode 

    this is my event :

    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3.  
    4.  
    5. int i=event->key();
    6. //char z=(char)i;
    7.  
    8. qDebug()<<q;
    9.  
    10.  
    11.  
    12. if(i>=48&&i<=57)
    13.  
    14. {
    15. QString s= QString::number(i-'0');
    16.  
    17.  
    18. q+=s;
    19. ui->lineEdit->setText(q);
    20.  
    21. //qDebug()<<q;
    22. }
    23. if(q.length()>20&&flag==0)
    24. {
    25. //ui->lineEdit->setText("test");
    26. dialog =new Dialog();
    27. flag=1;
    28. dialog->show();
    29. raise();
    30. activateWindow();
    31. connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(seText(QString)));
    32.  
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

    now, the problem is, the text is not displayed as the window opens, I have to input one more number to display the whole string in the dialog.

    (how to make it such that , as the window opens, I can see the already input text)
    because, there is one situation where textchanged is not emitted and the window is open.


    Added after 38 minutes:


    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 "" ;
    Last edited by harvey_slash; 9th October 2013 at 07:58.

  6. #6
    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 the same variable from multiple windows.

    If its just a problem of showing some variable when you instantiate the dialog, just pass the variable from main window into the constructor of the dialog and set it directly in the constructor.
    Or you can make a method in the dialog supposedly a getter method that gets the text from main window when you create the dialog and set it in the dialogs ui.
    Good luck.

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

    Default Re: Accessing the same variable from multiple windows.

    I had thought of that, but I want to use this global variable approach.
    also, I want to know what is wrong with my code

  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 the same variable from multiple windows.

    Quote Originally Posted by harvey_slash
    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 "" ;
    What are you trying to do ? And what exactly is the problem?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 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 the same variable from multiple windows.

    Quote Originally Posted by harvey_slash View Post
    I had thought of that, but I want to use this global variable approach.
    Moving to "General Programming" forum, since this is C++ related and not Qt related.
    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.


  10. #10
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Accessing the same variable from multiple windows.

    You could try to use a common OOP getter/setter pattern. Something like:
    Qt Code:
    1. int newValue;
    2. dialog = new Dialog();
    3. dialog->setValue(i);
    4. if(dialog->->exec() != QDialog::Rejected)//here i'll display in some way your data
    5. newValue = dialog->getValue();//if you can modify it in your dialog (i.e. if you print it into an editable QTextEdit field, allowing user to do changes)
    6. ...
    To copy to clipboard, switch view to plain text mode 
    As Dialog is your custom subclass of QDialog (i guess) you can add those getter/setter methods.
    Hope this helps.
    Last edited by AlbertoN; 9th October 2013 at 14:44.

Similar Threads

  1. Accessing same variable from multiple windows
    By harvey_slash in forum Newbie
    Replies: 21
    Last Post: 5th May 2016, 10:26
  2. multiple definitoin of variable
    By saman_artorious in forum Qt Programming
    Replies: 6
    Last Post: 22nd April 2013, 19:47
  3. Multiple definition of a variable?
    By T0bi4s in forum Newbie
    Replies: 5
    Last Post: 14th January 2010, 22:13
  4. Replies: 6
    Last Post: 25th December 2008, 05:58
  5. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10: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.