Climate Control Widget Example

This example shows how to access the climate control from C++.

This example shows how to access the climate control from the C++.

The first thing to do is to create a QIviClimateControl instance in our MainWindow constructor. As we only have one Climate backend and don't want to choose which one to use, we call startAutoDiscovery to start searching for a suitable backend right away and pick the first one that matches.

Note: To simplify the deployment process, this example loads a simulation backend.

isValid() is used for verifying that the autoDiscovery found a backend:

     m_climateControl = new QIviClimateControl(QString(), this);
     m_climateControl->setDiscoveryMode(QIviAbstractFeature::LoadOnlySimulationBackends);
     m_climateControl->startAutoDiscovery();

     if (!m_climateControl->isValid())
         QMessageBox::critical(this, tr("Auto Discovery Failed !"), tr("No Climate Backend available"));

The constructor then continues to connect the climate control attribute change signals to the UI components:

     //Air Flow Direction
     setupFlowDirectionRadioButtons(m_climateControl->airflowDirections());
     connect(m_buttonGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
             this, &MainWindow::onFlowDirectionButtonToggled);

     connect(m_climateControl, &QIviClimateControl::airflowDirectionsChanged,
             this, &MainWindow::setupFlowDirectionRadioButtons);

     //Air Condition
     ui->cb_airCondition->setChecked(m_climateControl->isAirConditioningEnabled());
     connect(m_climateControl, &QIviClimateControl::airConditioningEnabledChanged,
             ui->cb_airCondition, &QCheckBox::setChecked);
     connect(ui->cb_airCondition, &QCheckBox::clicked,
             m_climateControl, &QIviClimateControl::setAirConditioningEnabled);

     //Air Recirculation
     ui->cb_airRecirculation->setChecked(m_climateControl->recirculationMode() == QtIviVehicleFunctionsModule::RecirculationOn);
     connect(m_climateControl, &QIviClimateControl::recirculationModeChanged,
             this, &MainWindow::onAirRecirculationModeChanged);
     connect(ui->cb_airRecirculation, &QCheckBox::clicked,
             this, &MainWindow::setAirRecirculationEnabled);

     //Heater
     ui->cb_heater->setChecked(m_climateControl->isHeaterEnabled());
     connect(m_climateControl, &QIviClimateControl::heaterEnabledChanged,
             ui->cb_heater, &QCheckBox::setChecked);
     connect(ui->cb_heater, &QCheckBox::clicked,
             m_climateControl, &QIviClimateControl::setHeaterEnabled);
 }

Airflow direction is controlled using these functions:

 void MainWindow::setupFlowDirectionRadioButtons(QtIviVehicleFunctionsModule::AirflowDirections direction)
 {
     ui->cb_windshield->setChecked(direction.testFlag(QtIviVehicleFunctionsModule::Windshield));
     ui->cb_dashboard->setChecked(direction.testFlag(QtIviVehicleFunctionsModule::Dashboard));
     ui->cb_floor->setChecked(direction.testFlag(QtIviVehicleFunctionsModule::Floor));
 }

 void MainWindow::onFlowDirectionButtonToggled(QAbstractButton *button, bool checked)
 {
     Q_UNUSED(button)
     Q_UNUSED(checked)

     QtIviVehicleFunctionsModule::AirflowDirections direction;

     if (ui->cb_windshield->isChecked())
         direction |= QtIviVehicleFunctionsModule::Windshield;
     if (ui->cb_dashboard->isChecked())
         direction |= QtIviVehicleFunctionsModule::Dashboard;
     if (ui->cb_floor->isChecked())
         direction |= QtIviVehicleFunctionsModule::Floor;

     m_climateControl->setAirflowDirections(direction);
 }

Example project @ code.qt.io