Results 1 to 3 of 3

Thread: Getting an error for QString::arg instead of message

  1. #1
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Getting an error for QString::arg instead of message

    Hello,
    I'm using QString::arg in a dynamical environment: string and arguments are unknown. So I've to replace the place markers as they come.
    My problem is that I can get more arguments to replace than markers in the string, so it's probable I'm getting an error. The documentation says:
    If there is no unreplaced place marker remaining, a warning message is output and the result is undefined. Place marker numbers must be in the range 1 to 99.
    I don't need the warning message, but something I can handle in the code (value or exception). But being the result undefined in case of error, I don't know how to proceed.

    Apart the brutal counting of "%X" in the string (which, again, is useless to count as my arguments are dynamic so the count may change in each substitution), is there a way to get an error/exception instead of that warning message?

    Thanks.
    ~Aki

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting an error for QString::arg instead of message

    I think the only way to know ahead of time if the pattern string contains placeholders is to look for one (no need to count them all). Something like this:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. QString dynamicReplace(const QString &pattern, const QStringList &values)
    5. {
    6. static QRegExp marker("%\\d\\d?");
    7.  
    8. QString result = pattern;
    9. QStringListIterator i(values);
    10. while (i.hasNext() && result.contains(marker) > 0)
    11. result = result.arg(i.next());
    12. return result;
    13. }
    14.  
    15.  
    16. int main(int argc, char *argv[])
    17. {
    18. QCoreApplication app(argc, argv);
    19.  
    20.  
    21. QStringList values = QStringList() << "A" << "B" << "C";
    22.  
    23. qDebug() << "Markers match values:" << dynamicReplace("%1 %2 %3", values);
    24. qDebug() << "More values than markers:" << dynamicReplace("%1 %2", values);
    25. qDebug() << "More markers than values:" << dynamicReplace("%1 %2 %3 %4", values);
    26.  
    27. values << "value inserts %1 marker or %2" << "D" << "E";
    28. qDebug() << "Marker added on the fly:" << dynamicReplace("%1 %2 %3 %4", values);
    29.  
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    akiross (9th April 2011)

  4. #3
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting an error for QString::arg instead of message

    Ok, I was hoping in Qt to have such a method, but I guess it's not the case
    Thank you very much!
    ~Aki

Similar Threads

  1. Message editor already in use error
    By Ismb in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 17th February 2011, 14:22
  2. error message
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2009, 14:08
  3. Showing a error message
    By srohit24 in forum Qt Programming
    Replies: 2
    Last Post: 21st August 2009, 07:05
  4. what`s means this Error Message?
    By blm in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2008, 16:58
  5. Qtopia-arm error message ?
    By anafor2004 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th March 2008, 08:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.