// Dialog1.h
{
Q_OBJECT
public:
{
connect( pEdit,
SIGNAL( textEdited
( const QString & ) ),
this,
SLOT( onTextEdited
( const QString & ) ) );
}
signals:
void myVariableChanged
( const QString & );
public slots:
void onMyVariableChanged
( const QString & ) {
pEdit->setText( s );
}
private slots:
void onTextEdited
( const QString & s
) {
emit myVariableChanged( s );
}
private:
};
// Exactly the same code for Dialog2.
// main.cpp:
int main( int argc, char * * argv )
{
Dialog1 dlg1;
Dialog2 dlg2;
connect( &dlg1,
SIGNAL( myVariableChanged
( const QString & ) ),
&dlg2,
SLOT( onMyVariableChanged
( const QString & ) ) );
connect( &dlg2,
SIGNAL( myVariableChanged
( const QString & ) ),
&dlg1,
SLOT( onMyVariableChanged
( const QString & ) ) );
dlg1.show();
dlg2.show();
app.exec();
}
// Dialog1.h
class Dialog1 : public QDialog
{
Q_OBJECT
public:
Dialog1( QWidget * parent = 0 )
: QDialog( parent )
{
pEdit = new QLineEdit( this );
connect( pEdit, SIGNAL( textEdited( const QString & ) ), this, SLOT( onTextEdited( const QString & ) ) );
}
signals:
void myVariableChanged( const QString & );
public slots:
void onMyVariableChanged( const QString & )
{
pEdit->setText( s );
}
private slots:
void onTextEdited( const QString & s )
{
emit myVariableChanged( s );
}
private:
QLineEdit * pEdit;
};
// Exactly the same code for Dialog2.
// main.cpp:
int main( int argc, char * * argv )
{
QApplication app( argc, argv );
Dialog1 dlg1;
Dialog2 dlg2;
connect( &dlg1, SIGNAL( myVariableChanged( const QString & ) ), &dlg2, SLOT( onMyVariableChanged( const QString & ) ) );
connect( &dlg2, SIGNAL( myVariableChanged( const QString & ) ), &dlg1, SLOT( onMyVariableChanged( const QString & ) ) );
dlg1.show();
dlg2.show();
app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks