Results 1 to 10 of 10

Thread: help to convert code from c++ to qt

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default help to convert code from c++ to qt

    Hi all!

    I have a function that was given to me from a c++ project and I have to use it in qt. The function receives a byte value and according to that byte value creates a bitarray with true and false values that represent flags.

    Lets say that I have 7 flags {cancel, retry, confirm, stop, manual, resume, skip}

    Then for example the value:
    - 1 means that the cancel is active.
    - 2 means that the retry is active.
    - 2 means that the cancel and the retry are active.
    - 4 means that the confirm is active.
    - 5 means that the cancel and the confirm are active
    ...
    ...
    and so on until 127 where all are active

    a co-worker of mine wrote a function to do this in C++ but I can use it well in Qt:

    Qt Code:
    1. private BitArray ToBitArray(byte b)
    2. {
    3. BitArray ba = new BitArray(8);
    4.  
    5. for (int i = 7; i >= 0; i--) {
    6.  
    7. if (b >= Convert.ToByte(Math.Pow(2d, (double)i))) {
    8.  
    9. ba[i] = true;
    10. b = Convert.ToByte(b - Math.Pow(2d, (double)i));
    11. } else {
    12. ba[i] = false;
    13. }
    14. }
    15.  
    16. return ba;
    17. }
    To copy to clipboard, switch view to plain text mode 

    so far in qt I have this:

    Qt Code:
    1. int flags = 6;
    2.  
    3. QBitArray ba; ba.resize(8);
    4.  
    5. for (int i = 7; i >= 0; i--){
    6.  
    7. if(flags >= int(pow(double(i),2))){
    8. ba[i] = true;
    9. flags = int(flags - pow(double(i),2));
    10. }else{
    11. ba[i] = false;
    12. }
    13. }
    14.  
    15. for (int i = 0; i < ba.size(); i++){
    16. if(ba[i]){
    17. cout << i << ": true" << endl;
    18. }else {
    19. cout << i << ": false" << endl;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    I get some true and false correct but most of them are wrong.

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help to convert code from c++ to qt

    why do you have top convert the code? Qt is C++. you can use c++-code in any qt-project (ok, except pyqt).

  3. #3
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: help to convert code from c++ to qt

    Quote Originally Posted by FelixB View Post
    why do you have top convert the code? Qt is C++. you can use c++-code in any qt-project (ok, except pyqt).
    I can't use the BitArray so I have to use the QBitArray, I can't use the byte variable type and the Convert.ToByte function. What do I have to include to use those variables and functions??

  4. #4
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help to convert code from c++ to qt

    Ruben, that code that have BitArray isn't C++ code, it's C# (C-Sharp) code.

    A byte is a unsigned char, so. Instead of using int flags, replace to unsigned char flags.

    Note:
    unsigned char = 1 byte = 8 bits
    int (or unsigned int) = 4 byte = 32 bits

    Qt Code:
    1. int flags = 6;
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. unsigned char flags = 6;
    To copy to clipboard, switch view to plain text mode 

    Now you need to convert this flags = int(flags - pow(double(i),2)); to byte, right ?
    As the type is just an unsigned char, try this:

    Qt Code:
    1. flags = (unsigned char)(flags - pow(double(i), 2));
    To copy to clipboard, switch view to plain text mode 

    Note: If you don't need for check for big-endian computers I think it will work, and if you want this works for all system, I think you will need to do some research on this, it's simple ^^
    (Someone corrects me if this works in big-endian computers)

    If you want to read more:
    http://en.wikipedia.org/wiki/Endianness
    Last edited by rsilva; 11th May 2011 at 13:41.

  5. #5
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: help to convert code from c++ to qt

    thanks for your time.

    You are right, the code is C#. when I use the (byte) typecasting, the compliler gives me this error @ flags = (byte)(flags - pow(double(i), 2)); // ‘byte’ was not declared in this scope

    what can I do about that?

  6. #6
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help to convert code from c++ to qt

    I've just fixed it in my post:

    Qt Code:
    1. flags = (unsigned char)(flags - pow(double(i), 2));
    To copy to clipboard, switch view to plain text mode 

    or you can just declare tye type too ^^:

    Qt Code:
    1. typedef unsigned char byte;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: help to convert code from c++ to qt

    thanks for your time. I can compile the code again but it is still not working properly. Maybe the C# code was already wrong.

    For ex. the flags = 4 should give me the ba[0] = false, the ba[1] = false and the ba[2] = true. Although it tell me that all first 3 are true. The same with flags = 2, should give the only the ba[1] as true but it gives me the ba[0] = true as well.

    Isn't there already a working function available? I looked up but didn't find anything.

  8. #8
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help to convert code from c++ to qt

    what the others are also telling is that there is no data type called "byte" in c++.
    Instead you can use char or unsigned char, just check this out:http://cplusplus.com/doc/tutorial/variables/

    Anyway if you would like to use something just slightly little bit more intuitive you may like uint8_t in stdint.h which is available under qt, but is not that "world-wide", for example DevC++ does not recognise, nor does the simple gcc which I tried once under a Linux distro. As you can see on page http://linux.die.net/include/stdint.h it is not really anything more than some typedefs, but it is convenient in a way. But let me point out also again that it is not surely the best solution as this header is not obviously given. But its web page gives at least a hint which typedef gives what sort of variable.
    Szilvi

  9. #9
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help to convert code from c++ to qt

    You want to convert the flags to an array ?

    If you're trying to do this, I think using the AND or OR operators may be better.
    http://www.cprogramming.com/tutorial...operators.html

  10. #10
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: help to convert code from c++ to qt

    Quote Originally Posted by rsilva View Post
    You want to convert the flags to an array ?

    If you're trying to do this, I think using the AND or OR operators may be better.
    http://www.cprogramming.com/tutorial...operators.html
    I am trying to create a bitarray with boolean values and allegedly the C# code already does that.

    The flags variable represents the sum of the available flags and those are the following:

    Cancel = 0x0000001
    Retry = 0x0000002
    Confirm = 0x0000004
    Stop = 0x0000008
    Manual = 0x0000010
    Resume = 0x0000020
    Skip = 0x0000040

    Then for ex.:
    - 1 is only cancel.
    - 2 is only retry.
    - 3 is cancel/retry.
    - 4 is only confirm
    ....
    Although when I use the flag 4 it gives me cancel/confirm ... and for example the 2 give me cancel/retry

    Is there already an example of my code idea? I looked up but didn't find it.

Similar Threads

  1. Replies: 3
    Last Post: 23rd December 2010, 00:01
  2. how to convert qt3 code into qt4...
    By kamal in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2010, 08:25
  3. How to convert XML+XSL to PDF in QT4.5
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2009, 23:14
  4. how to convert a xml + xsl to PDF in Qt4.4.3
    By richardander in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2009, 11:01
  5. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47

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.