Results 1 to 5 of 5

Thread: QT enum array

  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QT enum array

    Hi everybody,

    How can I declare an array of shape in QPainter?

    In the header file, I have an enumerated shape. It works fine without making array. But now I want to have an array of size 10 of this enumerated shape. I could not successfully convert the shape to an array of size 10. Please help..

    Here are my code pieces:

    my header file

    Qt Code:
    1. enum Shape {Kare, Hexa, Rect};
    2. void setShape(Shape shape[10]);
    3. Shape shape[10];
    To copy to clipboard, switch view to plain text mode 

    my cpp file

    Qt Code:
    1. void RenderArea1::setShape(Shape shape[10])
    2. {
    3. for (int kat = 0; kat < 10; kat += 1) {
    4. this->shape[kat] = shape[kat];
    5. update();
    6. }
    7. }
    8.  
    9. for (int kat = 0; kat < 10; kat += 1) {
    10.  
    11. switch (shape[kat]) {
    12.  
    13. case Kare:
    14. ....
    15. case Hexa:
    16. ....
    17. case Rect:
    18. ....
    19. }
    20. }
    21.  
    22. RenderArea1::Shape shape[Window::kat] = renderArea1->Kare;
    23. renderArea1->setShape(shape[Window::kat]);
    24. renderArea1->update();
    To copy to clipboard, switch view to plain text mode 


    Thanks in advance..
    Last edited by wysota; 3rd April 2011 at 08:27. Reason: missing [code] tags

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

    Default Re: QT enum array

    I could not successfully convert the shape to an array of size 10.
    What does it mean exactly? It doesn't compile? What error do you get?
    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.


  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT enum array

    Hard-coded stuff is bad. Imagine that at some point you'd like to use 20 shapes instead of 10 - in your case that means replacing the values in every place in your code. This is bug-prone, because you can forget about something. Simple improvement could be to use #define for number of shapes:
    Qt Code:
    1. #define N_SHAPES 10
    2. ...
    3. enum Shape {Kare, Hexa, Rect};
    4. void setShape(Shape shape[N_SHAPES]);
    5. Shape shape[N_SHAPES];
    6. ...
    7. for (int kat = 0; kat < N_SHAPES; kat += 1) {
    8. this->shape[kat] = shape[kat];
    9. update();
    10. }
    11. //... and so on
    To copy to clipboard, switch view to plain text mode 
    Furthermore, I'd switch to one of Qt containers instead of C-styled arrays - for example, take a look at QList.

  4. #4
    Join Date
    Apr 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT enum array

    thanks stampede but I still got these errors:

    for line 22
    expected constant expression
    cannot allocate an array of constant size 0
    error C2440: 'initializing' : cannot convert from 'RenderArea1::Shape' to 'RenderArea1::Shape []'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    for line 23
    'RenderArea1::setShape' : cannot convert parameter 1 from 'RenderArea1::Shape' to 'RenderArea1::Shape []'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Last edited by angarali06; 3rd April 2011 at 08:54.

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT enum array

    Qt Code:
    1. RenderArea1::Shape shape[Window::kat] = renderArea1->Kare;
    To copy to clipboard, switch view to plain text mode 
    What do you mean by this line ? Window::kat is some kind of enum value ? This declares an array of Shapes, with number of elements equal to Window::kat value, which seems to be 0 according to compiler message. Next you want to assign a single value for the array, you can't do that because there are different types.
    renderArea1->setShape(shape[Window::kat]);
    setShape method expects an array, and you are giving it only one value ( shape[Window::kat] is probably the same as shape[0], assuming that Window::kat==0 ). Probably you mean
    Qt Code:
    1. renderArea1->setShape(shape);
    To copy to clipboard, switch view to plain text mode 
    but there is still first error to fix.
    For me this looks like a basic C/C++ issues, not really related to Qt.

Similar Threads

  1. Replies: 2
    Last Post: 12th November 2010, 14:42
  2. How to export enum ?
    By Peppy in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2010, 15:15
  3. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  4. enum property
    By illuzioner in forum Qt Tools
    Replies: 10
    Last Post: 22nd August 2006, 21:47
  5. enum scope
    By illuzioner in forum General Programming
    Replies: 1
    Last Post: 15th February 2006, 05:39

Tags for this Thread

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.