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 constru
QDebug
should be used for logging and debugging. QDebug
constructs a QTextStream
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, use tr
for user visible strings, or QStringLiteral
instead of tr
elsewhere:
const auto errStr = QMetaEnum::fromType<QCanBusDevice::CanBusError>().valueToKey(error);
ui->statusBar->showMessage(tr("Error occured: %1").arg(errStr));
Another, more elegant way, is by using QVariant's toString() method:
QString errStr = QVariant::fromValue(error).toString();
ui->statusBar->showMessage("Error occured: " + errStr);