The "Dialog" and "Dialog_2" classes are friend classes of that MainWindow class.
Wrong, wrong, wrong. Except in rare circumstance you never need friend classes, and certainly not for the case you are describing.
Your case is exactly what Qt's signals and slots are designed for. You want to add a new person to MainWindow's data structure from your Dialog class? Then add a signal to Dialog_2 to let MainWindow know:
class Dialog_2 /...
{
Q_OBJECT
// ...
signals:
// ...
};
class Dialog_2 /...
{
Q_OBJECT
// ...
signals:
void addNewPerson( const QString & name, const QString & number );
// ...
};
To copy to clipboard, switch view to plain text mode
In your Dialog_2::add_new_person method, you do this:
void Dialog_2::add_new_person()
{
QString new_name
= ui
->lineEdit_1
->text
();
QString new_number
= ui
->lineEdit_2
->text
();
emit addNewPerson(new_name, new_number);
}
void Dialog_2::add_new_person()
{
QString new_name = ui->lineEdit_1->text();
QString new_number = ui->lineEdit_2->text();
emit addNewPerson(new_name, new_number);
}
To copy to clipboard, switch view to plain text mode
In your MainWindow class, you create a slot:
// MainWindow.h
class MainWindow : //...
{
Q_OBJECT
// ...
private slots:
};
// MainWindow.cpp:
void MainWindow
::onAddNewPerson( const QString & name,
const QString & number
) {
data_map.insert( name, number );
}
// MainWindow.h
class MainWindow : //...
{
Q_OBJECT
// ...
private slots:
void onAddNewPerson( const QString & name, const QString & number );
};
// MainWindow.cpp:
void MainWindow::onAddNewPerson( const QString & name, const QString & number )
{
data_map.insert( name, number );
}
To copy to clipboard, switch view to plain text mode
And at the place where you invoke the Dialog_2 instance:
void MainWindow::getNewPersonInfo()
{
Dialog_2 dlg( this );
connect ( &dlg, &Dialog_2::addNewPerson, this, &MainWindow::onAddNewPerson );
dlg.exec();
}
void MainWindow::getNewPersonInfo()
{
Dialog_2 dlg( this );
connect ( &dlg, &Dialog_2::addNewPerson, this, &MainWindow::onAddNewPerson );
dlg.exec();
}
To copy to clipboard, switch view to plain text mode
And as soon as the Add button is clicked in Dialog_2, the signal gets sent to MainWindow, and the new person gets added. If you want to get feedback to Dialog_2 to report success, then you add a signal to MainWindow ("void newPersonAdded( bool bStatus )" ) that you emit after the insert into the map. If the insert succeeded, you pass "true" in the signal, otherwise you pass "false" as the "bStatus" value.
In Dialog_2, you add a slot (void onNewPersonAdded( bool bStatus ) ). In that slot you can display your QMessageBox with the "Success!" or "Failed!" message.
You add a second connection between the MainWindow's signal and the Dialog_2 slot at the same place you made the first connection:
void MainWindow::getNewPersonInfo()
{
Dialog_2 dlg( this );
connect ( &dlg, &Dialog_2::addNewPerson, this, &MainWindow::onAddNewPerson );
connect( this, &MainWindow::onNewPersonAdded, &dlg, &Dialog_2::onNewPersonAdded );
dlg.exec();
}
// and update this method:
void MainWindow
::onAddNewPerson( const QString & name,
const QString & number
) {
if ( data_map.contains( name ) )
emit newPersonAdded( false ); // duplicate name, so don't add it
else
{
data_map.insert( name, number );
emit newPersoanAdded( true );
}
}
void MainWindow::getNewPersonInfo()
{
Dialog_2 dlg( this );
connect ( &dlg, &Dialog_2::addNewPerson, this, &MainWindow::onAddNewPerson );
connect( this, &MainWindow::onNewPersonAdded, &dlg, &Dialog_2::onNewPersonAdded );
dlg.exec();
}
// and update this method:
void MainWindow::onAddNewPerson( const QString & name, const QString & number )
{
if ( data_map.contains( name ) )
emit newPersonAdded( false ); // duplicate name, so don't add it
else
{
data_map.insert( name, number );
emit newPersoanAdded( true );
}
}
To copy to clipboard, switch view to plain text mode
No need for friend classes, no need for Dialog_2 to know how MainWindow stores person information, no need for Dialog_2 to even know there is a MainWindow class. The signals and slots take care of transmitting the information between Dialog_2 and MainWindow.
Bookmarks