Results 1 to 14 of 14

Thread: Embeded QGraphicsProxyWidget away from boundingRect

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Embeded QGraphicsProxyWidget away from boundingRect

    Hi,

    Hi, I'm embedding and widget into a scene with a QGraphicsProxyWidget but the embedded widget is away from its boundingRect. See following image



    Here is the code that adds it:
    Qt Code:
    1. QGraphicsProxyWidget *item = new QGraphicsProxyWidget;
    2.  
    3. comboWidget = new myComboBox;
    4. item->setWidget(comboWidget);
    5. comboWidget->setValue("Item 1");
    6.  
    7. QPointF draggedPoint;
    8. draggedPoint.setX(100);
    9. draggedPoint.setY(100);
    10. myscene.addItem(item);
    11. item->setPos(draggedPoint);
    12. item->setFlag(QGraphicsItem::ItemIsMovable, true);
    13. item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    To copy to clipboard, switch view to plain text mode 

    I have been trying to move the widget to center the boundingRect but nothing seems to work.

    Any ideas will be apppreciated,
    Carlos.

  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: Embeded QGraphicsProxyWidget away from boundingRect

    What do you mean that the widget is "away from its boundingRect()"? Bounding rect of an item has nothing to do with its position on the scene.
    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
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embeded QGraphicsProxyWidget away from boundingRect

    Hi,

    For example in the image that I posted I use the boundingRect of the item to draw the black small boxes. For this I capture the position of the boundingRect of the item in the scene and then I draw the small boxes around it. For other item types like polygons the small boxes are around the item because the polygon is centered on his boundingRect but not for Proxy widgets. See below for a polygon



    Many thanks

  4. #4
    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: Embeded QGraphicsProxyWidget away from boundingRect

    Show the code please.
    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.


  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Post Re: Embeded QGraphicsProxyWidget away from boundingRect

    Here is the code that set the boxes around an item

    Qt Code:
    1. void tnkdesignscene::showSelectionItems(QGraphicsItem *Item)
    2. {
    3. float xpos;
    4. float ypos;
    5.  
    6. //The separation between the boundingRect of item and the boxes
    7. separation = 10;
    8.  
    9. //tnkresizeitem is a class of QGraphicsPolygonItem. These are the boxes appearing around item
    10. tnkresizeitem *titem;
    11.  
    12. //Get the position of the item;
    13. xpos = Item->scenePos().x();
    14. ypos = Item->scenePos().y();
    15.  
    16. //Upper left box
    17. titem = new tnkresizeitem();
    18. titem->setX(xpos - (Item->boundingRect().width()/2) -separation);
    19. titem->setY(ypos - (Item->boundingRect().height()/2) - separation);
    20. titem->setZValue(selItemZ);
    21. titem->setResizePosition(tnkresizeitem::UpperLeft);
    22. addItem(titem);
    23.  
    24. //Lower right box
    25. titem = new tnkresizeitem();
    26. titem->setX(xpos + (Item->boundingRect().width()/2) + separation);
    27. titem->setY(ypos + (Item->boundingRect().height()/2) + separation);
    28. titem->setZValue(selItemZ);
    29. titem->setResizePosition(tnkresizeitem::LowerRight);
    30. addItem(titem);
    31.  
    32.  
    33. //Lower left box
    34. titem = new tnkresizeitem();
    35. titem->setX(xpos - (Item->boundingRect().width()/2) - separation);
    36. titem->setY(ypos + (Item->boundingRect().height()/2) + separation);
    37. titem->setZValue(selItemZ);
    38. titem->setResizePosition(tnkresizeitem::LowerLeft);
    39. addItem(titem);
    40.  
    41.  
    42. //Upper right box
    43. titem = new tnkresizeitem();
    44. titem->setX(xpos + (Item->boundingRect().width()/2) + separation);
    45. titem->setY(ypos - (Item->boundingRect().height()/2) - separation);
    46. titem->setZValue(selItemZ);
    47. titem->setResizePosition(tnkresizeitem::UpperRight);
    48. addItem(titem);
    49.  
    50.  
    51. //Top box
    52. titem = new tnkresizeitem();
    53. titem->setX(xpos);
    54. titem->setY(ypos - (Item->boundingRect().height()/2) - separation);
    55. titem->setZValue(selItemZ);
    56. titem->setResizePosition(tnkresizeitem::Top);
    57. addItem(titem);
    58.  
    59.  
    60. //Bottom box
    61. titem = new tnkresizeitem();
    62. titem->setX(xpos);
    63. titem->setY(ypos + (Item->boundingRect().height()/2) + separation);
    64. titem->setZValue(selItemZ);
    65. titem->setResizePosition(tnkresizeitem::Bottom);
    66. addItem(titem);
    67.  
    68.  
    69. //Left box
    70. titem = new tnkresizeitem();
    71. titem->setX(xpos - (Item->boundingRect().width()/2) - separation);
    72. titem->setY(ypos);
    73. titem->setZValue(selItemZ);
    74. titem->setResizePosition(tnkresizeitem::Left);
    75. addItem(titem);
    76.  
    77.  
    78. //Right box
    79. titem = new tnkresizeitem();
    80. titem->setX(xpos + (Item->boundingRect().width()/2) + separation);
    81. titem->setY(ypos);
    82. titem->setZValue(selItemZ);
    83. titem->setResizePosition(tnkresizeitem::Right);
    84. addItem(titem);
    85.  
    86. }
    To copy to clipboard, switch view to plain text mode 

    Here is the definition of tnkresizeitem used in the function
    Qt Code:
    1. class tnkresizeitem : public QGraphicsPolygonItem
    2. {
    3. public:
    4. enum { Type = UserType + 37 };
    5. enum resizePositions { None, UpperLeft, LowerRight, UpperRight, LowerLeft, Top, Bottom, Left, Right };
    6. tnkresizeitem();
    7. int type() const { return Type; }
    8. void setResizePosition(resizePositions position) {resizeposition = position;};
    9. resizePositions getResizePosition() {return resizeposition;};
    10. protected:
    11. void hoverEnterEvent(QGraphicsSceneHoverEvent *event); //Will change the color to blue
    12. void hoverLeaveEvent(QGraphicsSceneHoverEvent * event); //Will change the color to black
    13. resizePositions resizeposition; //Where the item is
    14. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: Embeded QGraphicsProxyWidget away from boundingRect

    You are assuming that the point of origin of the item's coordinate system is in the middle of the item which is not the case for proxy widget items where the origin is in top left corner of the item. Better use:

    Qt Code:
    1. QPointF topLeft = item->mapToScene(item->boundingRect().topLeft());
    2. QPointF bottomLeft = item->mapToScene(item->boundingRect().bottomLeft());
    3. QPointF topRight = item->mapToScene(item->boundingRect().topRight());
    4. QPointF bottomRight = item->mapToScene(item->boundingRect().bottomRight());
    To copy to clipboard, switch view to plain text mode 
    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.


  7. #7
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embeded QGraphicsProxyWidget away from boundingRect

    Here is an small application that re-creates the problem.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. scene.setSceneRect(0, 0, 800, 480);
    9.  
    10. QGraphicsProxyWidget *w = new QGraphicsProxyWidget;
    11. w->setWidget(new QPushButton("Test"));
    12. w->widget()->setGeometry(0,0,100,100); //Makes the pushbutton 100x100
    13.  
    14. //Creates a polygon of 100x100
    15. QPolygonF poly;
    16. poly << QPointF(-50, -50) << QPointF(50, -50)
    17. << QPointF(50, 50) << QPointF(-50, 50)
    18. << QPointF(-50, -50);
    19.  
    20.  
    21. p->setPolygon(poly);
    22.  
    23. //Sets both polygon and proxy widget in the same position.
    24. p->setPos(100,100);
    25. w->setPos(100,100);
    26. //This would look as if the proxy widget has a black border all around it but this does not work.
    27.  
    28.  
    29. scene.addItem(w);
    30. scene.addItem(p);
    31. QGraphicsView view(&scene);
    32. view.show();
    33.  
    34. return app.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Carlos.

  8. #8
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embeded QGraphicsProxyWidget away from boundingRect

    Thanks for the reply.

    Is there any way to change the origin or move the internal widget?

  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: Embeded QGraphicsProxyWidget away from boundingRect

    You can always subclass the proxy and reimplement boundingRect() for it or place it in another item with a boundingRect fitting your design.
    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.


  10. #10
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embeded QGraphicsProxyWidget away from boundingRect

    Hi,

    I opted for sub-classing the proxy and reimplementing boudingrect but I still cant make it.

    Here is the implementation:
    Qt Code:
    1. QRectF boundingRect() const
    2. {
    3. QRectF newbound;
    4. newbound.setWidth(widget()->geometry().width());
    5. newbound.setHeight(widget()->geometry().height());
    6. QPointF center;
    7. center.setX(0);
    8. center.setY(0);
    9. newbound.moveCenter(center); //Centers the boundrect in 0,0 just like other types of items
    10. return newbound;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Here is the result:


    With this code is almost the same but just a quarter of the item is shown.

  11. #11
    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: Embeded QGraphicsProxyWidget away from boundingRect

    I don't think that's enough as paint() still paints starting with (0,0). You probably have to reimplement it as well and translate the painter before calling the base class implementation. An alternative would be to implement an "effect" for the item that would translate it to coordinates you expect. Then you wouldn't have to touch the proxy class itself assuring that you don't break the way events are handled, etc.
    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.


  12. #12
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embeded QGraphicsProxyWidget away from boundingRect

    ok. I reimplemented the proxy paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)

    How can I tell the painter to start somewhere and not 0,0?

  13. #13
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embeded QGraphicsProxyWidget away from boundingRect

    I used the translate(x,y) but this mess up with the translations between the widget and proxy in the scene.

    This may be a bit messy... I will follow your other suggestion of place it in another item with a boundingRect fitting your design.

    Let me see if I can work out this suggestion.

  14. #14
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: Embeded QGraphicsProxyWidget away from boundingRect

    I managed to do it by embedding the proxy into a QGraphicsRectItem.

    Thanks a lot for your advice.

    [CLOSED]

    Carlos.

Similar Threads

  1. Question on QGraphicsProxyWidget and boundingRect
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2010, 12:29
  2. cross compile Qt Embeded with Open GL ES
    By efan in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 23rd September 2009, 12:23
  3. Resizing Mfc dialog with embeded QWinWidget
    By elizabeth.h1 in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2009, 21:29
  4. QContextMenu in an embeded widget
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2008, 19:20
  5. QGraphicsView and embeded widgets
    By bunjee in forum Qt Programming
    Replies: 10
    Last Post: 12th October 2008, 07:43

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.