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:

Qt Code:
  1. void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, ...
  2. /* char *btn1, char *btn2, ... */ );
To copy to clipboard, switch view to plain text mode 

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, obviously is not really portable:

Qt Code:
  1. void foo(int n, va_list args)
  2. {
  3. int i = 0;
  4.  
  5. printf("[foo] n is %d and i is %d\n", n, i);
  6.  
  7. for (; i != n; ++i)
  8. printf("'%s'\n", va_arg(args, char *));
  9.  
  10. printf("I'm here... safe!\n");
  11. }
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. QCoreApplication a(argc, argv);
  16.  
  17. QStringList args = a.arguments();
  18.  
  19. QVector<char *> v;
  20.  
  21. // It works but I need to allocate memory as QString::toAscii().data() is not saved in 'v' I don' know why!
  22. foreach (QString a, args) {
  23. //char str[strlen(a.toAscii().data()) + 1];
  24. char *str = strdup(a.toAscii().data());
  25.  
  26. //strcpy(str, a.toAscii().data());
  27. printf("adding to v: '%s'\n", a.toAscii().data());
  28. v << str;
  29. }
  30.  
  31. foo(args.count(), reinterpret_cast<va_list>((v.data())));
  32.  
  33. // 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?
  34. return 0;
  35. }
To copy to clipboard, switch view to plain text mode 

As you could see it is a Kludge it incredibly works as, at least in Windows and Linux/X86, va_list is, in reality a char *. 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[]? The toAscii().data() seems to not create a valid QByteArray...

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…

P.S. For the de-allocation of the char * I've resolved calling QdeleteAll() on the QVector and then his method clean()... I've seen another useful class call QScopedPointer but the compiler doesn't liked the declaration of QVector < QScopedPointer > sadly