There are three solutions:
1) pass a pointer to the first dialog
SecondDialog
::SecondDialog( FirstDialog
*ptr,
QWidget *parent
) : QDialog( parent
), _firstDialogPtr
( ptr
) {
...
}
...
void SecondDialog::processParams()
{
...
_firstDialogPtr->setSomenting( whatever );
...
}
SecondDialog::SecondDialog( FirstDialog *ptr, QWidget *parent )
: QDialog( parent ), _firstDialogPtr( ptr )
{
...
}
...
void SecondDialog::processParams()
{
...
_firstDialogPtr->setSomenting( whatever );
...
}
To copy to clipboard, switch view to plain text mode
This is quite easy solution, but it also the worst one (except for the one with global variables which I won't even mention). It's bad, because both dialogs are tightly coupled.
2) use accessor methods to retrieve data from the SecondDialog:
void FirstDialog::doSomething()
{
SecondDialog dlg( this );
if( dlg.exec() == Qt::Accepted ) {
someWidget->setSomething( dlg.something() ); // SecondDialog::something() returns the data you need
...
}
}
void FirstDialog::doSomething()
{
SecondDialog dlg( this );
if( dlg.exec() == Qt::Accepted ) {
someWidget->setSomething( dlg.something() ); // SecondDialog::something() returns the data you need
...
}
}
To copy to clipboard, switch view to plain text mode
This is a bit better approach, since you can reuse SecondDialog for other purposes.
3) Use signals and slots:
void FirstDialog::showSecondDialog()
{
SecondDialog *dlg = new SecondDialog( this );
dlg->setWFlags( dlg->getWFlags() | Qt::WDestructiveClose );
connect( dlg, SIGNAL( somethingChanged( int ) ),
this, SLOT( setSomething( int ) ) );
...
dlg->show();
}
void FirstDialog::showSecondDialog()
{
SecondDialog *dlg = new SecondDialog( this );
dlg->setWFlags( dlg->getWFlags() | Qt::WDestructiveClose );
connect( dlg, SIGNAL( somethingChanged( int ) ),
this, SLOT( setSomething( int ) ) );
...
dlg->show();
}
To copy to clipboard, switch view to plain text mode
This is the version with minimal coupling, but it also requires a bit more work than other ones.
Bookmarks