HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Settings dialog, using information from its parent window

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
settingsparentitsusingdialogwindowfrominformation

Problem

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(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:

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.