Qmake: sprintf



  • In a .pro file I have three variables having the following sample values:
    A = 1
    B = 15
    C = 8

    These numbers should be joined in a single string by ensuring that each of them will be two characters wide (prepend 0 if the number is composed of one digit) and separated by underscore:
    01_15_08

    The current implementation of "qmake's sprintf()":http://qt-project.org/doc/qt-4.8/qmake-function-reference.html#sprintf-string-arguments allows for specifying the arguments as %1, %2, %3, ..., %9 without the field width functionality that its equivalent - "QString's arg()":http://qt-project.org/doc/qt-4.8/qstring.html#arg member function allows for.

    Since it is obvious that sprintf won't help formatting the string further than just filling strings in positions, then is there some qmake functionality that is equivalent to "QString's arg()":http://qt-project.org/doc/qt-4.8/qstring.html#arg?



  • <qt-library-version-4.8.3-src>\qmake\project.cpp, line 2271:
    @
    bool
    QMakeProject::doProjectTest(QString func, QList<QStringList> args_list, QMap<QString, QStringList> &place)
    {
    ...
    switch(func_t) {
    ...
    case T_COUNT:
    ...
    }
    @

    A new function should be added here that does the same work as "QString's arg()":http://qt-project.org/doc/qt-4.8/qstring.html#arg function.



  • Try this (if you're on linux):

    @A = 3
    A = $$system(printf [percent]02i $$A)
    message($$A)@
    (Unfortunately, the forum once again fails to reproduce what I've written here, that's why I used [percent] for the percent sign. Someone really needs to fix the code tags to actually reproduce the contents verbatim.)

    So I've just used the external program "printf" that's usually there in unix environments, to do the job that's missing in qmake. To be cross-platform compatible you might want to create a small pure C program yourself to do the job and distribute it with the project. Or even better, a python script, so you won't need to recompile your qmake extensions for other systems.

    That's what I usually do, because qmake is actually quite limited and obscure, unfortunately. It could have been so much more... A sensible syntax (How about a C++-like syntax? It's highly likely that people using qmake already know C++, but they didn't think of that), a proper documentation, some basic utility functions. The qmake-crowd should've less emphasized the "make" and put more on "q".



  • That's great idea. Use python to do the stuff. What I've noticed is that there is a "size" function in Qt 4.8.3 qmake parser's sources that does the stuff:

    @
    QStringList
    QMakeProject::doProjectExpand(QString func, QList<QStringList> args_list,
    QMap<QString, QStringList> &place)
    {
    ...
    case E_SIZE: {
    if(args.count() != 1) {
    fprintf(stderr, "%s:%d: size(var) requires one argument.\n",
    parser.file.toLatin1().constData(), parser.line_no);
    } else {
    int size = values(args[0], place).size();
    ret += QString::number(size);
    }
    break; }

    ...
    }
    @

    I wanted to write a custom qmake variable processing function that takes the same arguments as the one "QString's arg()":http://qt-project.org/doc/qt-4.8/qstring.html#arg function takes.

    I needed a function that finds out what is the size of a variable and traversing qt-everywhere-opensource-src-4.8.3\qmake\project.cpp found the size function.

    Thought this would work:
    @
    A = 1234
    A_SIZE = $$size($$A)
    @
    ... but the function always returns 0.

    Could you give an example with the python script?



  • Pro file:
    @A = 1234
    length = $$system(./strLength.py $$A)
    message($$length)@

    strLengt.py file:
    @#!/usr/bin/env python
    import sys
    print len(sys.argv[1])@

    Yet another example how crazy qmake is. qmake as a Python extension that would've been nice, too. All power of existing python plus qmake functionality.



  • I found the way the function should be used:
    @
    A = 1234
    A_SIZE = $$size(A) #pass the variable, not the variable's value

    result = 1

    A += 5678
    A_SIZE = $$size(A)

    result = 2

    @

    As it appears it is not the qmake's function I've been searching for:
    @
    A = 1234
    A_SIZE = $$size(A)

    desired result = 4

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.