Results 1 to 6 of 6

Thread: can Qt help me with this c++ problem?

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    36
    Thanked 1 Time in 1 Post

    Default can Qt help me with this c++ problem?

    Hi,

    I'd like to do something that I have no idea how to do even in c++ but maybe Qt has someting that can help me?
    I was wondering how you would set up an "if statement" where the condition is a variable (and whether that is even possible). This is what I mean:
    i have:
    Qt Code:
    1. if(statement1) dosomething1();
    2. if(statement2) dosomething2();
    To copy to clipboard, switch view to plain text mode 
    But I want the statements in the "if clause" to come from a user-made text file. For example if user makes a text file like so:

    a<b || a>c
    a>=b && a<=C

    Then the program is going to execute these lines:
    Qt Code:
    1. if(a<b || a>c) dosomething1()
    2. if(a>=b && a<=C) dosomething2();
    To copy to clipboard, switch view to plain text mode 
    Is this even possible and what would be the best way to do it?
    Thans a lot.

    Eric

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

    Default Re: can Qt help me with this c++ problem?

    You need a parser and an evaluator for that. Compiled languages such as C++ are not the best choice here, interpreted languages that have an eval() function available (such as perl or php) are the easiest to use here although also the least safe.

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

    eric (25th February 2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    57
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    36
    Thanked 1 Time in 1 Post

    Default Re: can Qt help me with this c++ problem?

    Wysota, thank you for your clarification.
    I'd really like to use C/C++ however. What are parser and evaluator? I'd like to attemp this with C/C++.
    I apologize for asking a question that is not really a Qt question but maybe just this one time it's OK. Thanks a bunch!

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

    Default Re: can Qt help me with this c++ problem?

    Quote Originally Posted by eric View Post
    What are parser and evaluator?
    Parser is a function that is able to parse a literal string and form something meaningful for the machine (like a syntax tree). Evaluator is a function that executes the syntax tree.

    For instance if you have a "2+3" statement a parser would form a tree with "+" as the root and "2" and "3" as leaves of the tree. Then the evaluator (which can be integrated with the parser) calculates the result of the above to "5".

    Google for "bison", "yacc" and take a look here: http://labs.trolltech.com/page/Projects/Compilers/QLALR

  6. The following user says thank you to wysota for this useful post:

    eric (25th February 2008)

  7. #5
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: can Qt help me with this c++ problem?

    As wysota already mentioned, it would be much easier to use a scripting language for this, where you can directly evaluate strings from text files.

    You might want to look at QtScript. Then you can build your entire application in C++, but evaluate the user's expressions from a text file in javascript, and use the result in C++ again. Have a look here: http://doc.trolltech.com/4.3/qtscript.html

  8. #6
    Join Date
    Jun 2007
    Posts
    62
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 1 Time in 1 Post

    Default Re: can Qt help me with this c++ problem?

    I will also note that one can program something like that with the programming language Python (since it has an eval function), and there exist Python bindings for Qt (called PyQt).

Similar Threads

  1. problem with opengl, zooming, drawpixels, and origin
    By ntp in forum General Programming
    Replies: 0
    Last Post: 22nd February 2008, 22:48
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 11:35
  3. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 08:08
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 15:08
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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.