Results 1 to 6 of 6

Thread: Random number of QGraphicsItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Random number of QGraphicsItem

    Hi All,
    I have to draw a custom QGraphicsItem or QGraphicsRectItem ..depending on the user input..for eg if the input is 3...then i should be drawing 9 items in a 3 X 3 matrix...if the input is 4 ..then 16 items in 4 X 4 matrix...the problem is that there can be any value as a input so the cordinates and the size of the item changes...the smaller the item is when the bigger the number is...any solution ??...i was thinking of putting each condition in a if block but obviously that isnt a good idea ..

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Random number of QGraphicsItem

    What exactly if the question?
    Do you need help with the logic of the application, or working with QGraphcis View?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Random number of QGraphicsItem

    i jus need to know if its possible and if it is how it is possible logicaly...

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Random number of QGraphicsItem

    Try this:

    Qt Code:
    1. #include "matrixscene.h"
    2.  
    3. #include <QGraphicsRectItem>
    4.  
    5. MatrixScene::MatrixScene(QObject *parent) :
    6. {
    7. setSceneRect(0,0,200,200);
    8. }
    9.  
    10. void MatrixScene::setNumberOfItems(int number)
    11. {
    12. items().clear();
    13.  
    14. qreal rectWidth = (width() / number) - 5;
    15. qreal rectHeight = (height() / number) - 5;
    16.  
    17. qreal startX = 1;
    18. qreal startY = 1;
    19.  
    20. int counterX = 0;
    21. int counterY = 0;
    22.  
    23.  
    24. for(counterY = 0; counterY < number; ++counterY) {
    25. startY = counterY * (rectHeight + 5);
    26. for(counterX = 0; counterX < number; ++counterX) {
    27. startX = counterX * (rectWidth + 5);
    28. newItem = new QGraphicsRectItem;
    29. newItem->setRect(0, 0, rectWidth, rectHeight);
    30. newItem->setPos(startX, startY);
    31. addItem(newItem);
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to tbscope for this useful post:

    salmanmanekia (10th June 2010)

  6. #5
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Random number of QGraphicsItem

    it compiles succesfuly but there is no output....when i remove the for blocks i can see my view and widgets but as i include the for blocks No Output !

  7. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Random number of QGraphicsItem

    You must call setNumberOfItems somewhere, in your constructor for example.
    If you define it as a slot, you can create a combobox or lineedit with button and use it to change the matrix.

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 16:26
  2. Random Number Qt MacOs
    By Daniela in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 17:40
  3. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33
  4. Random No Generator in C/C++
    By ankurjain in forum General Programming
    Replies: 1
    Last Post: 6th July 2006, 11:33
  5. Replies: 7
    Last Post: 31st May 2006, 09:37

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.