Congratulations to our 2022 Qt Champions!

Convert enum in QProcess to string

  • I'm writing some code which responds to the error() signal in QProcess. What I'd like to do is easily output the value of the QProcess::ProcessError that gets passed in... I was going to take a generic approach when I dug through the code and found that they don't use Q_ENUMS inside of QProcess which would have made the enum values easily QString-able. See:

    As opposed to enums in the Qt namespace, which are Q_ENUMed:

    Is there any "rule" for Qt developers like, if you have a class that uses enumerated values publicly, you should tell moc about them with Q_ENUMS so that people can write really friendly error handlers.

    Should I submit this as a bug/feature request? Is there some means of getting at this thing that I don't know about? It is my understanding that one must use Q_ENUMS if you are going to try and use QMetaEnum.

  • You could do something like:
    @#define ENUM_TO_STR(X,Y) do
    if((X) == (Y) return QString::fromLatin1( #Y );

    QString error2str(const int &error)
    ENUM_TO_STR(error, QProcess::FailedToStart);
    ENUM_TO_STR(error, QProcess::Crashed);
    ENUM_TO_STR(error, QProcess::Timedot);
    ENUM_TO_STR(error, QProcess::ReadError);
    ENUM_TO_STR(error, QProcess::WriteError);
    ENUM_TO_STR(error, QProcess::UnknownError);

  • Yeah, I was trying to avoid writing any kind of specific case function - given Qt has a generic way of getting information about enums, it would be awesome if people registered their enums.

  • what about QIODevice::errorString() ?
    QIODevice is the base class of QProcess and there is an example that uses errorString() with a QProcess object, so you might just give it a try. :)

    see here:
    QProcess builder;
    builder.start("make", QStringList() << "-j2");

    if (!builder.waitForFinished())
    qDebug() << "Make failed:" << builder.errorString();
    qDebug() << "Make output:" << builder.readAll();

Log in to reply