Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    https://qt.gitorious.org/qt/qtbase/source/d52b00e1d3cc60c81b54a89d6da488dc4bbce384:src/corelib/io/qprocess.h

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

    https://qt.gitorious.org/qt/qtbase/source/d52b00e1d3cc60c81b54a89d6da488dc4bbce384:src/corelib/global/qnamespace.h

    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 );
    }
    while(0)

    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: http://qt-project.org/doc/qt-5/qprocess.html#setProcessChannelMode
    @
    QProcess builder;
    builder.setProcessChannelMode(QProcess::MergedChannels);
    builder.start("make", QStringList() << "-j2");

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


Log in to reply