Results 1 to 2 of 2

Thread: How to set common variable for multiple Dailogs in Qt

  1. #1
    Join Date
    Feb 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to set common variable for multiple Dailogs in Qt

    I am having two dialogs,dialog1 contains (dial1.h,dial1.cpp) and dialog2 contain(dial2.h,dial2.cpp) and one common main.cpp file ,how will i set common variable for this dialogs some one help me pls...


    thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set common variable for multiple Dailogs in Qt

    The Qt way to share information between different parts of the GUI is to use signals and slots.

    If you have a value that is in one place (for example dialog1), and you need to notify another place (dialog2) that the value has changed, then you create a signal for dialog1 (variableChanged( const MyVariable & )) and write a slot for dialog2 (onVariableChanged( const MyVariable & )). Connect the signal to the slot. In dialog1, when the user does something to change the value of your variable, dialog1 emits variableChanged( newValue ). Dialog2 receives this signal through the slot, and can update its copy of the variable. If the value can be changed in either place, then you make the same signal for dialog2, and slot for dialog1 and connect them the other way also.

    The code looks something like this:

    Qt Code:
    1. // Dialog1.h
    2. class Dialog1 : public QDialog
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. Dialog1( QWidget * parent = 0 )
    8. : QDialog( parent )
    9. {
    10. pEdit = new QLineEdit( this );
    11. connect( pEdit, SIGNAL( textEdited( const QString & ) ), this, SLOT( onTextEdited( const QString & ) ) );
    12. }
    13.  
    14. signals:
    15. void myVariableChanged( const QString & );
    16.  
    17. public slots:
    18. void onMyVariableChanged( const QString & )
    19. {
    20. pEdit->setText( s );
    21. }
    22.  
    23. private slots:
    24. void onTextEdited( const QString & s )
    25. {
    26. emit myVariableChanged( s );
    27. }
    28.  
    29. private:
    30. QLineEdit * pEdit;
    31. };
    32.  
    33. // Exactly the same code for Dialog2.
    34.  
    35. // main.cpp:
    36.  
    37. int main( int argc, char * * argv )
    38. {
    39. QApplication app( argc, argv );
    40.  
    41. Dialog1 dlg1;
    42. Dialog2 dlg2;
    43.  
    44. connect( &dlg1, SIGNAL( myVariableChanged( const QString & ) ), &dlg2, SLOT( onMyVariableChanged( const QString & ) ) );
    45. connect( &dlg2, SIGNAL( myVariableChanged( const QString & ) ), &dlg1, SLOT( onMyVariableChanged( const QString & ) ) );
    46.  
    47. dlg1.show();
    48. dlg2.show();
    49.  
    50. app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 

    Of course your code will not be the same as this. This is just a toy example to show how to communicate a value between two dialogs. What is important to see is that Dialog1 and Dialog2 do not know anything about each other. main() knows that both dialogs have a myVariableChanged() signal and an onMyVariableChanged() slot. That's it. main() doesn't know what theses slots do, and it doesn't matter. Dialog1 simply knows that when the user edits the text in the line edit, it should tell the rest of the world using its signal. Likewise, when someone out in the world changes the value, Dialog1 knows because it is listening for it through its slot. When someone tells it, it updates the user interface.

Similar Threads

  1. State variable used by multiple threads
    By bryang in forum Qt Programming
    Replies: 4
    Last Post: 29th April 2012, 21:08
  2. Replies: 3
    Last Post: 25th February 2012, 23:25
  3. multiple table in a single variable
    By sattu in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2011, 12:01
  4. Replies: 4
    Last Post: 2nd July 2010, 22:16
  5. Multiple definition of a variable?
    By T0bi4s in forum Newbie
    Replies: 5
    Last Post: 14th January 2010, 23:13

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.