diff options
-rw-r--r-- | src/sensors/qsensor.cpp | 25 | ||||
-rw-r--r-- | src/sensors/qsensorbackend.cpp | 8 | ||||
-rw-r--r-- | tests/auto/qsensor/tst_qsensor.cpp | 22 |
3 files changed, 41 insertions, 14 deletions
diff --git a/src/sensors/qsensor.cpp b/src/sensors/qsensor.cpp index 8d670bb7..a0724ae8 100644 --- a/src/sensors/qsensor.cpp +++ b/src/sensors/qsensor.cpp @@ -263,19 +263,22 @@ bool QSensor::connectToBackend() if (isConnectedToBackend()) return true; + int dataRate = d->dataRate; + int outputRange = d->outputRange; + d->backend = QSensorManager::createBackend(this); - // Reset the properties to their default values and re-set them now so - // that the logic we've put into the setters gets called. - if (d->dataRate != 0) { - int tmp = d->dataRate; - d->dataRate = 0; - setDataRate(tmp); - } - if (d->outputRange != -1) { - int tmp = d->outputRange; - d->outputRange = -1; - setOutputRange(tmp); + if (d->backend) { + // Reset the properties to their default values and re-set them now so + // that the logic we've put into the setters gets called. + if (dataRate != 0) { + d->dataRate = 0; + setDataRate(dataRate); + } + if (outputRange != -1) { + d->outputRange = -1; + setOutputRange(outputRange); + } } return isConnectedToBackend(); diff --git a/src/sensors/qsensorbackend.cpp b/src/sensors/qsensorbackend.cpp index a576e5b9..7c65c59b 100644 --- a/src/sensors/qsensorbackend.cpp +++ b/src/sensors/qsensorbackend.cpp @@ -224,8 +224,7 @@ void QSensorBackend::addDataRate(qreal min, qreal max) setDataRates(otherSensor); \endcode - Note that this function should be called from the constructor so that the information - is available immediately. + Note that this function must be called from the constructor. \sa QSensor::availableDataRates, addDataRate() */ @@ -239,9 +238,12 @@ void QSensorBackend::setDataRates(const QSensor *otherSensor) qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with an invalid sensor"; return; } + if (m_sensor->isConnectedToBackend()) { + qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates outside of the constructor"; + return; + } QSensorPrivate *d = m_sensor->d_func(); d->availableDataRates = otherSensor->availableDataRates(); - d->dataRate = otherSensor->dataRate(); } /*! diff --git a/tests/auto/qsensor/tst_qsensor.cpp b/tests/auto/qsensor/tst_qsensor.cpp index 92cb281a..8025ab21 100644 --- a/tests/auto/qsensor/tst_qsensor.cpp +++ b/tests/auto/qsensor/tst_qsensor.cpp @@ -353,6 +353,28 @@ private slots: QCOMPARE(actual, expected); } + // Test that a previously-set, valid data rate is retained + { + TestSensor sensor; + sensor.setDataRate(100); + sensor.setProperty("doThis", "rates"); + sensor.connectToBackend(); + int actual = sensor.dataRate(); + int expected = 100; + QCOMPARE(actual, expected); + } + + // Test that a previously-set, invalid data rate is reset to 0 + { + TestSensor sensor; + sensor.setDataRate(50); + sensor.setProperty("doThis", "rates"); + sensor.connectToBackend(); + int actual = sensor.dataRate(); + int expected = 0; + QCOMPARE(actual, expected); + } + { TestSensor sensor; sensor.setProperty("doThis", "rates(0)"); |