patterncppMinor
Settings dialog, using information from its parent window
Viewed 0 times
settingsparentitsusingdialogwindowfrominformation
Problem
My
Now in SettingsDialog:
It works fine but is it a good way to access the info which is present in the parent window? or should the parent window pass in the device type in the constructor so it only knows the device type and work with it?
MainWindow popups a SettingsDialog. The setting dialog need to hide certain fields if the connected device is certain type.void MainWindow::on_actionSettings_triggered()
{
SettingsDialog * settings = new SettingsDialog(this);
settings->exec();
}Now in SettingsDialog:
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
QSettings settings;
MainWindow * mainWindow = (MainWindow *) parent;
if ( mainWindow->deviceType == DEVICE_TYPE_B )
{
ui->comboBoxIP->setVisible( false );
}
}It works fine but is it a good way to access the info which is present in the parent window? or should the parent window pass in the device type in the constructor so it only knows the device type and work with it?
Solution
If the SettingsDialog can only take a MainWindow as a parent in the constructor then declare it to be so:
As for getting the type indeed passing it in explicitly is better.
My MainWindow popups a SettingsDialog. The setting dialog need to hide certain fields if the connected device is certain type.
Now in SettingsDialog:
SettingsDialog::SettingsDialog(MainWindow *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{As for getting the type indeed passing it in explicitly is better.
void MainWindow::on_actionSettings_triggered()
{
SettingsDialog * settings = new SettingsDialog(this, deviceType);
settings->exec();
}My MainWindow popups a SettingsDialog. The setting dialog need to hide certain fields if the connected device is certain type.
void MainWindow::on_actionSettings_triggered()
{
SettingsDialog * settings = new SettingsDialog(this);
settings->exec();
}Now in SettingsDialog:
SettingsDialog::SettingsDialog(MainWindow *parent, DeviceType deviceType) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
QSettings settings;
if (deviceType == DEVICE_TYPE_B )
{
ui->comboBoxIP->setVisible( false );
}
}Code Snippets
SettingsDialog::SettingsDialog(MainWindow *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{void MainWindow::on_actionSettings_triggered()
{
SettingsDialog * settings = new SettingsDialog(this, deviceType);
settings->exec();
}void MainWindow::on_actionSettings_triggered()
{
SettingsDialog * settings = new SettingsDialog(this);
settings->exec();
}SettingsDialog::SettingsDialog(MainWindow *parent, DeviceType deviceType) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
QSettings settings;
if (deviceType == DEVICE_TYPE_B )
{
ui->comboBoxIP->setVisible( false );
}
}Context
StackExchange Code Review Q#114389, answer score: 3
Revisions (0)
No revisions yet.