Results 1 to 9 of 9

Thread: How come this doesnt work?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How come this doesnt work?

    Quote Originally Posted by ShaChris23 View Post
    I wrote some code that tries to extract an integer 100 out of this string:

    "AB,SH#100 \r\n\0"
    That's exactly what I want. Just try extracting it.[/QUOTE]

    Qt Code:
    1. QString func(const QString &s){ return "100"; }
    To copy to clipboard, switch view to plain text mode 
    I doubt that's what you want...

    If you don't tell us what are the rules of extraction in general way, we can't possibly give you code (other than I have given above) to do it. You said "extract 100", so return "100" seems a fine way to do it.

  2. #2
    Join Date
    Apr 2007
    Posts
    62
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    43
    Thanked 1 Time in 1 Post

    Default Re: How come this doesnt work?

    Hi,

    Thanks for all the inputs. How about this:

    A string contains many fields. Each field starts with a 2 ASCII characters followed by the # sign.

    Each field is delimited by a comma.

    The last field is terminated by "\r\n"

    We need to extract the number ( int or float ) after the # sign.

    e.g. "DV#1200.03,AB#34,SH#100\r\n\0"

    So for example, if a user is interested in the field "SH", he has to

    Extract the SH field out,
    Chop off first 3 characters
    Convert the remaining string to be a number.

    I hope this is helpful. I might be wrong here...but I think there's a bug in the Qt's QstringList::filter() method.

    Thanks!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How come this doesnt work?

    I'd do:
    Qt Code:
    1. float extractCmd(const QString &str, const QString &expected){
    2. if(expected.size()>2 || str.size()<4) return 0;
    3. QStringList commands = str.simplified().split(",");
    4. static QRegExp rx("([A-Z][A-Z])#([0-9]*\\.?[0-9]+)");
    5. foreach(QString cmd, commands){
    6. if(!rx.exactMatch(cmd))
    7. continue;
    8. if(rx.cap(1)==expected)
    9. return rx.cap(2).toFloat();
    10. }
    11. }
    12.  
    13. QString str = "DV#1200.03,AB#34,SH#100\r\n\0";
    14. float val = extractCmd(str, "SH");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QWidget -> updateGeometry dont work.
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 22nd May 2013, 15:55
  2. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  3. Work offline and Archive web page in Konqueror
    By jamadagni in forum KDE Forum
    Replies: 0
    Last Post: 22nd February 2006, 09:18
  4. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 12:02

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
  •  
Qt is a trademark of The Qt Company.