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

Calling function with variable arguments having a QStringList



  • Hy to all I have a GUI application that at work we use to test the creations of Qt Widget itself, for example I have a dialog to create a QMessageBox with favourited properties icon, title, message and button of Qt or custom (you write the text on a list).

    All this data are send using a json and the buttons are memorized in QStringList, I have a C function that creates the json, that normally it would called in C but now I want to call in C++, but how convert the QStringList in the "..." (va_list de facto?).

    This is the prototype of the function that I want use:

    @
    void messagebox(json_object **json, char *iconType, char *title, char msg, int showTime, ...
    /
    char *btn1, char *btn2, ... */ );
    @

    I cannot change the signature of the function to use a QStringList as it have to be used by a plain C application...

    I have found a solution that in Windows XP works, but, obviusly is not really portable:

    @
    void foo(int n, va_list args)
    {
    int i = 0;

    printf("[foo] n is %d and i is %d\n", n, i);
    
    for (; i != n; ++i)
        printf("'%s'\n", va_arg(args, char *));
    
    printf("I'm here... safe!\n");
    

    }

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QStringList args = a.arguments();
    
    QVector<char *> v;
    
    // It works but I need to allocate memory as QString::toAscii().data() is not saved in 'v' I don' know why!
    foreach (QString a, args) {
        //char str[strlen(a.toAscii().data()) + 1];
        char *str = strdup(a.toAscii().data());
    
        //strcpy(str, a.toAscii().data());
        printf("adding to v: '%s'\n", a.toAscii().data());
        v << str;
    }
    

    foo(args.count(), reinterpret_cast<va_list>((v.data())));

    // Now I need to free() str  in v or not? If it was a real function and not main() I has a memory leak here or not?
    return 0;
    

    }
    @

    As you could see it is a Kludge it incredibly works as, at least in Windows, va_list is, in reality a char *, I'm unsure on Linux (where I have to do this tomorrow). So I pass all the strings one after another to the "foo" function...

    At least I'd like to use a vector of QString to avoid the allocation of memory but I cannot find a way, data() in this case would be what a QString? Or QString *? Or a QString[]?

    By the way if my PM see that code probably he will kill me so I'm asking to you there's a more portable solution in Qt to this problem?

    For example supposing that could be happy with max 32 buttons MOC could automate the scripture of code as this:

    @
    QStringList buttons;

    switch(buttons) {
    case 0: // We have no buttons so I could call messagebox() w/o the last arg
    messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime);
    break;

    case 1: // We have only one buttons simple is the first element of the list...
       messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, buttons[0]);
       break;
    

    case 2: /* 2 buttons they're in index 0 and 1 */
    messagebox(json_object **json, char *iconType, char *title, char msg, int showTime, buttons[0], buttons[1]);
    break;
    .... /
    3-4-5-6-7-... buttons */
    case 31:
    messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, buttons[0], buttons[1], buttons[2], buttons[3], buttons[4], buttons[5], [...], buttons[31]);
    break;
    }
    @

    What do you think? Is this possible?

    Thanks for your attention...


Log in to reply