Results 1 to 1 of 1

Thread: Extending QGraphicsLayout

  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Extending QGraphicsLayout

    Hello everyone.
    I'm trying to extend QGraphicsLayout to build a new layout (let's called circleLayout). the program will be a plasma applet for KDE 4.2

    First of all the example found in the documentation http://doc.trolltech.com/4.4/layout....ayout-managers doesn't works (i think it's because it's written to extend QLayout).
    As explained in the page http://doc.trolltech.com/4.4/qgraphi...t.html#details i extended the method setGeometry() and sizeHint(). I took a look at source code of QGraphicsGridLayout and it doesn't seems that i need to extend other methods. Obviously, my code doesn't works
    here's my code:
    Qt Code:
    1. void circleLayout::setGeometry(const QRectF &r)
    2. {
    3. kDebug()<<"Set Geometry!!";
    4.  
    5. /*setting the real geometry*/
    6. QGraphicsLayout::setGeometry(r);
    7. /*taking the geometry*/
    8. QRectF effectiveRect = geometry();
    9. qreal left, top, right, bottom;
    10. getContentsMargins(&left, &top, &right, &bottom);
    11. /*adjust margins*/
    12. effectiveRect.adjust(+left, +top, -right, -bottom);
    13.  
    14. kDebug()<<left<<","<<top<<","<<right<<","<<bottom;
    15.  
    16. /*this is how qrectf works: Qrectf (xy top left corner, width ,height)*/
    17.  
    18. /*if there are no elements in the list, view only the main element*/
    19. /*finding the center of the rectangular*/
    20. qreal centerX = left + (right/2);
    21. qreal centerY = top + (bottom/2);
    22. /*finding the radius of the circle*/
    23. qreal radius = 0;
    24. if(right > bottom)
    25. radius = bottom/2;
    26. else
    27. radius = right/2;
    28.  
    29. /*the main element is located in the center and fill 1/3 of th space*/
    30. /*setting the rectf for the main element*/
    31. QRectF mainRect(centerX+radius/3, centerY+radius/3, radius*2/3, radius*2/3);
    32. mainElement->setGeometry(mainRect);
    33.  
    34. kDebug() << "setting the main element geom";
    35.  
    36. /*if i have only one element, i'll put in the top center*/
    37. if(list.size() == 1)
    38. {
    39. kDebug()<<"there is only one element";
    40. QGraphicsLayoutItem* element = list.at(0);
    41. /*putting this element at the top in the center*/
    42. QRectF elementRect(centerX+radius/3, centerY+radius, radius*2/3, radius*2/3);
    43. element->setGeometry(elementRect);
    44. }
    45. /*else i'll put the elements in the circle*/
    46. else if(list.size() > 1)
    47. {
    48. kDebug()<<"there are more elements, "<<list.size();
    49. for(int i=0; i<list.size(); i++)
    50. {
    51. /*this is the angle to rotate*/
    52. double angle = (2*PI*i)/list.size();
    53. /*than i made a rotation in center with the specified radius*/
    54. QMatrix rotMatrix(cos(angle), -sin(angle),
    55. sin(angle), cos(angle),
    56. centerX,centerY);
    57. /*then i multiply the matrix with the point on the top of the circle*/
    58. qreal pointX;
    59. qreal pointY;
    60. /*rotation*/
    61. rotMatrix.map(centerX,centerY+(radius*2)/3,&pointX, &pointY);
    62.  
    63. /*taking the i-esim element*/
    64. QGraphicsLayoutItem* element = list.at(i);
    65. /*try to put this element in the circle*/
    66. QRectF elementRect(pointX-radius/3, pointX-radius, radius*2/3, radius*2/3);
    67. element->setGeometry(elementRect);
    68. }
    69. }
    70. }
    71.  
    72. QSizeF circleLayout :: sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
    73. {
    74. kDebug()<<"sizeHint!";
    75.  
    76. QSizeF s(0,0);
    77. int n = list.count();
    78. if (n > 0)
    79. s = QSizeF(100,70); //start with a nice default size
    80. int i = 0;
    81. while (i < n) {
    82. QGraphicsLayoutItem *o = list.at(i);
    83. s = s.expandedTo(o->effectiveSizeHint(which, constraint));
    84. ++i;
    85. }
    86. return s ;
    87. }
    To copy to clipboard, switch view to plain text mode 
    (the method sizeHint is semi-copied from QGraphicsGridLayout. i don't understand the true meaning of it)
    So, here what's happens: i can compile everything without any problem, but i cannot see my object (maybe are too small?)
    I'm expecting to find my objects placed around a main object (in the center)
    As you may seen in the attachment, there are some dots in the center.. well, i think they're my objects!

    here is the debug text i receive in my shell:
    Qt Code:
    1. plasmoidviewer(29212) AdvancedMenuLayout::sizeHint: sizeHint!
    2. plasmoidviewer(29212) AdvancedMenuLayout::sizeHint: sizeHint!
    3. plasmoidviewer(29212) AdvancedMenuLayout::sizeHint: sizeHint!
    4. plasmoidviewer(29212) AdvancedMenuLayout::setGeometry: Set Geometry!!
    5. plasmoidviewer(29212) AdvancedMenuLayout::setGeometry: 4 , 4 , 4 , 4
    6. plasmoidviewer(29212) AdvancedMenuLayout::setGeometry: setting the main element geom
    7. plasmoidviewer(29212) AdvancedMenuLayout::setGeometry: there is only one element
    8. QPainter::begin: Cannot paint on a null pixmap
    9. QPainter::begin: Cannot paint on a null pixmap
    10. QPainter::begin: Cannot paint on a null pixmap
    11. QPainter::begin: Cannot paint on a null pixmap
    12. QPainter::begin: Cannot paint on a null pixmap
    13. QPainter::begin: Cannot paint on a null pixmap
    To copy to clipboard, switch view to plain text mode 

    The method setGeometry is never called!
    Finally, here are my questions:
    how can i extend QGraphicsLayout? what i have to do?
    What i exactly have to implement inside sizeHint()? In what method i can specify where to place my elements?

    thanks everyone in advance
    Attached Images Attached Images
    Last edited by Darkman; 13th March 2009 at 12:39.

Similar Threads

  1. Extending a plugin in a static library
    By ultim8 in forum Qt Programming
    Replies: 5
    Last Post: 25th March 2010, 15:10
  2. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  3. Replies: 0
    Last Post: 3rd January 2009, 05:26
  4. Extending the Diagram Scene Example
    By dosto.walla in forum Newbie
    Replies: 1
    Last Post: 7th October 2008, 18:02
  5. Video Stream, extending QIODevice
    By rextr in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2008, 08:55

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.