Results 1 to 9 of 9

Thread: Structs for C++ and Qt

  1. #1
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Structs for C++ and Qt

    just wondering if there is a set way of doing structs in qt. i have made structs before in the past using C, but im not too sure about using C++ to do it and to make it work with the Qt stuff. any idea's???

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

    Default Re: Structs for C++ and Qt

    Try the same way as in C

    Qt Code:
    1. struct Point {
    2. int x;
    3. int y;
    4. };
    To copy to clipboard, switch view to plain text mode 

    BTW. Structs are treated by C++ as public classes, so you can add methods to them.

  3. #3
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Structs for C++ and Qt

    ah ok thankyou...whilst im on the subject of storage, would it be easier to have a 2-d array of booleans or an array of linked lists??how would i create a 2-d array of booleans anyway because ive never tried it before - even with C??

  4. #4
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Structs for C++ and Qt

    Quote Originally Posted by wysota
    Try the same way as in C

    Qt Code:
    1. struct Point {
    2. int x;
    3. int y;
    4. };
    To copy to clipboard, switch view to plain text mode 

    BTW. Structs are treated by C++ as public classes, so you can add methods to them.
    Ah... so that explains why Fullmetalcoder can, in the "devqthighlighter.h" header of his "DevQt" project, use
    Qt Code:
    1. class CodeBlock;
    2. ....
    3. ....
    4. struct CodeBlock{
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    It compiles with a ton of warnings, an executable, but no errors. If I change the declaration to "struct CodeBlock;" and compile then I get a ton of errors and no executable.

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

    Default Re: Structs for C++ and Qt

    Quote Originally Posted by therealjag
    ah ok thankyou...whilst im on the subject of storage, would it be easier to have a 2-d array of booleans or an array of linked lists??how would i create a 2-d array of booleans anyway because ive never tried it before - even with C??
    It depends. In general there are no such things as 2D arrays. How many bools do you need in each "row"? Maybe you could use bit fields?

  6. #6
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Structs for C++ and Qt

    It depends. In general there are no such things as 2D arrays. How many bools do you need in each "row"? Maybe you could use bit fields?
    well i'd probably need around 30 maximum but around 10 would do for now....what are bit fields though i have never used them before?

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

    Default Re: Structs for C++ and Qt

    Then bitfields will do fine.

    Qt Code:
    1. struct bfentry{
    2. unsigned int
    3. one:1,
    4. two:1,
    5. three:1,
    6. four:1,
    7. //...
    8. thirtytwo:1;
    9. }
    To copy to clipboard, switch view to plain text mode 

    The above structure occupies.... 4 bytes Making it an array is easy now;

    Of course you can also use: std::vector< std::vector< bool > >

  8. #8
    Join Date
    Apr 2011
    Location
    Macerata, Italy
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Structs for C++ and Qt

    Dear All,
    I've a similar problem, i need a global struct such as:
    Qt Code:
    1. struct _Stxy1000{
    2. double dY[1000];
    3. double dX[1000];
    4. }
    To copy to clipboard, switch view to plain text mode 
    I've put the previous code in a .h file and the follow:
    Qt Code:
    1. struct _Stxy1000 _stxy1000;
    To copy to clipboard, switch view to plain text mode 
    in a .cpp file which include the .h as definition.
    When i use the struct field members in a .cpp, the compiler rise up some problems, such as "multiple definition" of the _stxy1000 variable.
    Do you think i should use extern in the declaration?
    Could you please post a c-like code??
    Thanks in advance.
    Andrea
    Last edited by wysota; 6th June 2011 at 12:55. Reason: changed [qtclass] to [code]

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

    Default Re: Structs for C++ and Qt

    I think you should definitely end your structure definition with a semicolon.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 13:53

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.