I read several advices how to get an actual QString
from a Q_ENUM
value.
Below are 3 possible ways, I came up with, that are compilable constructs in QT5.11.1
What of them should one prefer and why?
void MainWindow::setErrorText(QCanBusDevice::CanBusError error)
{
QString errorString;
QDebug(&errorString) << error;
ui->statusBar->showMessage("Error occured: " + errorString);
// QT4 ?
const QMetaObject& mo = QCanBusDevice::staticMetaObject;
QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("CanBusError"));
QString errorStr(me.valueToKey(QCanBusDevice::UnconnectedState));
ui->statusBar->showMessage("Error occured: " + errorStr);
// From QT5?
QString errorS(QMetaEnum::fromType<QCanBusDevice::CanBusError>().valueToKey(error));
ui->statusBar->showMessage("Error occured: " + errorS);
}
QDebug
should be used for logging and debugging.QDebug
constructs aQTextStream
and is quite expensive for what you're trying to do.Using
QMetaEnum
is proper. You shouldn't be doing string concatenation the way you do, usetr
for user visible strings, orQStringLiteral
instead oftr
elsewhere: